|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.Collections.Generic; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Text.Json.Nodes; |
7 | | -using Microsoft.OpenApi.Reader; |
8 | 8 |
|
9 | | -namespace Microsoft.OpenApi |
| 9 | +namespace Microsoft.OpenApi; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Schema reference information that includes metadata annotations from JSON Schema 2020-12. |
| 13 | +/// This class extends OpenApiReference to provide schema-specific metadata override capabilities. |
| 14 | +/// </summary> |
| 15 | +public class JsonSchemaReference : BaseOpenApiReference |
10 | 16 | { |
11 | 17 | /// <summary> |
12 | | - /// Schema reference information that includes metadata annotations from JSON Schema 2020-12. |
13 | | - /// This class extends OpenApiReference to provide schema-specific metadata override capabilities. |
| 18 | + /// A default value which by default SHOULD override that of the referenced component. |
| 19 | + /// If the referenced object-type does not allow a default field, then this field has no effect. |
14 | 20 | /// </summary> |
15 | | - public class JsonSchemaReference : OpenApiReference |
16 | | - { |
17 | | - /// <summary> |
18 | | - /// A default value which by default SHOULD override that of the referenced component. |
19 | | - /// If the referenced object-type does not allow a default field, then this field has no effect. |
20 | | - /// </summary> |
21 | | - public JsonNode? Default { get; set; } |
22 | | - |
23 | | - /// <summary> |
24 | | - /// A title which by default SHOULD override that of the referenced component. |
25 | | - /// If the referenced object-type does not allow a title field, then this field has no effect. |
26 | | - /// </summary> |
27 | | - public string? Title { get; set; } |
28 | | - |
29 | | - /// <summary> |
30 | | - /// Indicates whether the referenced component is deprecated. |
31 | | - /// If the referenced object-type does not allow a deprecated field, then this field has no effect. |
32 | | - /// </summary> |
33 | | - public bool? Deprecated { get; set; } |
34 | | - |
35 | | - /// <summary> |
36 | | - /// Indicates whether the referenced component is read-only. |
37 | | - /// If the referenced object-type does not allow a readOnly field, then this field has no effect. |
38 | | - /// </summary> |
39 | | - public bool? ReadOnly { get; set; } |
40 | | - |
41 | | - /// <summary> |
42 | | - /// Indicates whether the referenced component is write-only. |
43 | | - /// If the referenced object-type does not allow a writeOnly field, then this field has no effect. |
44 | | - /// </summary> |
45 | | - public bool? WriteOnly { get; set; } |
46 | | - |
47 | | - /// <summary> |
48 | | - /// Example values which by default SHOULD override those of the referenced component. |
49 | | - /// If the referenced object-type does not allow examples, then this field has no effect. |
50 | | - /// </summary> |
51 | | - public IList<JsonNode>? Examples { get; set; } |
52 | | - |
53 | | - /// <summary> |
54 | | - /// Parameterless constructor |
55 | | - /// </summary> |
56 | | - public JsonSchemaReference() { } |
57 | | - |
58 | | - /// <summary> |
59 | | - /// Initializes a copy instance of the <see cref="JsonSchemaReference"/> object |
60 | | - /// </summary> |
61 | | - public JsonSchemaReference(JsonSchemaReference reference) : base(reference) |
62 | | - { |
63 | | - Utils.CheckArgumentNull(reference); |
64 | | - Default = reference.Default; |
65 | | - Title = reference.Title; |
66 | | - Deprecated = reference.Deprecated; |
67 | | - ReadOnly = reference.ReadOnly; |
68 | | - WriteOnly = reference.WriteOnly; |
69 | | - Examples = reference.Examples; |
70 | | - } |
| 21 | + public JsonNode? Default { get; set; } |
71 | 22 |
|
72 | | - /// <summary> |
73 | | - /// Serialize <see cref="JsonSchemaReference"/> to Open Api v3.1. |
74 | | - /// </summary> |
75 | | - public override void SerializeAsV31(IOpenApiWriter writer) |
76 | | - { |
77 | | - Utils.CheckArgumentNull(writer); |
| 23 | + /// <summary> |
| 24 | + /// A title which by default SHOULD override that of the referenced component. |
| 25 | + /// If the referenced object-type does not allow a title field, then this field has no effect. |
| 26 | + /// </summary> |
| 27 | + public string? Title { get; set; } |
78 | 28 |
|
79 | | - if (Type == ReferenceType.Tag && !string.IsNullOrEmpty(ReferenceV3) && ReferenceV3 is not null) |
80 | | - { |
81 | | - // Write the string value only |
82 | | - writer.WriteValue(ReferenceV3); |
83 | | - return; |
84 | | - } |
| 29 | + /// <summary> |
| 30 | + /// Indicates whether the referenced component is deprecated. |
| 31 | + /// If the referenced object-type does not allow a deprecated field, then this field has no effect. |
| 32 | + /// </summary> |
| 33 | + public bool? Deprecated { get; set; } |
85 | 34 |
|
86 | | - writer.WriteStartObject(); |
87 | | - |
88 | | - // summary and description are in 3.1 but not in 3.0 |
89 | | - writer.WriteProperty(OpenApiConstants.Summary, Summary); |
90 | | - writer.WriteProperty(OpenApiConstants.Description, Description); |
91 | | - |
92 | | - // Additional schema metadata annotations in 3.1 |
93 | | - writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d)); |
94 | | - writer.WriteProperty(OpenApiConstants.Title, Title); |
95 | | - if (Deprecated.HasValue) |
96 | | - { |
97 | | - writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated.Value, false); |
98 | | - } |
99 | | - if (ReadOnly.HasValue) |
100 | | - { |
101 | | - writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly.Value, false); |
102 | | - } |
103 | | - if (WriteOnly.HasValue) |
104 | | - { |
105 | | - writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly.Value, false); |
106 | | - } |
107 | | - if (Examples != null && Examples.Any()) |
108 | | - { |
109 | | - writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (w, e) => w.WriteAny(e)); |
110 | | - } |
| 35 | + /// <summary> |
| 36 | + /// Indicates whether the referenced component is read-only. |
| 37 | + /// If the referenced object-type does not allow a readOnly field, then this field has no effect. |
| 38 | + /// </summary> |
| 39 | + public bool? ReadOnly { get; set; } |
111 | 40 |
|
112 | | - // $ref |
113 | | - writer.WriteProperty(OpenApiConstants.DollarRef, ReferenceV3); |
| 41 | + /// <summary> |
| 42 | + /// Indicates whether the referenced component is write-only. |
| 43 | + /// If the referenced object-type does not allow a writeOnly field, then this field has no effect. |
| 44 | + /// </summary> |
| 45 | + public bool? WriteOnly { get; set; } |
114 | 46 |
|
115 | | - writer.WriteEndObject(); |
116 | | - } |
| 47 | + /// <summary> |
| 48 | + /// Example values which by default SHOULD override those of the referenced component. |
| 49 | + /// If the referenced object-type does not allow examples, then this field has no effect. |
| 50 | + /// </summary> |
| 51 | + public IList<JsonNode>? Examples { get; set; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Parameterless constructor |
| 55 | + /// </summary> |
| 56 | + public JsonSchemaReference() { } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Initializes a copy instance of the <see cref="JsonSchemaReference"/> object |
| 60 | + /// </summary> |
| 61 | + public JsonSchemaReference(JsonSchemaReference reference) : base(reference) |
| 62 | + { |
| 63 | + Utils.CheckArgumentNull(reference); |
| 64 | + Default = reference.Default; |
| 65 | + Title = reference.Title; |
| 66 | + Deprecated = reference.Deprecated; |
| 67 | + ReadOnly = reference.ReadOnly; |
| 68 | + WriteOnly = reference.WriteOnly; |
| 69 | + Examples = reference.Examples; |
| 70 | + } |
117 | 71 |
|
118 | | - /// <summary> |
119 | | - /// Sets metadata fields from a JSON node during parsing |
120 | | - /// </summary> |
121 | | - internal override void SetMetadataFromMapNode(MapNode mapNode) |
| 72 | + /// <inheritdoc/> |
| 73 | + protected override void SerializeAdditionalV31Properties(IOpenApiWriter writer) |
| 74 | + { |
| 75 | + if (Type != ReferenceType.Schema) throw new InvalidOperationException( |
| 76 | + $"JsonSchemaReference can only be serialized for ReferenceType.Schema, but was {Type}."); |
| 77 | + |
| 78 | + base.SerializeAdditionalV31Properties(writer); |
| 79 | + // Additional schema metadata annotations in 3.1 |
| 80 | + writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d)); |
| 81 | + writer.WriteProperty(OpenApiConstants.Title, Title); |
| 82 | + if (Deprecated.HasValue) |
| 83 | + { |
| 84 | + writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated.Value, false); |
| 85 | + } |
| 86 | + if (ReadOnly.HasValue) |
| 87 | + { |
| 88 | + writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly.Value, false); |
| 89 | + } |
| 90 | + if (WriteOnly.HasValue) |
122 | 91 | { |
123 | | - base.SetMetadataFromMapNode(mapNode); |
124 | | - |
125 | | - if (mapNode.JsonNode is not JsonObject jsonObject) return; |
| 92 | + writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly.Value, false); |
| 93 | + } |
| 94 | + if (Examples != null && Examples.Any()) |
| 95 | + { |
| 96 | + writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (w, e) => w.WriteAny(e)); |
| 97 | + } |
| 98 | + } |
126 | 99 |
|
127 | | - var title = GetPropertyValueFromNode(jsonObject, OpenApiConstants.Title); |
128 | | - if (!string.IsNullOrEmpty(title)) |
129 | | - { |
130 | | - Title = title; |
131 | | - } |
| 100 | + /// <inheritdoc/> |
| 101 | + protected override void SetAdditional31MetadataFromMapNode(JsonObject jsonObject) |
| 102 | + { |
| 103 | + base.SetAdditional31MetadataFromMapNode(jsonObject); |
132 | 104 |
|
133 | | - // Boolean properties |
134 | | - if (jsonObject.TryGetPropertyValue(OpenApiConstants.Deprecated, out var deprecatedNode) && deprecatedNode is JsonValue deprecatedValue && deprecatedValue.TryGetValue<bool>(out var deprecated)) |
135 | | - { |
136 | | - Deprecated = deprecated; |
137 | | - } |
| 105 | + var title = GetPropertyValueFromNode(jsonObject, OpenApiConstants.Title); |
| 106 | + if (!string.IsNullOrEmpty(title)) |
| 107 | + { |
| 108 | + Title = title; |
| 109 | + } |
138 | 110 |
|
139 | | - if (jsonObject.TryGetPropertyValue(OpenApiConstants.ReadOnly, out var readOnlyNode) && readOnlyNode is JsonValue readOnlyValue && readOnlyValue.TryGetValue<bool>(out var readOnly)) |
140 | | - { |
141 | | - ReadOnly = readOnly; |
142 | | - } |
| 111 | + // Boolean properties |
| 112 | + if (jsonObject.TryGetPropertyValue(OpenApiConstants.Deprecated, out var deprecatedNode) && deprecatedNode is JsonValue deprecatedValue && deprecatedValue.TryGetValue<bool>(out var deprecated)) |
| 113 | + { |
| 114 | + Deprecated = deprecated; |
| 115 | + } |
143 | 116 |
|
144 | | - if (jsonObject.TryGetPropertyValue(OpenApiConstants.WriteOnly, out var writeOnlyNode) && writeOnlyNode is JsonValue writeOnlyValue && writeOnlyValue.TryGetValue<bool>(out var writeOnly)) |
145 | | - { |
146 | | - WriteOnly = writeOnly; |
147 | | - } |
| 117 | + if (jsonObject.TryGetPropertyValue(OpenApiConstants.ReadOnly, out var readOnlyNode) && readOnlyNode is JsonValue readOnlyValue && readOnlyValue.TryGetValue<bool>(out var readOnly)) |
| 118 | + { |
| 119 | + ReadOnly = readOnly; |
| 120 | + } |
148 | 121 |
|
149 | | - // Default value |
150 | | - if (jsonObject.TryGetPropertyValue(OpenApiConstants.Default, out var defaultNode)) |
151 | | - { |
152 | | - Default = defaultNode; |
153 | | - } |
| 122 | + if (jsonObject.TryGetPropertyValue(OpenApiConstants.WriteOnly, out var writeOnlyNode) && writeOnlyNode is JsonValue writeOnlyValue && writeOnlyValue.TryGetValue<bool>(out var writeOnly)) |
| 123 | + { |
| 124 | + WriteOnly = writeOnly; |
| 125 | + } |
| 126 | + |
| 127 | + // Default value |
| 128 | + if (jsonObject.TryGetPropertyValue(OpenApiConstants.Default, out var defaultNode)) |
| 129 | + { |
| 130 | + Default = defaultNode; |
| 131 | + } |
154 | 132 |
|
155 | | - // Examples |
156 | | - if (jsonObject.TryGetPropertyValue(OpenApiConstants.Examples, out var examplesNode) && examplesNode is JsonArray examplesArray) |
| 133 | + // Examples |
| 134 | + if (jsonObject.TryGetPropertyValue(OpenApiConstants.Examples, out var examplesNode) && examplesNode is JsonArray examplesArray) |
| 135 | + { |
| 136 | + Examples = new List<JsonNode>(); |
| 137 | + foreach (var example in examplesArray) |
157 | 138 | { |
158 | | - Examples = new List<JsonNode>(); |
159 | | - foreach (var example in examplesArray) |
| 139 | + if (example != null) |
160 | 140 | { |
161 | | - if (example != null) |
162 | | - { |
163 | | - Examples.Add(example); |
164 | | - } |
| 141 | + Examples.Add(example); |
165 | 142 | } |
166 | 143 | } |
167 | 144 | } |
168 | | - |
169 | | - private static string? GetPropertyValueFromNode(JsonObject jsonObject, string key) => |
170 | | - jsonObject.TryGetPropertyValue(key, out var valueNode) && valueNode is JsonValue valueCast && valueCast.TryGetValue<string>(out var strValue) ? strValue : null; |
171 | 145 | } |
172 | 146 | } |
0 commit comments