Repository navigation
Expand file tree
/
Copy pathAsyncApiSchema.cs
More file actions
474 lines (374 loc) · 19.5 KB
/
Copy pathAsyncApiSchema.cs
File metadata and controls
474 lines (374 loc) · 19.5 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;
/// <summary>
/// The Schema Object allows the definition of input and output data types.
/// </summary>
public class AsyncApiSchema : IAsyncApiReferenceable, IAsyncApiExtensible, IAsyncApiSerializable
{
/// <summary>
/// follow JSON Schema definition. Short text providing information about the data.
/// </summary>
public string Title { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public SchemaType? Type { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public string Format { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// CommonMark syntax MAY be used for rich text representation.
/// </summary>
public string Description { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public double? Maximum { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public bool? ExclusiveMaximum { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public double? Minimum { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public bool? ExclusiveMinimum { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public int? MaxLength { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public int? MinLength { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// This string SHOULD be a valid regular expression, according to the ECMA 262 regular expression dialect.
/// </summary>
public string Pattern { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public double? MultipleOf { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// The default value represents what would be assumed by the consumer of the input as the value of the schema if one is not provided.
/// Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level.
/// For example, if type is string, then default can be "foo" but cannot be 1.
/// </summary>
public AsyncApiAny Default { get; set; }
/// <summary>
/// a value indicating whether relevant only for Schema "properties" definitions. Declares the property as "read only".
/// This means that it MAY be sent as part of a response but SHOULD NOT be sent as part of the request.
/// If the property is marked as readOnly being true and is in the required list,
/// the required will take effect on the response only.
/// A property MUST NOT be marked as both readOnly and writeOnly being true.
/// Default value is false.
/// </summary>
public bool ReadOnly { get; set; }
/// <summary>
/// a value indicating whether relevant only for Schema "properties" definitions. Declares the property as "write only".
/// Therefore, it MAY be sent as part of a request but SHOULD NOT be sent as part of the response.
/// If the property is marked as writeOnly being true and is in the required list,
/// the required will take effect on the request only.
/// A property MUST NOT be marked as both readOnly and writeOnly being true.
/// Default value is false.
/// </summary>
public bool WriteOnly { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public IList<AsyncApiSchema> AllOf { get; set; } = new List<AsyncApiSchema>();
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public IList<AsyncApiSchema> OneOf { get; set; } = new List<AsyncApiSchema>();
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public IList<AsyncApiSchema> AnyOf { get; set; } = new List<AsyncApiSchema>();
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public AsyncApiSchema Not { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public AsyncApiSchema Contains { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public AsyncApiSchema If { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public AsyncApiSchema Then { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public AsyncApiSchema Else { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public ISet<string> Required { get; set; } = new HashSet<string>();
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object
/// and not a standard JSON Schema. items MUST be present if the type is array.
/// </summary>
public AsyncApiSchema Items { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object
/// and not a standard JSON Schema. items MUST be present if the type is array.
/// </summary>
public AsyncApiSchema AdditionalItems { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public int? MaxItems { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public int? MinItems { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public bool? UniqueItems { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// Property definitions MUST be a Schema Object and not a standard JSON Schema (inline or referenced).
/// </summary>
public IDictionary<string, AsyncApiSchema> Properties { get; set; } = new Dictionary<string, AsyncApiSchema>();
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public int? MaxProperties { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public int? MinProperties { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
/// Value can be boolean or object. Inline or referenced schema
/// MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public AsyncApiSchema AdditionalProperties { get; set; }
public IDictionary<string, AsyncApiSchema> PatternProperties { get; set; } = new Dictionary<string, AsyncApiSchema>();
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public AsyncApiSchema PropertyNames { get; set; }
/// <summary>
/// adds support for polymorphism.
/// The discriminator is the schema property name that is used to differentiate between other schema that inherit this schema.
/// </summary>
public string Discriminator { get; set; }
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public IList<AsyncApiAny> Enum { get; set; } = new List<AsyncApiAny>();
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public IList<AsyncApiAny> Examples { get; set; } = new List<AsyncApiAny>();
/// <summary>
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
/// </summary>
public AsyncApiAny Const { get; set; }
/// <summary>
/// a value indicating whether allows sending a null value for the defined schema. Default value is false.
/// </summary>
public bool Nullable { get; set; }
/// <summary>
/// additional external documentation for this schema.
/// </summary>
public AsyncApiExternalDocumentation ExternalDocs { get; set; }
/// <summary>
/// a value indicating whether specifies that a schema is deprecated and SHOULD be transitioned out of usage.
/// Default value is false.
/// </summary>
public bool Deprecated { get; set; }
/// <inheritdoc/>
public bool UnresolvedReference { get; set; }
/// <inheritdoc/>
public AsyncApiReference Reference { get; set; }
public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
public void SerializeV2WithoutReference(IAsyncApiWriter writer)
{
writer.WriteStartObject();
// title
writer.WriteOptionalProperty(AsyncApiConstants.Title, this.Title);
// type
if (this.Type != null)
{
var types = EnumExtensions.GetFlags<SchemaType>(this.Type.Value);
if (types.Count() == 1)
{
writer.WriteOptionalProperty(AsyncApiConstants.Type, types.First().GetDisplayName());
}
else
{
writer.WriteOptionalCollection(AsyncApiConstants.Type, types.Select(t => t.GetDisplayName()), (w, s) => w.WriteValue(s));
}
}
// format
writer.WriteOptionalProperty(AsyncApiConstants.Format, this.Format);
// description
writer.WriteOptionalProperty(AsyncApiConstants.Description, this.Description);
// maximum
writer.WriteOptionalProperty(AsyncApiConstants.Maximum, this.Maximum);
// exclusiveMaximum
writer.WriteOptionalProperty(AsyncApiConstants.ExclusiveMaximum, this.ExclusiveMaximum);
// minimum
writer.WriteOptionalProperty(AsyncApiConstants.Minimum, this.Minimum);
// exclusiveMinimum
writer.WriteOptionalProperty(AsyncApiConstants.ExclusiveMinimum, this.ExclusiveMinimum);
// maxLength
writer.WriteOptionalProperty(AsyncApiConstants.MaxLength, this.MaxLength);
// minLength
writer.WriteOptionalProperty(AsyncApiConstants.MinLength, this.MinLength);
// pattern
writer.WriteOptionalProperty(AsyncApiConstants.Pattern, this.Pattern);
// multipleOf
writer.WriteOptionalProperty(AsyncApiConstants.MultipleOf, this.MultipleOf);
// default
writer.WriteOptionalObject(AsyncApiConstants.Default, this.Default, (w, d) => w.WriteAny(d));
// readOnly
writer.WriteOptionalProperty(AsyncApiConstants.ReadOnly, this.ReadOnly, false);
// writeOnly
writer.WriteOptionalProperty(AsyncApiConstants.WriteOnly, this.WriteOnly, false);
// allOf
writer.WriteOptionalCollection(AsyncApiConstants.AllOf, this.AllOf, (w, s) => s.SerializeV2(w));
// oneOf
writer.WriteOptionalCollection(AsyncApiConstants.OneOf, this.OneOf, (w, s) => s.SerializeV2(w));
// anyOf
writer.WriteOptionalCollection(AsyncApiConstants.AnyOf, this.AnyOf, (w, s) => s.SerializeV2(w));
// not
writer.WriteOptionalObject(AsyncApiConstants.Not, this.Not, (w, s) => s.SerializeV2(w));
// contains
writer.WriteOptionalObject(AsyncApiConstants.Contains, this.Contains, (w, s) => s.SerializeV2(w));
// anyOf
writer.WriteOptionalObject(AsyncApiConstants.If, this.If, (w, s) => s.SerializeV2(w));
// then
writer.WriteOptionalObject(AsyncApiConstants.Then, this.Then, (w, s) => s.SerializeV2(w));
// else
writer.WriteOptionalObject(AsyncApiConstants.Else, this.Else, (w, s) => s.SerializeV2(w));
// required
writer.WriteOptionalCollection(AsyncApiConstants.Required, this.Required, (w, s) => w.WriteValue(s));
// items
if (this.Items is FalseApiSchema)
{
writer.WriteOptionalProperty<bool>(AsyncApiConstants.Items, false);
}
else
{
writer.WriteOptionalObject(AsyncApiConstants.Items, this.Items, (w, s) => s.SerializeV2(w));
}
// additionalItems
if (this.AdditionalItems is FalseApiSchema)
{
writer.WriteOptionalProperty<bool>(AsyncApiConstants.AdditionalItems, false);
}
else
{
writer.WriteOptionalObject(AsyncApiConstants.AdditionalItems, this.AdditionalItems, (w, s) => s.SerializeV2(w));
}
// maxItems
writer.WriteOptionalProperty(AsyncApiConstants.MaxItems, this.MaxItems);
// minItems
writer.WriteOptionalProperty(AsyncApiConstants.MinItems, this.MinItems);
// uniqueItems
writer.WriteOptionalProperty(AsyncApiConstants.UniqueItems, this.UniqueItems);
// properties
writer.WriteOptionalMap(AsyncApiConstants.Properties, this.Properties, (w, s) => s.SerializeV2(w));
// maxProperties
writer.WriteOptionalProperty(AsyncApiConstants.MaxProperties, this.MaxProperties);
// minProperties
writer.WriteOptionalProperty(AsyncApiConstants.MinProperties, this.MinProperties);
// additionalProperties
if (this.AdditionalProperties is FalseApiSchema)
{
writer.WriteOptionalProperty<bool>(AsyncApiConstants.AdditionalProperties, false);
}
else
{
writer.WriteOptionalObject(AsyncApiConstants.AdditionalProperties, this.AdditionalProperties, (w, s) => s.SerializeV2(w));
}
writer.WriteOptionalMap(AsyncApiConstants.PatternProperties, this.PatternProperties, (w, s) => s.SerializeV2(w));
writer.WriteOptionalObject(AsyncApiConstants.PropertyNames, this.PropertyNames, (w, s) => s.SerializeV2(w));
// discriminator
writer.WriteOptionalProperty(AsyncApiConstants.Discriminator, this.Discriminator);
// enum
writer.WriteOptionalCollection(AsyncApiConstants.Enum, this.Enum, (nodeWriter, s) => nodeWriter.WriteAny(s));
// example
writer.WriteOptionalCollection(AsyncApiConstants.Examples, this.Examples, (w, e) => w.WriteAny(e));
writer.WriteOptionalObject(AsyncApiConstants.Const, this.Const, (w, s) => w.WriteAny(s));
// nullable
writer.WriteOptionalProperty(AsyncApiConstants.Nullable, this.Nullable, false);
// externalDocs
writer.WriteOptionalObject(AsyncApiConstants.ExternalDocs, this.ExternalDocs, (w, s) => s.SerializeV2(w));
// deprecated
writer.WriteOptionalProperty(AsyncApiConstants.Deprecated, this.Deprecated, false);
// extensions
writer.WriteExtensions(this.Extensions);
writer.WriteEndObject();
}
public void SerializeV2(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
var target = this;
var settings = writer.GetSettings();
if (this.Reference != null)
{
if (!settings.ShouldInlineReference(this.Reference))
{
this.Reference.SerializeV2(writer);
return;
}
// If Loop is detected then just Serialize as a reference.
if (!settings.LoopDetector.PushLoop(this))
{
settings.LoopDetector.SaveLoop(this);
this.Reference.SerializeV2(writer);
return;
}
target = this.GetReferenced(this.Reference.HostDocument);
}
target.SerializeV2WithoutReference(writer);
if (this.Reference != null)
{
settings.LoopDetector.PopLoop<AsyncApiSchema>();
}
}
public AsyncApiSchema GetReferenced(AsyncApiDocument document)
{
if (this.Reference != null && document != null)
{
return document.ResolveReference<AsyncApiSchema>(this.Reference);
}
else
{
return this;
}
}
}
}