Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v14: Update to NJsonSchema 11.0.0 and use SystemTextJsonSchemaGeneratorSettings #16030

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion tools/Umbraco.JsonSchema/Umbraco.JsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions tools/Umbraco.JsonSchema/UmbracoCmsSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class UmbracoCmsDefinition
public ImagingSettings Imaging { get; set; } = null!;

public IndexCreatorSettings Examine { get; set; } = null!;

public IndexingSettings Indexing { get; set; } = null!;

public LoggingSettings Logging { get; set; } = null!;
Expand Down
47 changes: 30 additions & 17 deletions tools/Umbraco.JsonSchema/UmbracoJsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,28 +11,41 @@
/// 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);
}
}
}

Check warning on line 49 in tools/Umbraco.JsonSchema/UmbracoJsonSchemaGenerator.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v14/dev)

❌ New issue: Excess Number of Function Arguments

GenerateProperties has 5 arguments, threshold = 4. This function has too many arguments, indicating a lack of encapsulation. Avoid adding more arguments.
}
}
Loading