-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiReference.cs
More file actions
303 lines (262 loc) · 10.9 KB
/
OpenApiReference.cs
File metadata and controls
303 lines (262 loc) · 10.9 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Reader.ParseNodes;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// A simple object to allow referencing other components in the specification, internally and externally.
/// </summary>
public class OpenApiReference : IOpenApiSerializable, IOpenApiDescribedElement, IOpenApiSummarizedElement
{
/// <summary>
/// A short summary which by default SHOULD override that of the referenced component.
/// If the referenced object-type does not allow a summary field, then this field has no effect.
/// </summary>
public string? Summary { get; set; }
/// <summary>
/// A description which by default SHOULD override that of the referenced component.
/// CommonMark syntax MAY be used for rich text representation.
/// If the referenced object-type does not allow a description field, then this field has no effect.
/// </summary>
public string? Description { get; set; }
/// <summary>
/// External resource in the reference.
/// It maybe:
/// 1. a absolute/relative file path, for example: ../commons/pet.json
/// 2. a Url, for example: http://localhost/pet.json
/// </summary>
public string? ExternalResource { get; init; }
/// <summary>
/// The element type referenced.
/// </summary>
/// <remarks>This must be present if <see cref="ExternalResource"/> is not present.</remarks>
public ReferenceType Type { get; init; }
/// <summary>
/// The identifier of the reusable component of one particular ReferenceType.
/// If ExternalResource is present, this is the path to the component after the '#/'.
/// For example, if the reference is 'example.json#/path/to/component', the Id is 'path/to/component'.
/// If ExternalResource is not present, this is the name of the component without the reference type name.
/// For example, if the reference is '#/components/schemas/componentName', the Id is 'componentName'.
/// </summary>
public string? Id { get; init; }
/// <summary>
/// Gets a flag indicating whether this reference is an external reference.
/// </summary>
public bool IsExternal => ExternalResource != null;
/// <summary>
/// Gets a flag indicating whether this reference is a local reference.
/// </summary>
public bool IsLocal => ExternalResource == null;
/// <summary>
/// Gets a flag indicating whether a file is a valid OpenAPI document or a fragment
/// </summary>
public bool IsFragment { get; init; }
private OpenApiDocument? hostDocument;
/// <summary>
/// The OpenApiDocument that is hosting the OpenApiReference instance. This is used to enable dereferencing the reference.
/// </summary>
public OpenApiDocument? HostDocument { get => hostDocument; init => hostDocument = value; }
/// <summary>
/// Gets the full reference string for v3.0.
/// </summary>
public string? ReferenceV3
{
get
{
if (IsExternal)
{
return GetExternalReferenceV3();
}
if (Type == ReferenceType.Tag)
{
return Id;
}
if (Type == ReferenceType.SecurityScheme)
{
return Id;
}
if (!string.IsNullOrEmpty(Id) && Id is not null && Id.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
!string.IsNullOrEmpty(Id) && Id is not null && Id.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return Id;
}
return "#/components/" + Type.GetDisplayName() + "/" + Id;
}
}
/// <summary>
/// Gets the full reference string for V2.0
/// </summary>
public string? ReferenceV2
{
get
{
if (IsExternal)
{
return GetExternalReferenceV2();
}
if (Type == ReferenceType.Tag)
{
return Id;
}
if (Type == ReferenceType.SecurityScheme)
{
return Id;
}
return "#/" + GetReferenceTypeNameAsV2(Type) + "/" + Id;
}
}
/// <summary>
/// Parameterless constructor
/// </summary>
public OpenApiReference() { }
/// <summary>
/// Initializes a copy instance of the <see cref="OpenApiReference"/> object
/// </summary>
public OpenApiReference(OpenApiReference reference)
{
Utils.CheckArgumentNull(reference);
Summary = reference.Summary;
Description = reference.Description;
ExternalResource = reference.ExternalResource;
Type = reference.Type;
Id = reference.Id;
HostDocument = reference.HostDocument;
}
/// <summary>
/// Serialize <see cref="OpenApiReference"/> to Open Api v3.1.
/// </summary>
public void SerializeAsV31(IOpenApiWriter writer)
{
SerializeInternal(writer, w =>
{
// summary and description are in 3.1 but not in 3.0
w.WriteProperty(OpenApiConstants.Summary, Summary);
w.WriteProperty(OpenApiConstants.Description, Description);
});
}
/// <summary>
/// Serialize <see cref="OpenApiReference"/> to Open Api v3.0.
/// </summary>
public void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer);
}
/// <summary>
/// Serialize <see cref="OpenApiReference"/>
/// </summary>
private void SerializeInternal(IOpenApiWriter writer, Action<IOpenApiWriter>? callback = null)
{
Utils.CheckArgumentNull(writer);
if (Type == ReferenceType.Tag && !string.IsNullOrEmpty(ReferenceV3) && ReferenceV3 is not null)
{
// Write the string value only
writer.WriteValue(ReferenceV3);
return;
}
writer.WriteStartObject();
if (callback is not null)
{
callback(writer);
}
// $ref
writer.WriteProperty(OpenApiConstants.DollarRef, ReferenceV3);
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiReference"/> to Open Api v2.0.
/// </summary>
public void SerializeAsV2(IOpenApiWriter writer)
{
Utils.CheckArgumentNull(writer);
if (Type == ReferenceType.Tag && !string.IsNullOrEmpty(ReferenceV2) && ReferenceV2 is not null)
{
// Write the string value only
writer.WriteValue(ReferenceV2);
return;
}
if (Type == ReferenceType.SecurityScheme && !string.IsNullOrEmpty(ReferenceV2) && ReferenceV2 is not null)
{
// Write the string as property name
writer.WritePropertyName(ReferenceV2);
return;
}
writer.WriteStartObject();
// $ref
writer.WriteProperty(OpenApiConstants.DollarRef, ReferenceV2);
writer.WriteEndObject();
}
private string? GetExternalReferenceV3()
{
if (Id != null)
{
if (IsFragment)
{
return ExternalResource + "#" + Id;
}
if (Id.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
Id.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return Id;
}
return ExternalResource + "#/components/" + Type.GetDisplayName() + "/"+ Id;
}
return ExternalResource;
}
private string? GetExternalReferenceV2()
{
if (Id is not null)
{
return ExternalResource + "#/" + GetReferenceTypeNameAsV2(Type) + "/" + Id;
}
return ExternalResource;
}
private static string? GetReferenceTypeNameAsV2(ReferenceType type)
{
return type switch
{
ReferenceType.Schema => OpenApiConstants.Definitions,
ReferenceType.Parameter or ReferenceType.RequestBody => OpenApiConstants.Parameters,
ReferenceType.Response => OpenApiConstants.Responses,
ReferenceType.Header => OpenApiConstants.Headers,
ReferenceType.Tag => OpenApiConstants.Tags,
ReferenceType.SecurityScheme => OpenApiConstants.SecurityDefinitions,
_ => null,// If the reference type is not supported in V2, simply return null
// to indicate that the reference is not pointing to any object.
};
}
/// <summary>
/// Sets the host document after deserialization or before serialization.
/// This method is internal on purpose to avoid consumers mutating the host document.
/// </summary>
/// <param name="currentDocument">Host document to set if none is present</param>
internal void EnsureHostDocumentIsSet(OpenApiDocument currentDocument)
{
Utils.CheckArgumentNull(currentDocument);
hostDocument ??= currentDocument;
}
private static string? GetPropertyValueFromNode(JsonObject jsonObject, string key) =>
jsonObject.TryGetPropertyValue(key, out var valueNode) && valueNode is JsonValue valueCast && valueCast.TryGetValue<string>(out var strValue) ? strValue : null;
internal void SetSummaryAndDescriptionFromMapNode(MapNode mapNode)
{
var (description, summary) = mapNode.JsonNode switch {
JsonObject jsonObject => (GetPropertyValueFromNode(jsonObject, OpenApiConstants.Description),
GetPropertyValueFromNode(jsonObject, OpenApiConstants.Summary)),
_ => (null, null)
};
if (!string.IsNullOrEmpty(description))
{
Description = description;
}
if (!string.IsNullOrEmpty(summary))
{
Summary = summary;
}
}
}
}