-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathOpenApiComponents.cs
337 lines (301 loc) · 12.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
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
{
/// <summary>
/// An object to hold reusable <see cref="OpenApiSchema"/> Objects.
/// </summary>
public IDictionary<string, OpenApiSchema>? Schemas { get; set; } = new Dictionary<string, OpenApiSchema>();
/// <summary>
/// An object to hold reusable <see cref="OpenApiResponse"/> Objects.
/// </summary>
public IDictionary<string, OpenApiResponse>? Responses { get; set; } = new Dictionary<string, OpenApiResponse>();
/// <summary>
/// An object to hold reusable <see cref="IOpenApiParameter"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiParameter>? Parameters { get; set; } =
new Dictionary<string, IOpenApiParameter>();
/// <summary>
/// An object to hold reusable <see cref="OpenApiExample"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiExample>? Examples { get; set; } = new Dictionary<string, IOpenApiExample>();
/// <summary>
/// An object to hold reusable <see cref="OpenApiRequestBody"/> Objects.
/// </summary>
public IDictionary<string, OpenApiRequestBody>? RequestBodies { get; set; } =
new Dictionary<string, OpenApiRequestBody>();
/// <summary>
/// An object to hold reusable <see cref="IOpenApiHeader"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiHeader>? Headers { get; set; } = new Dictionary<string, IOpenApiHeader>();
/// <summary>
/// An object to hold reusable <see cref="OpenApiSecurityScheme"/> Objects.
/// </summary>
public IDictionary<string, OpenApiSecurityScheme>? SecuritySchemes { get; set; } =
new Dictionary<string, OpenApiSecurityScheme>();
/// <summary>
/// An object to hold reusable <see cref="IOpenApiLink"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiLink>? Links { get; set; } = new Dictionary<string, IOpenApiLink>();
/// <summary>
/// An object to hold reusable <see cref="OpenApiCallback"/> Objects.
/// </summary>
public IDictionary<string, IOpenApiCallback>? Callbacks { get; set; } = new Dictionary<string, IOpenApiCallback>();
/// <summary>
/// An object to hold reusable <see cref="IOpenApiPathItem"/> Object.
/// </summary>
public IDictionary<string, IOpenApiPathItem>? PathItems { get; set; } = new Dictionary<string, IOpenApiPathItem>();
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
/// <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, OpenApiSchema>(components.Schemas) : null;
Responses = components?.Responses != null ? new Dictionary<string, OpenApiResponse>(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, OpenApiRequestBody>(components.RequestBodies) : null;
Headers = components?.Headers != null ? new Dictionary<string, IOpenApiHeader>(components.Headers) : null;
SecuritySchemes = components?.SecuritySchemes != null ? new Dictionary<string, OpenApiSecurityScheme>(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));
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));
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)
{
var loops = writer.GetSettings().LoopDetector.Loops;
writer.WriteStartObject();
if (loops.TryGetValue(typeof(OpenApiSchema), out var schemas))
{
writer.WriteOptionalMap(OpenApiConstants.Schemas, Schemas, callback);
}
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.
}
}
}