-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathOpenApiReference.cs
279 lines (235 loc) · 8.98 KB
/
OpenApiReference.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
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
{
/// <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; set; }
/// <summary>
/// The element type referenced.
/// </summary>
/// <remarks>This must be present if <see cref="ExternalResource"/> is not present.</remarks>
public ReferenceType? Type { get; set; }
/// <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; set; }
/// <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 = false;
/// <summary>
/// The OpenApiDocument that is hosting the OpenApiReference instance. This is used to enable dereferencing the reference.
/// </summary>
public OpenApiDocument HostDocument { get; set; }
/// <summary>
/// Gets the full reference string for v3.0.
/// </summary>
public string ReferenceV3
{
get
{
if (IsExternal)
{
return GetExternalReferenceV3();
}
if (!Type.HasValue)
{
throw new ArgumentNullException(nameof(Type));
}
if (Type == ReferenceType.Tag)
{
return Id;
}
if (Type == ReferenceType.SecurityScheme)
{
return Id;
}
if (Id.StartsWith("http"))
{
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.HasValue)
{
throw new ArgumentNullException(nameof(Type));
}
if (Type == ReferenceType.Tag)
{
return Id;
}
if (Type == ReferenceType.SecurityScheme)
{
return Id;
}
return "#/" + GetReferenceTypeNameAsV2(Type.Value) + "/" + Id;
}
}
/// <summary>
/// Parameterless constructor
/// </summary>
public OpenApiReference() { }
/// <summary>
/// Initializes a copy instance of the <see cref="OpenApiReference"/> object
/// </summary>
public OpenApiReference(OpenApiReference reference)
{
Summary = reference?.Summary;
Description = reference?.Description;
ExternalResource = reference?.ExternalResource;
Type = reference?.Type;
Id = reference?.Id;
HostDocument = new(reference?.HostDocument);
}
/// <summary>
/// Serialize <see cref="OpenApiReference"/> to Open Api v3.1.
/// </summary>
public void SerializeAsV31(IOpenApiWriter writer)
{
// summary and description are in 3.1 but not in 3.0
writer.WriteProperty(OpenApiConstants.Summary, Summary);
writer.WriteProperty(OpenApiConstants.Description, Description);
SerializeInternal(writer);
}
/// <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)
{
Utils.CheckArgumentNull(writer);
if (Type == ReferenceType.Tag)
{
// Write the string value only
writer.WriteValue(ReferenceV3);
return;
}
if (Type == ReferenceType.SecurityScheme)
{
// Write the string as property name
writer.WritePropertyName(ReferenceV3);
return;
}
writer.WriteStartObject();
// $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)
{
// Write the string value only
writer.WriteValue(ReferenceV2);
return;
}
if (Type == ReferenceType.SecurityScheme)
{
// 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"))
{
return Id;
}
return ExternalResource + "#/components/" + Type.GetDisplayName() + "/" + Id;
}
return ExternalResource;
}
private string GetExternalReferenceV2()
{
if (Id != null)
{
return ExternalResource + "#/" + GetReferenceTypeNameAsV2((ReferenceType)Type) + "/" + Id;
}
return ExternalResource;
}
private 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.
};
}
}
}