Skip to content
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 @@ 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);
}
}
}
}
}