-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathOpenApiComponents.cs
403 lines (367 loc) · 15.9 KB
/
OpenApiComponents.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Writers;
#nullable enable
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// Components Object.
/// </summary>
public class OpenApiComponents : IOpenApiSerializable, IOpenApiExtensible
{
private Lazy<IDictionary<string, IOpenApiSchema>>? _schemas = new(() => new Dictionary<string, IOpenApiSchema>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="IOpenApiSchema"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiSchema>? Schemas
{
get => _schemas?.Value;
set => _schemas = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiResponse>>? _responses = new(() => new Dictionary<string, IOpenApiResponse>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="IOpenApiResponse"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiResponse>? Responses
{
get => _responses?.Value;
set => _responses = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiParameter>>? _parameters = new(() => new Dictionary<string, IOpenApiParameter>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="IOpenApiParameter"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiParameter>? Parameters
{
get => _parameters?.Value;
set => _parameters = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiExample>>? _examples = new(() => new Dictionary<string, IOpenApiExample>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="OpenApiExample"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiExample>? Examples
{
get => _examples?.Value;
set => _examples = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiRequestBody>>? _requestBodies = new(() => new Dictionary<string, IOpenApiRequestBody>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="IOpenApiRequestBody"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiRequestBody>? RequestBodies
{
get => _requestBodies?.Value;
set => _requestBodies = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiHeader>>? _headers = new(() => new Dictionary<string, IOpenApiHeader>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="IOpenApiHeader"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiHeader>? Headers
{
get => _headers?.Value;
set => _headers = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiSecurityScheme>>? _securitySchemes = new(() => new Dictionary<string, IOpenApiSecurityScheme>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="IOpenApiSecurityScheme"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiSecurityScheme>? SecuritySchemes
{
get => _securitySchemes?.Value;
set => _securitySchemes = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiLink>>? _links = new(() => new Dictionary<string, IOpenApiLink>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="IOpenApiLink"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiLink>? Links
{
get => _links?.Value;
set => _links = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiCallback>>? _callbacks = new(() => new Dictionary<string, IOpenApiCallback>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="OpenApiCallback"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiCallback>? Callbacks
{
get => _callbacks?.Value;
set => _callbacks = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiPathItem>>? _pathItems = new(() => new Dictionary<string, IOpenApiPathItem>(StringComparer.Ordinal));
/// <summary>
/// An object to hold reusable <see cref="IOpenApiPathItem"/> Object.
/// </summary>
public IDictionary<string, IOpenApiPathItem>? PathItems
{
get => _pathItems?.Value;
set => _pathItems = value is null ? null : new(() => value);
}
private Lazy<IDictionary<string, IOpenApiExtension>>? _extensions = new(() => new Dictionary<string, IOpenApiExtension>(StringComparer.Ordinal));
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public IDictionary<string, IOpenApiExtension>? Extensions
{
get => _extensions?.Value;
set => _extensions = value is null ? null : new(() => value);
}
/// <summary>
/// Parameter-less constructor
/// </summary>
public OpenApiComponents() { }
/// <summary>
/// Initializes a copy of an <see cref="OpenApiComponents"/> object
/// </summary>
public OpenApiComponents(OpenApiComponents? components)
{
Schemas = components?.Schemas != null ? new Dictionary<string, IOpenApiSchema>(components.Schemas) : null;
Responses = components?.Responses != null ? new Dictionary<string, IOpenApiResponse>(components.Responses) : null;
Parameters = components?.Parameters != null ? new Dictionary<string, IOpenApiParameter>(components.Parameters) : null;
Examples = components?.Examples != null ? new Dictionary<string, IOpenApiExample>(components.Examples) : null;
RequestBodies = components?.RequestBodies != null ? new Dictionary<string, IOpenApiRequestBody>(components.RequestBodies) : null;
Headers = components?.Headers != null ? new Dictionary<string, IOpenApiHeader>(components.Headers) : null;
SecuritySchemes = components?.SecuritySchemes != null ? new Dictionary<string, IOpenApiSecurityScheme>(components.SecuritySchemes) : null;
Links = components?.Links != null ? new Dictionary<string, IOpenApiLink>(components.Links) : null;
Callbacks = components?.Callbacks != null ? new Dictionary<string, IOpenApiCallback>(components.Callbacks) : null;
PathItems = components?.PathItems != null ? new Dictionary<string, IOpenApiPathItem>(components.PathItems) : null;
Extensions = components?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(components.Extensions) : null;
}
/// <summary>
/// Serialize <see cref="OpenApiComponents"/> to Open API v3.1.
/// </summary>
/// <param name="writer"></param>
public void SerializeAsV31(IOpenApiWriter writer)
{
Utils.CheckArgumentNull(writer);
// If references have been inlined we don't need the to render the components section
// however if they have cycles, then we will need a component rendered
if (writer.GetSettings().InlineLocalReferences)
{
RenderComponents(writer, (writer, element) => element.SerializeAsV31(writer), OpenApiSpecVersion.OpenApi3_1);
return;
}
writer.WriteStartObject();
// pathItems - only present in v3.1
writer.WriteOptionalMap(
OpenApiConstants.PathItems,
PathItems,
(w, key, component) =>
{
if (component is OpenApiPathItemReference reference)
{
reference.SerializeAsV31(w);
}
else
{
component.SerializeAsV31(w);
}
});
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_1, (writer, element) => element.SerializeAsV31(writer),
(writer, referenceElement) => referenceElement.SerializeAsV31(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiComponents"/> to v3.0
/// </summary>
/// <param name="writer"></param>
public void SerializeAsV3(IOpenApiWriter writer)
{
Utils.CheckArgumentNull(writer);
// If references have been inlined we don't need the to render the components section
// however if they have cycles, then we will need a component rendered
if (writer.GetSettings().InlineLocalReferences)
{
RenderComponents(writer, (writer, element) => element.SerializeAsV3(writer), OpenApiSpecVersion.OpenApi3_0);
return;
}
writer.WriteStartObject();
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer),
(writer, referenceElement) => referenceElement.SerializeAsV3(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiComponents"/>.
/// </summary>
private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback, Action<IOpenApiWriter, IOpenApiReferenceHolder> action)
{
// Serialize each referenceable object as full object without reference if the reference in the object points to itself.
// If the reference exists but points to other objects, the object is serialized to just that reference.
// schemas
writer.WriteOptionalMap(
OpenApiConstants.Schemas,
Schemas,
(w, key, component) =>
{
if (component is OpenApiSchemaReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// responses
writer.WriteOptionalMap(
OpenApiConstants.Responses,
Responses,
(w, key, component) =>
{
if (component is OpenApiResponseReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// parameters
writer.WriteOptionalMap(
OpenApiConstants.Parameters,
Parameters,
(w, key, component) =>
{
if (component is OpenApiParameterReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// examples
writer.WriteOptionalMap(
OpenApiConstants.Examples,
Examples,
(w, key, component) =>
{
if (component is OpenApiExampleReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// requestBodies
writer.WriteOptionalMap(
OpenApiConstants.RequestBodies,
RequestBodies,
(w, key, component) =>
{
if (component is OpenApiRequestBodyReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// headers
writer.WriteOptionalMap(
OpenApiConstants.Headers,
Headers,
(w, key, component) =>
{
if (component is OpenApiHeaderReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// securitySchemes
writer.WriteOptionalMap(
OpenApiConstants.SecuritySchemes,
SecuritySchemes,
(w, key, component) =>
{
if (component is OpenApiSecuritySchemeReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// links
writer.WriteOptionalMap(
OpenApiConstants.Links,
Links,
(w, key, component) =>
{
if (component is OpenApiLinkReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// callbacks
writer.WriteOptionalMap(
OpenApiConstants.Callbacks,
Callbacks,
(w, key, component) =>
{
if (component is OpenApiCallbackReference reference)
{
action(w, reference);
}
else
{
callback(w, component);
}
});
// extensions
writer.WriteExtensions(Extensions, version);
writer.WriteEndObject();
}
private void RenderComponents(IOpenApiWriter writer, Action<IOpenApiWriter, IOpenApiSerializable> callback, OpenApiSpecVersion version)
{
var loops = writer.GetSettings().LoopDetector.Loops;
writer.WriteStartObject();
if (loops.TryGetValue(typeof(OpenApiSchema), out var schemas))
{
writer.WriteOptionalMap(OpenApiConstants.Schemas, Schemas, callback);
}
// always render security schemes as inlining of security requirement objects is not allowed in the spec
if (SecuritySchemes is not null && SecuritySchemes.Any())
{
writer.WriteOptionalMap(
OpenApiConstants.SecuritySchemes,
SecuritySchemes,
(w, key, component) =>
{
if (version is OpenApiSpecVersion.OpenApi3_1)
component.SerializeAsV31(writer);
component.SerializeAsV3(writer);
});
}
writer.WriteEndObject();
}
/// <summary>
/// Serialize <see cref="OpenApiComponents"/> to Open Api v2.0.
/// </summary>
public void SerializeAsV2(IOpenApiWriter writer)
{
// Components object does not exist in V2.
}
}
}