Update to NJsonSchema 11.0.0 and use SystemTextJsonSchemaGeneratorSettings (#16030)

This commit is contained in:
Ronald Barendse
2024-04-10 21:25:54 +02:00
committed by GitHub
parent 16dd5327d4
commit 7c2a0f8976
3 changed files with 31 additions and 18 deletions

View File

@@ -8,7 +8,6 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" VersionOverride="2.9.1" />
<PackageReference Include="NJsonSchema" VersionOverride="11.0.0" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" VersionOverride="11.0.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -37,6 +37,7 @@ internal class UmbracoCmsSchema
public ImagingSettings Imaging { get; set; } = null!;
public IndexCreatorSettings Examine { get; set; } = null!;
public IndexingSettings Indexing { get; set; } = null!;
public LoggingSettings Logging { get; set; } = null!;

View File

@@ -1,8 +1,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Namotion.Reflection;
using NJsonSchema;
using NJsonSchema.Generation;
using NJsonSchema.NewtonsoftJson.Generation;
/// <inheritdoc />
public class UmbracoJsonSchemaGenerator : JsonSchemaGenerator
@@ -11,28 +11,41 @@ public class UmbracoJsonSchemaGenerator : JsonSchemaGenerator
/// Initializes a new instance of the <see cref="UmbracoJsonSchemaGenerator" /> class.
/// </summary>
public UmbracoJsonSchemaGenerator()
: base(new NewtonsoftJsonSchemaGeneratorSettings()
: base(new SystemTextJsonSchemaGeneratorSettings()
{
AlwaysAllowAdditionalObjectProperties = true,
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull,
FlattenInheritanceHierarchy = true,
IgnoreObsoleteProperties = true,
SerializerSettings = new JsonSerializerSettings()
ReflectionService = new UmbracoSystemTextJsonReflectionService(),
SerializerOptions = new JsonSerializerOptions()
{
ContractResolver = new WritablePropertiesOnlyResolver()
}
Converters =
{
new JsonStringEnumConverter()
},
WriteIndented = true,
},
})
{
((NewtonsoftJsonSchemaGeneratorSettings)Settings).SerializerSettings.Converters.Add(new StringEnumConverter());
}
{ }
/// <inheritdoc />
private class WritablePropertiesOnlyResolver : DefaultContractResolver
private class UmbracoSystemTextJsonReflectionService : SystemTextJsonReflectionService
{
/// <inheritdoc />
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
=> base.CreateProperties(type, memberSerialization).Where(p => p.Writable).ToList();
public override void GenerateProperties(JsonSchema schema, ContextualType contextualType, SystemTextJsonSchemaGeneratorSettings settings, JsonSchemaGenerator schemaGenerator, JsonSchemaResolver schemaResolver)
{
base.GenerateProperties(schema, contextualType, settings, schemaGenerator, schemaResolver);
// Remove read-only properties
foreach (ContextualPropertyInfo property in contextualType.Properties)
{
if (property.CanWrite is false)
{
string propertyName = GetPropertyName(property, settings);
schema.Properties.Remove(propertyName);
}
}
}
}
}