Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//-----------------------------------------------------------------------

using System.Collections.Concurrent;
using System.Reflection;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using NJsonSchema.Generation;
Expand All @@ -20,6 +19,7 @@ public class NewtonsoftJsonSchemaGeneratorSettings : JsonSchemaGeneratorSettings
private readonly ConcurrentDictionary<string, JsonContract?> _cachedContracts = [];

private JsonSerializerSettings _serializerSettings;
private readonly DefaultContractResolver _defaultContractResolver = new();

/// <summary>Initializes a new instance of the <see cref="JsonSchemaGeneratorSettings"/> class.</summary>
public NewtonsoftJsonSchemaGeneratorSettings()
Expand All @@ -44,7 +44,7 @@ public JsonSerializerSettings SerializerSettings
/// <returns>The contract resolver.</returns>
/// <exception cref="InvalidOperationException">A setting is misconfigured.</exception>
[JsonIgnore]
public IContractResolver ActualContractResolver => SerializerSettings?.ContractResolver ?? new DefaultContractResolver();
public IContractResolver ActualContractResolver => SerializerSettings?.ContractResolver ?? _defaultContractResolver;

/// <summary>Gets the contract for the given type.</summary>
/// <param name="type">The type.</param>
Expand All @@ -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
}
}
}