-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequireValueTypesSchemaProcessor.cs
56 lines (50 loc) · 1.95 KB
/
RequireValueTypesSchemaProcessor.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
53
54
55
56
using System;
using Microsoft.AspNetCore.Mvc;
using NJsonSchema;
using NJsonSchema.Generation;
namespace OpenApiSample
{
public abstract class PatchRequest
{
}
/// <summary>
/// Schema processor that makes all value types (int, string, bool, etc.) required in OpenApi
/// Classes that inherits from PatchRequest are omitted (since all properties in these classes are optional)
/// </summary>
public class RequireValueTypesSchemaProcessor : ISchemaProcessor
{
private static readonly Type _patchRequestType = typeof(PatchRequest);
public void Process(SchemaProcessorContext context)
{
var schema = context.Schema;
if (context.Type.IsSubclassOf(_patchRequestType)
|| context.Type == typeof(ValidationProblemDetails))
{
// Classes that inherits from PatchRequest are omitted (since all properties in these classes are optional)
return;
}
foreach (var propertyKeyValue in schema.Properties)
{
var property = propertyKeyValue.Value;
string propertyName = property.Name;
if (property.Type == JsonObjectType.String || property.Type == JsonObjectType.Boolean ||
property.Type == JsonObjectType.Integer || property.Type == JsonObjectType.Number
|| property.Type == JsonObjectType.None /* enum */
)
{
if (!schema.RequiredProperties.Contains(propertyName))
{
schema.RequiredProperties.Add(propertyName);
}
}
if (property.Type == JsonObjectType.String)
{
if (property.Format != "date-time")
{
property.IsNullableRaw = false;
}
}
}
}
}
}