using System.Text.Json; using System.Text.Json.Serialization; using Namotion.Reflection; using NJsonSchema; using NJsonSchema.Generation; /// internal sealed class UmbracoJsonSchemaGenerator : JsonSchemaGenerator { /// /// Initializes a new instance of the class. /// public UmbracoJsonSchemaGenerator() : base(new SystemTextJsonSchemaGeneratorSettings() { AlwaysAllowAdditionalObjectProperties = true, FlattenInheritanceHierarchy = true, IgnoreObsoleteProperties = true, ReflectionService = new UmbracoSystemTextJsonReflectionService(), SerializerOptions = new JsonSerializerOptions() { Converters = { new JsonStringEnumConverter() }, IgnoreReadOnlyProperties = true, }, }) { } /// private sealed class UmbracoSystemTextJsonReflectionService : SystemTextJsonReflectionService { /// public override void GenerateProperties(JsonSchema schema, ContextualType contextualType, SystemTextJsonSchemaGeneratorSettings settings, JsonSchemaGenerator schemaGenerator, JsonSchemaResolver schemaResolver) { // Populate schema properties base.GenerateProperties(schema, contextualType, settings, schemaGenerator, schemaResolver); if (settings.SerializerOptions.IgnoreReadOnlyProperties) { // Remove read-only properties (because this is not implemented by the base class) foreach (ContextualPropertyInfo property in contextualType.Properties) { if (property.CanWrite is false) { string propertyName = GetPropertyName(property, settings); schema.Properties.Remove(propertyName); } } } } } }