-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathJsonSchemaReference.cs
More file actions
159 lines (138 loc) · 6.51 KB
/
JsonSchemaReference.cs
File metadata and controls
159 lines (138 loc) · 6.51 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
namespace Microsoft.OpenApi;
/// <summary>
/// Schema reference information that includes metadata annotations from JSON Schema 2020-12.
/// This class extends OpenApiReference to provide schema-specific metadata override capabilities.
/// </summary>
public class JsonSchemaReference : OpenApiReferenceWithDescription
{
/// <summary>
/// A default value which by default SHOULD override that of the referenced component.
/// If the referenced object-type does not allow a default field, then this field has no effect.
/// You must use the <see cref="JsonNullSentinel.IsJsonNullSentinel(JsonNode?)"/> method to check whether Default was assigned a null value in the document.
/// Assign <see cref="JsonNullSentinel.JsonNull"/> to use get null as a serialized value.
/// </summary>
public JsonNode? Default { get; set; }
/// <summary>
/// A title which by default SHOULD override that of the referenced component.
/// If the referenced object-type does not allow a title field, then this field has no effect.
/// </summary>
public string? Title { get; set; }
/// <summary>
/// Indicates whether the referenced component is deprecated.
/// If the referenced object-type does not allow a deprecated field, then this field has no effect.
/// </summary>
public bool? Deprecated { get; set; }
/// <summary>
/// Indicates whether the referenced component is read-only.
/// If the referenced object-type does not allow a readOnly field, then this field has no effect.
/// </summary>
public bool? ReadOnly { get; set; }
/// <summary>
/// Indicates whether the referenced component is write-only.
/// If the referenced object-type does not allow a writeOnly field, then this field has no effect.
/// </summary>
public bool? WriteOnly { get; set; }
/// <summary>
/// Example values which by default SHOULD override those of the referenced component.
/// If the referenced object-type does not allow examples, then this field has no effect.
/// </summary>
public IList<JsonNode>? Examples { get; set; }
/// <summary>
/// Extension data for this schema reference. Only allowed in OpenAPI 3.1 and later.
/// Extensions are NOT written when serializing for OpenAPI 2.0 or 3.0.
/// </summary>
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; }
/// <summary>
/// Parameterless constructor
/// </summary>
public JsonSchemaReference() { }
/// <summary>
/// Initializes a copy instance of the <see cref="JsonSchemaReference"/> object
/// </summary>
public JsonSchemaReference(JsonSchemaReference reference) : base(reference)
{
Utils.CheckArgumentNull(reference);
Default = reference.Default;
Title = reference.Title;
Deprecated = reference.Deprecated;
ReadOnly = reference.ReadOnly;
WriteOnly = reference.WriteOnly;
Examples = reference.Examples;
Extensions = reference.Extensions != null ? new Dictionary<string, IOpenApiExtension>(reference.Extensions) : null;
}
/// <inheritdoc/>
protected override void SerializeAdditionalV31Properties(IOpenApiWriter writer)
{
if (Type != ReferenceType.Schema) throw new InvalidOperationException(
$"JsonSchemaReference can only be serialized for ReferenceType.Schema, but was {Type}.");
base.SerializeAdditionalV31Properties(writer);
// Additional schema metadata annotations in 3.1
writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d));
writer.WriteProperty(OpenApiConstants.Title, Title);
if (Deprecated.HasValue)
{
writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated.Value, false);
}
if (ReadOnly.HasValue)
{
writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly.Value, false);
}
if (WriteOnly.HasValue)
{
writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly.Value, false);
}
if (Examples != null && Examples.Any())
{
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (w, e) => w.WriteAny(e));
}
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi3_1);
}
/// <inheritdoc/>
protected override void SetAdditional31MetadataFromMapNode(JsonObject jsonObject)
{
base.SetAdditional31MetadataFromMapNode(jsonObject);
var title = GetPropertyValueFromNode(jsonObject, OpenApiConstants.Title);
if (!string.IsNullOrEmpty(title))
{
Title = title;
}
// Boolean properties
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Deprecated, out var deprecatedNode) && deprecatedNode is JsonValue deprecatedValue && deprecatedValue.TryGetValue<bool>(out var deprecated))
{
Deprecated = deprecated;
}
if (jsonObject.TryGetPropertyValue(OpenApiConstants.ReadOnly, out var readOnlyNode) && readOnlyNode is JsonValue readOnlyValue && readOnlyValue.TryGetValue<bool>(out var readOnly))
{
ReadOnly = readOnly;
}
if (jsonObject.TryGetPropertyValue(OpenApiConstants.WriteOnly, out var writeOnlyNode) && writeOnlyNode is JsonValue writeOnlyValue && writeOnlyValue.TryGetValue<bool>(out var writeOnly))
{
WriteOnly = writeOnly;
}
// Default value
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Default, out var defaultNode))
{
Default = defaultNode;
}
// Examples
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Examples, out var examplesNode) && examplesNode is JsonArray examplesArray)
{
Examples = examplesArray.OfType<JsonNode>().ToList();
}
// Extensions (properties starting with "x-")
foreach (var property in jsonObject
.Where(static p => p.Key.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase)
&& p.Value is not null))
{
var extensionValue = property.Value!;
Extensions ??= new Dictionary<string, IOpenApiExtension>(StringComparer.OrdinalIgnoreCase);
Extensions[property.Key] = new JsonNodeExtension(extensionValue.DeepClone());
}
}
}