diff --git a/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonSchemaGeneratorSettings.cs b/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonSchemaGeneratorSettings.cs index 3605848c1..fd9de959f 100644 --- a/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonSchemaGeneratorSettings.cs +++ b/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonSchemaGeneratorSettings.cs @@ -7,7 +7,6 @@ //----------------------------------------------------------------------- using System.Collections.Concurrent; -using System.Reflection; using Newtonsoft.Json.Serialization; using Newtonsoft.Json; using NJsonSchema.Generation; @@ -20,6 +19,7 @@ public class NewtonsoftJsonSchemaGeneratorSettings : JsonSchemaGeneratorSettings private readonly ConcurrentDictionary _cachedContracts = []; private JsonSerializerSettings _serializerSettings; + private readonly DefaultContractResolver _defaultContractResolver = new(); /// Initializes a new instance of the class. public NewtonsoftJsonSchemaGeneratorSettings() @@ -44,7 +44,7 @@ public JsonSerializerSettings SerializerSettings /// The contract resolver. /// A setting is misconfigured. [JsonIgnore] - public IContractResolver ActualContractResolver => SerializerSettings?.ContractResolver ?? new DefaultContractResolver(); + public IContractResolver ActualContractResolver => SerializerSettings?.ContractResolver ?? _defaultContractResolver; /// Gets the contract for the given type. /// The type. @@ -57,7 +57,14 @@ public JsonSerializerSettings SerializerSettings return null; } +#if NET8_0_OR_GREATER + return _cachedContracts.GetOrAdd( + key, static (_, state) => !state.type.IsGenericTypeDefinition ? state.ActualContractResolver.ResolveContract(state.type) : null, + (type, ActualContractResolver) + ); +#else return _cachedContracts.GetOrAdd(key, s => !type.IsGenericTypeDefinition ? ActualContractResolver.ResolveContract(type) : null); +#endif } } } \ No newline at end of file