-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
UmbracoJsonSchemaGenerator.cs
52 lines (48 loc) · 1.97 KB
/
UmbracoJsonSchemaGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Text.Json;
using System.Text.Json.Serialization;
using Namotion.Reflection;
using NJsonSchema;
using NJsonSchema.Generation;
/// <inheritdoc />
internal class UmbracoJsonSchemaGenerator : JsonSchemaGenerator
{
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoJsonSchemaGenerator" /> class.
/// </summary>
public UmbracoJsonSchemaGenerator()
: base(new SystemTextJsonSchemaGeneratorSettings()
{
AlwaysAllowAdditionalObjectProperties = true,
FlattenInheritanceHierarchy = true,
IgnoreObsoleteProperties = true,
ReflectionService = new UmbracoSystemTextJsonReflectionService(),
SerializerOptions = new JsonSerializerOptions()
{
Converters = { new JsonStringEnumConverter() },
IgnoreReadOnlyProperties = true,
},
})
{ }
/// <inheritdoc />
private class UmbracoSystemTextJsonReflectionService : SystemTextJsonReflectionService
{
/// <inheritdoc />
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);
}
}
}
}
}
}