2022-11-22 12:48:11 +01:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Converters;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
using NJsonSchema.Generation;
|
2024-04-10 12:30:30 +02:00
|
|
|
using NJsonSchema.NewtonsoftJson.Generation;
|
2022-11-22 12:48:11 +01:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public class UmbracoJsonSchemaGenerator : JsonSchemaGenerator
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="UmbracoJsonSchemaGenerator" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public UmbracoJsonSchemaGenerator()
|
2024-04-10 12:30:30 +02:00
|
|
|
: base(new NewtonsoftJsonSchemaGeneratorSettings()
|
2022-11-22 12:48:11 +01:00
|
|
|
{
|
|
|
|
|
AlwaysAllowAdditionalObjectProperties = true,
|
|
|
|
|
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull,
|
|
|
|
|
FlattenInheritanceHierarchy = true,
|
|
|
|
|
IgnoreObsoleteProperties = true,
|
|
|
|
|
SerializerSettings = new JsonSerializerSettings()
|
|
|
|
|
{
|
|
|
|
|
ContractResolver = new WritablePropertiesOnlyResolver()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
{
|
2024-04-10 12:30:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
((NewtonsoftJsonSchemaGeneratorSettings)Settings).SerializerSettings.Converters.Add(new StringEnumConverter());
|
2022-11-22 12:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
private class WritablePropertiesOnlyResolver : DefaultContractResolver
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
|
|
|
|
=> base.CreateProperties(type, memberSerialization).Where(p => p.Writable).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|