-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathOpenApiSchema.cs
898 lines (732 loc) · 38.9 KB
/
OpenApiSchema.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Helpers;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Models
{
/// <summary>
/// The Schema Object allows the definition of input and output data types.
/// </summary>
public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiReferenceable, IOpenApiSerializable
{
private JsonNode _example;
private JsonNode _default;
private IList<JsonNode> _examples;
/// <summary>
/// Follow JSON Schema definition. Short text providing information about the data.
/// </summary>
public virtual string Title { get; set; }
/// <summary>
/// $schema, a JSON Schema dialect identifier. Value must be a URI
/// </summary>
public virtual string Schema { get; set; }
/// <summary>
/// $id - Identifies a schema resource with its canonical URI.
/// </summary>
public virtual string Id { get; set; }
/// <summary>
/// $comment - reserves a location for comments from schema authors to readers or maintainers of the schema.
/// </summary>
public virtual string Comment { get; set; }
/// <summary>
/// $vocabulary- used in meta-schemas to identify the vocabularies available for use in schemas described by that meta-schema.
/// </summary>
public virtual string Vocabulary { get; set; }
/// <summary>
/// $dynamicRef - an applicator that allows for deferring the full resolution until runtime, at which point it is resolved each time it is encountered while evaluating an instance
/// </summary>
public virtual string DynamicRef { get; set; }
/// <summary>
/// $dynamicAnchor - used to create plain name fragments that are not tied to any particular structural location for referencing purposes, which are taken into consideration for dynamic referencing.
/// </summary>
public virtual string DynamicAnchor { get; set; }
/// <summary>
/// $recursiveAnchor - used to construct recursive schemas i.e one that has a reference to its own root, identified by the empty fragment URI reference ("#")
/// </summary>
public virtual string RecursiveAnchor { get; set; }
/// <summary>
/// $recursiveRef - used to construct recursive schemas i.e one that has a reference to its own root, identified by the empty fragment URI reference ("#")
/// </summary>
public virtual string RecursiveRef { get; set; }
/// <summary>
/// $defs - reserves a location for schema authors to inline re-usable JSON Schemas into a more general schema.
/// The keyword does not directly affect the validation result
/// </summary>
public virtual IDictionary<string, OpenApiSchema> Definitions { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual decimal? V31ExclusiveMaximum { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual decimal? V31ExclusiveMinimum { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual bool UnEvaluatedProperties { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// Value MUST be a string in V2 and V3.
/// </summary>
public virtual object Type { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// While relying on JSON Schema's defined formats,
/// the OAS offers a few additional predefined formats.
/// </summary>
public virtual 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 virtual string Description { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual decimal? Maximum { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual bool? ExclusiveMaximum { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual decimal? Minimum { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual bool? ExclusiveMinimum { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual int? MaxLength { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual int? MinLength { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// This string SHOULD be a valid regular expression, according to the ECMA 262 regular expression dialect
/// </summary>
public virtual string Pattern { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual decimal? 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 virtual JsonNode Default
{
get => _default;
set => _default = value;
}
/// <summary>
/// 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 virtual bool ReadOnly { get; set; }
/// <summary>
/// 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 virtual bool WriteOnly { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public virtual IList<OpenApiSchema> AllOf { get; set; } = new List<OpenApiSchema>();
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public virtual IList<OpenApiSchema> OneOf { get; set; } = new List<OpenApiSchema>();
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public virtual IList<OpenApiSchema> AnyOf { get; set; } = new List<OpenApiSchema>();
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public virtual OpenApiSchema Not { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual ISet<string> Required { get; set; } = new HashSet<string>();
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// 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 virtual OpenApiSchema Items { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual int? MaxItems { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual int? MinItems { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual bool? UniqueItems { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// Property definitions MUST be a Schema Object and not a standard JSON Schema (inline or referenced).
/// </summary>
public virtual IDictionary<string, OpenApiSchema> Properties { get; set; } = new Dictionary<string, OpenApiSchema>();
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// PatternProperty definitions MUST be a Schema Object and not a standard JSON Schema (inline or referenced)
/// Each property name of this object SHOULD be a valid regular expression according to the ECMA 262 r
/// egular expression dialect. Each property value of this object MUST be an object, and each object MUST
/// be a valid Schema Object not a standard JSON Schema.
/// </summary>
public virtual IDictionary<string, OpenApiSchema> PatternProperties { get; set; } = new Dictionary<string, OpenApiSchema>();
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual int? MaxProperties { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual int? MinProperties { get; set; }
/// <summary>
/// Indicates if the schema can contain properties other than those defined by the properties map.
/// </summary>
public virtual bool AdditionalPropertiesAllowed { get; set; } = true;
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// Value can be boolean or object. Inline or referenced schema
/// MUST be of a Schema Object and not a standard JSON Schema.
/// </summary>
public virtual OpenApiSchema AdditionalProperties { get; set; }
/// <summary>
/// Adds support for polymorphism. The discriminator is an object name that is used to differentiate
/// between other schemas which may satisfy the payload description.
/// </summary>
public virtual OpenApiDiscriminator Discriminator { get; set; }
/// <summary>
/// A free-form property to include an example of an instance for this schema.
/// To represent examples that cannot be naturally represented in JSON or YAML,
/// a string value can be used to contain the example with escaping where necessary.
/// </summary>
public virtual JsonNode Example
{
get => _example;
set => _example = value;
}
/// <summary>
/// A free-form property to include examples of an instance for this schema.
/// To represent examples that cannot be naturally represented in JSON or YAML,
/// a list of values can be used to contain the examples with escaping where necessary.
/// </summary>
public virtual IList<JsonNode> Examples
{
get => _examples;
set => _examples = value;
}
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual IList<JsonNode> Enum { get; set; } = new List<JsonNode>();
/// <summary>
/// Allows sending a null value for the defined schema. Default value is false.
/// </summary>
public virtual bool Nullable { get; set; }
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public virtual bool UnevaluatedProperties { get; set;}
/// <summary>
/// Additional external documentation for this schema.
/// </summary>
public virtual OpenApiExternalDocs ExternalDocs { get; set; }
/// <summary>
/// Specifies that a schema is deprecated and SHOULD be transitioned out of usage.
/// Default value is false.
/// </summary>
public virtual bool Deprecated { get; set; }
/// <summary>
/// This MAY be used only on properties schemas. It has no effect on root schemas.
/// Adds additional metadata to describe the XML representation of this property.
/// </summary>
public virtual OpenApiXml Xml { get; set; }
/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public virtual IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
/// <summary>
/// Indicates object is a placeholder reference to an actual object and does not contain valid data.
/// </summary>
public virtual bool UnresolvedReference { get; set; }
/// <summary>
/// Reference object.
/// </summary>
public virtual OpenApiReference Reference { get; set; }
/// <inheritdoc />
public IDictionary<string, object> Annotations { get; set; }
/// <summary>
/// Parameterless constructor
/// </summary>
public OpenApiSchema() { }
/// <summary>
/// Initializes a copy of <see cref="OpenApiSchema"/> object
/// </summary>
public OpenApiSchema(OpenApiSchema schema)
{
Title = schema?.Title ?? Title;
Id = schema?.Id ?? Id;
Schema = schema?.Schema ?? Schema;
Comment = schema?.Comment ?? Comment;
Vocabulary = schema?.Vocabulary ?? Vocabulary;
DynamicAnchor = schema?.DynamicAnchor ?? DynamicAnchor;
DynamicRef = schema?.DynamicRef ?? DynamicRef;
RecursiveAnchor = schema?.RecursiveAnchor ?? RecursiveAnchor;
RecursiveRef = schema?.RecursiveRef ?? RecursiveRef;
Definitions = schema?.Definitions != null ? new Dictionary<string, OpenApiSchema>(schema.Definitions) : null;
UnevaluatedProperties = schema?.UnevaluatedProperties ?? UnevaluatedProperties;
V31ExclusiveMaximum = schema?.V31ExclusiveMaximum ?? V31ExclusiveMaximum;
V31ExclusiveMinimum = schema?.V31ExclusiveMinimum ?? V31ExclusiveMinimum;
Type = DeepCloneType(schema?.Type);
Format = schema?.Format ?? Format;
Description = schema?.Description ?? Description;
Maximum = schema?.Maximum ?? Maximum;
ExclusiveMaximum = schema?.ExclusiveMaximum ?? ExclusiveMaximum;
Minimum = schema?.Minimum ?? Minimum;
ExclusiveMinimum = schema?.ExclusiveMinimum ?? ExclusiveMinimum;
MaxLength = schema?.MaxLength ?? MaxLength;
MinLength = schema?.MinLength ?? MinLength;
Pattern = schema?.Pattern ?? Pattern;
MultipleOf = schema?.MultipleOf ?? MultipleOf;
_default = schema?.Default != null ? JsonNodeCloneHelper.Clone(schema?.Default) : null;
ReadOnly = schema?.ReadOnly ?? ReadOnly;
WriteOnly = schema?.WriteOnly ?? WriteOnly;
AllOf = schema?.AllOf != null ? new List<OpenApiSchema>(schema.AllOf) : null;
OneOf = schema?.OneOf != null ? new List<OpenApiSchema>(schema.OneOf) : null;
AnyOf = schema?.AnyOf != null ? new List<OpenApiSchema>(schema.AnyOf) : null;
Not = schema?.Not != null ? new(schema?.Not) : null;
Required = schema?.Required != null ? new HashSet<string>(schema.Required) : null;
Items = schema?.Items != null ? new(schema?.Items) : null;
MaxItems = schema?.MaxItems ?? MaxItems;
MinItems = schema?.MinItems ?? MinItems;
UniqueItems = schema?.UniqueItems ?? UniqueItems;
Properties = schema?.Properties != null ? new Dictionary<string, OpenApiSchema>(schema.Properties) : null;
PatternProperties = schema?.PatternProperties != null ? new Dictionary<string, OpenApiSchema>(schema.PatternProperties) : null;
MaxProperties = schema?.MaxProperties ?? MaxProperties;
MinProperties = schema?.MinProperties ?? MinProperties;
AdditionalPropertiesAllowed = schema?.AdditionalPropertiesAllowed ?? AdditionalPropertiesAllowed;
AdditionalProperties = schema?.AdditionalProperties != null ? new(schema?.AdditionalProperties) : null;
Discriminator = schema?.Discriminator != null ? new(schema?.Discriminator) : null;
_example = schema?.Example != null ? JsonNodeCloneHelper.Clone(schema?.Example) : null;
_examples = schema?.Examples != null ? new List<JsonNode>(schema.Examples) : null;
Enum = schema?.Enum != null ? new List<JsonNode>(schema.Enum) : null;
Nullable = schema?.Nullable ?? Nullable;
ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null;
Deprecated = schema?.Deprecated ?? Deprecated;
Xml = schema?.Xml != null ? new(schema?.Xml) : null;
Extensions = schema?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(schema.Extensions) : null;
UnresolvedReference = schema?.UnresolvedReference ?? UnresolvedReference;
Reference = schema?.Reference != null ? new(schema?.Reference) : null;
Annotations = schema?.Annotations != null ? new Dictionary<string, object>(schema?.Annotations) : null;
}
/// <summary>
/// Serialize <see cref="OpenApiParameter"/> to Open Api v3.1
/// </summary>
public virtual void SerializeAsV31(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_1, (writer, element) => element.SerializeAsV31(writer));
}
/// <summary>
/// Serialize <see cref="OpenApiParameter"/> to Open Api v3.0
/// </summary>
public virtual void SerializeAsV3(IOpenApiWriter writer)
{
SerializeInternal(writer, OpenApiSpecVersion.OpenApi3_0, (writer, element) => element.SerializeAsV3(writer));
}
/// <inheritdoc/>
public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
writer.WriteStartObject();
if (version == OpenApiSpecVersion.OpenApi3_1)
{
WriteV31Properties(writer);
}
// title
writer.WriteProperty(OpenApiConstants.Title, Title);
// multipleOf
writer.WriteProperty(OpenApiConstants.MultipleOf, MultipleOf);
// maximum
writer.WriteProperty(OpenApiConstants.Maximum, Maximum);
// exclusiveMaximum
writer.WriteProperty(OpenApiConstants.ExclusiveMaximum, ExclusiveMaximum);
// minimum
writer.WriteProperty(OpenApiConstants.Minimum, Minimum);
// exclusiveMinimum
writer.WriteProperty(OpenApiConstants.ExclusiveMinimum, ExclusiveMinimum);
// maxLength
writer.WriteProperty(OpenApiConstants.MaxLength, MaxLength);
// minLength
writer.WriteProperty(OpenApiConstants.MinLength, MinLength);
// pattern
writer.WriteProperty(OpenApiConstants.Pattern, Pattern);
// maxItems
writer.WriteProperty(OpenApiConstants.MaxItems, MaxItems);
// minItems
writer.WriteProperty(OpenApiConstants.MinItems, MinItems);
// uniqueItems
writer.WriteProperty(OpenApiConstants.UniqueItems, UniqueItems);
// maxProperties
writer.WriteProperty(OpenApiConstants.MaxProperties, MaxProperties);
// minProperties
writer.WriteProperty(OpenApiConstants.MinProperties, MinProperties);
// required
writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) => w.WriteValue(s));
// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(s));
// type
SerializeTypeProperty(Type, writer, version);
// allOf
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV3(w));
// anyOf
writer.WriteOptionalCollection(OpenApiConstants.AnyOf, AnyOf, (w, s) => s.SerializeAsV3(w));
// oneOf
writer.WriteOptionalCollection(OpenApiConstants.OneOf, OneOf, (w, s) => s.SerializeAsV3(w));
// not
writer.WriteOptionalObject(OpenApiConstants.Not, Not, (w, s) => s.SerializeAsV3(w));
// items
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV3(w));
// properties
writer.WriteOptionalMap(OpenApiConstants.Properties, Properties, (w, s) => s.SerializeAsV3(w));
// additionalProperties
if (AdditionalPropertiesAllowed)
{
writer.WriteOptionalObject(
OpenApiConstants.AdditionalProperties,
AdditionalProperties,
(w, s) => s.SerializeAsV3(w));
}
else
{
writer.WriteProperty(OpenApiConstants.AdditionalProperties, AdditionalPropertiesAllowed);
}
// description
writer.WriteProperty(OpenApiConstants.Description, Description);
// format
writer.WriteProperty(OpenApiConstants.Format, Format);
// default
writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d));
// nullable
if (version is OpenApiSpecVersion.OpenApi3_0)
{
writer.WriteProperty(OpenApiConstants.Nullable, Nullable, false);
}
// discriminator
writer.WriteOptionalObject(OpenApiConstants.Discriminator, Discriminator, (w, s) => s.SerializeAsV3(w));
// readOnly
writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly, false);
// writeOnly
writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly, false);
// xml
writer.WriteOptionalObject(OpenApiConstants.Xml, Xml, (w, s) => s.SerializeAsV2(w));
// externalDocs
writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, s) => s.SerializeAsV3(w));
// example
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e));
// deprecated
writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated, false);
// extensions
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi3_0);
writer.WriteEndObject();
}
/// <inheritdoc/>
public virtual void SerializeAsV2(IOpenApiWriter writer)
{
SerializeAsV2(writer: writer, parentRequiredProperties: new HashSet<string>(), propertyName: null);
}
internal void WriteV31Properties(IOpenApiWriter writer)
{
writer.WriteProperty(OpenApiConstants.Id, Id);
writer.WriteProperty(OpenApiConstants.DollarSchema, Schema);
writer.WriteProperty(OpenApiConstants.Comment, Comment);
writer.WriteProperty(OpenApiConstants.Vocabulary, Vocabulary);
writer.WriteOptionalMap(OpenApiConstants.Defs, Definitions, (w, s) => s.SerializeAsV3(w));
writer.WriteProperty(OpenApiConstants.DynamicRef, DynamicRef);
writer.WriteProperty(OpenApiConstants.DynamicAnchor, DynamicAnchor);
writer.WriteProperty(OpenApiConstants.RecursiveAnchor, RecursiveAnchor);
writer.WriteProperty(OpenApiConstants.RecursiveRef, RecursiveRef);
writer.WriteProperty(OpenApiConstants.V31ExclusiveMaximum, V31ExclusiveMaximum);
writer.WriteProperty(OpenApiConstants.V31ExclusiveMinimum, V31ExclusiveMinimum);
writer.WriteProperty(OpenApiConstants.UnevaluatedProperties, UnevaluatedProperties, false);
writer.WriteOptionalCollection(OpenApiConstants.Examples, _examples, (nodeWriter, s) => nodeWriter.WriteAny(s));
writer.WriteOptionalMap(OpenApiConstants.PatternProperties, PatternProperties, (w, s) => s.SerializeAsV31(w));
}
internal void WriteAsItemsProperties(IOpenApiWriter writer)
{
// type
writer.WriteProperty(OpenApiConstants.Type, (string)Type);
// format
if (string.IsNullOrEmpty(Format))
{
Format = AllOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
AnyOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
OneOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format;
}
writer.WriteProperty(OpenApiConstants.Format, Format);
// items
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));
// collectionFormat
// We need information from style in parameter to populate this.
// The best effort we can make is to pull this information from the first parameter
// that leverages this schema. However, that in itself may not be as simple
// as the schema directly under parameter might be referencing one in the Components,
// so we will need to do a full scan of the object before we can write the value for
// this property. This is not supported yet, so we will skip this property at the moment.
// default
writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d));
// maximum
writer.WriteProperty(OpenApiConstants.Maximum, Maximum);
// exclusiveMaximum
writer.WriteProperty(OpenApiConstants.ExclusiveMaximum, ExclusiveMaximum);
// minimum
writer.WriteProperty(OpenApiConstants.Minimum, Minimum);
// exclusiveMinimum
writer.WriteProperty(OpenApiConstants.ExclusiveMinimum, ExclusiveMinimum);
// maxLength
writer.WriteProperty(OpenApiConstants.MaxLength, MaxLength);
// minLength
writer.WriteProperty(OpenApiConstants.MinLength, MinLength);
// pattern
writer.WriteProperty(OpenApiConstants.Pattern, Pattern);
// maxItems
writer.WriteProperty(OpenApiConstants.MaxItems, MaxItems);
// minItems
writer.WriteProperty(OpenApiConstants.MinItems, MinItems);
// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(s));
// multipleOf
writer.WriteProperty(OpenApiConstants.MultipleOf, MultipleOf);
// extensions
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
}
/// <summary>
/// Serialize <see cref="OpenApiSchema"/> to Open Api v2.0 and handles not marking the provided property
/// as readonly if its included in the provided list of required properties of parent schema.
/// </summary>
/// <param name="writer">The open api writer.</param>
/// <param name="parentRequiredProperties">The list of required properties in parent schema.</param>
/// <param name="propertyName">The property name that will be serialized.</param>
internal void SerializeAsV2(
IOpenApiWriter writer,
ISet<string> parentRequiredProperties,
string propertyName)
{
parentRequiredProperties ??= new HashSet<string>();
writer.WriteStartObject();
// type
if (Type is string[] array)
{
DowncastTypeArrayToV2OrV3(array, writer, OpenApiSpecVersion.OpenApi2_0);
}
else
{
writer.WriteProperty(OpenApiConstants.Type, (string)Type);
}
// description
writer.WriteProperty(OpenApiConstants.Description, Description);
// format
if (string.IsNullOrEmpty(Format))
{
Format = AllOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
AnyOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ??
OneOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format;
}
writer.WriteProperty(OpenApiConstants.Format, Format);
// title
writer.WriteProperty(OpenApiConstants.Title, Title);
// default
writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d));
// multipleOf
writer.WriteProperty(OpenApiConstants.MultipleOf, MultipleOf);
// maximum
writer.WriteProperty(OpenApiConstants.Maximum, Maximum);
// exclusiveMaximum
writer.WriteProperty(OpenApiConstants.ExclusiveMaximum, ExclusiveMaximum);
// minimum
writer.WriteProperty(OpenApiConstants.Minimum, Minimum);
// exclusiveMinimum
writer.WriteProperty(OpenApiConstants.ExclusiveMinimum, ExclusiveMinimum);
// maxLength
writer.WriteProperty(OpenApiConstants.MaxLength, MaxLength);
// minLength
writer.WriteProperty(OpenApiConstants.MinLength, MinLength);
// pattern
writer.WriteProperty(OpenApiConstants.Pattern, Pattern);
// maxItems
writer.WriteProperty(OpenApiConstants.MaxItems, MaxItems);
// minItems
writer.WriteProperty(OpenApiConstants.MinItems, MinItems);
// uniqueItems
writer.WriteProperty(OpenApiConstants.UniqueItems, UniqueItems);
// maxProperties
writer.WriteProperty(OpenApiConstants.MaxProperties, MaxProperties);
// minProperties
writer.WriteProperty(OpenApiConstants.MinProperties, MinProperties);
// required
writer.WriteOptionalCollection(OpenApiConstants.Required, Required, (w, s) => w.WriteValue(s));
// enum
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(s));
// items
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));
// allOf
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w));
// If there isn't already an allOf, and the schema contains a oneOf or anyOf write an allOf with the first
// schema in the list as an attempt to guess at a graceful downgrade situation.
if (AllOf == null || AllOf.Count == 0)
{
// anyOf (Not Supported in V2) - Write the first schema only as an allOf.
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AnyOf?.Take(1), (w, s) => s.SerializeAsV2(w));
if (AnyOf == null || AnyOf.Count == 0)
{
// oneOf (Not Supported in V2) - Write the first schema only as an allOf.
writer.WriteOptionalCollection(OpenApiConstants.AllOf, OneOf?.Take(1), (w, s) => s.SerializeAsV2(w));
}
}
// properties
writer.WriteOptionalMap(OpenApiConstants.Properties, Properties, (w, key, s) =>
s.SerializeAsV2(w, Required, key));
// additionalProperties
if (AdditionalPropertiesAllowed)
{
writer.WriteOptionalObject(
OpenApiConstants.AdditionalProperties,
AdditionalProperties,
(w, s) => s.SerializeAsV2(w));
}
else
{
writer.WriteProperty(OpenApiConstants.AdditionalProperties, AdditionalPropertiesAllowed);
}
// discriminator
writer.WriteProperty(OpenApiConstants.Discriminator, Discriminator?.PropertyName);
// readOnly
// In V2 schema if a property is part of required properties of parent schema,
// it cannot be marked as readonly.
if (!parentRequiredProperties.Contains(propertyName))
{
writer.WriteProperty(name: OpenApiConstants.ReadOnly, value: ReadOnly, defaultValue: false);
}
// xml
writer.WriteOptionalObject(OpenApiConstants.Xml, Xml, (w, s) => s.SerializeAsV2(w));
// externalDocs
writer.WriteOptionalObject(OpenApiConstants.ExternalDocs, ExternalDocs, (w, s) => s.SerializeAsV2(w));
// example
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e));
// extensions
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
writer.WriteEndObject();
}
private void SerializeTypeProperty(object type, IOpenApiWriter writer, OpenApiSpecVersion version)
{
if (type?.GetType() == typeof(string))
{
// check whether nullable is true for upcasting purposes
if (Nullable || Extensions.ContainsKey(OpenApiConstants.NullableExtension))
{
// create a new array and insert the type and "null" as values
Type = new[] { (string)Type, OpenApiConstants.Null };
}
else
{
writer.WriteProperty(OpenApiConstants.Type, (string)Type);
}
}
if (Type is string[] array)
{
// type
if (version is OpenApiSpecVersion.OpenApi3_0)
{
DowncastTypeArrayToV2OrV3(array, writer, OpenApiSpecVersion.OpenApi3_0);
}
else
{
writer.WriteOptionalCollection(OpenApiConstants.Type, (string[])Type, (w, s) => w.WriteRaw(s));
}
}
}
private object DeepCloneType(object type)
{
if (type == null)
return null;
if (type is string)
{
return type; // Return the string as is
}
if (type is Array array)
{
Type elementType = type.GetType().GetElementType();
Array copiedArray = Array.CreateInstance(elementType, array.Length);
for (int i = 0; i < array?.Length; i++)
{
copiedArray.SetValue(DeepCloneType(array?.GetValue(i)), i);
}
return copiedArray;
}
return null;
}
private void DowncastTypeArrayToV2OrV3(string[] array, IOpenApiWriter writer, OpenApiSpecVersion version)
{
/* If the array has one non-null value, emit Type as string
* If the array has one null value, emit x-nullable as true
* If the array has two values, one null and one non-null, emit Type as string and x-nullable as true
* If the array has more than two values or two non-null values, do not emit type
* */
var nullableProp = version.Equals(OpenApiSpecVersion.OpenApi2_0)
? OpenApiConstants.NullableExtension
: OpenApiConstants.Nullable;
if (array.Length is 1)
{
var value = array[0];
if (value is OpenApiConstants.Null)
{
writer.WriteProperty(nullableProp, true);
}
else
{
writer.WriteProperty(OpenApiConstants.Type, value);
}
}
else if (array.Length is 2 && array.Contains(OpenApiConstants.Null))
{
// Find the non-null value and write it out
var nonNullValue = array.First(v => v != OpenApiConstants.Null);
writer.WriteProperty(OpenApiConstants.Type, nonNullValue);
if (!Nullable)
{
writer.WriteProperty(nullableProp, true);
}
}
}
}
}