@@ -15,12 +15,8 @@ namespace Microsoft.OpenApi.Models
15
15
/// <summary>
16
16
/// The Schema Object allows the definition of input and output data types.
17
17
/// </summary>
18
- public class OpenApiSchema : IOpenApiAnnotatable , IOpenApiExtensible , IOpenApiReferenceable , IOpenApiSerializable
18
+ public class OpenApiSchema : IOpenApiAnnotatable , IOpenApiExtensible , IOpenApiReferenceable
19
19
{
20
- private JsonNode _example ;
21
- private JsonNode _default ;
22
- private IList < JsonNode > _examples ;
23
-
24
20
/// <summary>
25
21
/// Follow JSON Schema definition. Short text providing information about the data.
26
22
/// </summary>
@@ -148,11 +144,7 @@ public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiRe
148
144
/// Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level.
149
145
/// For example, if type is string, then default can be "foo" but cannot be 1.
150
146
/// </summary>
151
- public virtual JsonNode Default
152
- {
153
- get => _default ;
154
- set => _default = value ;
155
- }
147
+ public virtual JsonNode Default { get ; set ; }
156
148
157
149
/// <summary>
158
150
/// Relevant only for Schema "properties" definitions. Declares the property as "read only".
@@ -273,22 +265,14 @@ public virtual JsonNode Default
273
265
/// To represent examples that cannot be naturally represented in JSON or YAML,
274
266
/// a string value can be used to contain the example with escaping where necessary.
275
267
/// </summary>
276
- public virtual JsonNode Example
277
- {
278
- get => _example ;
279
- set => _example = value ;
280
- }
268
+ public virtual JsonNode Example { get ; set ; }
281
269
282
270
/// <summary>
283
271
/// A free-form property to include examples of an instance for this schema.
284
272
/// To represent examples that cannot be naturally represented in JSON or YAML,
285
273
/// a list of values can be used to contain the examples with escaping where necessary.
286
274
/// </summary>
287
- public virtual IList < JsonNode > Examples
288
- {
289
- get => _examples ;
290
- set => _examples = value ;
291
- }
275
+ public virtual IList < JsonNode > Examples { get ; set ; }
292
276
293
277
/// <summary>
294
278
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
@@ -327,6 +311,11 @@ public virtual IList<JsonNode> Examples
327
311
/// </summary>
328
312
public virtual IDictionary < string , IOpenApiExtension > Extensions { get ; set ; } = new Dictionary < string , IOpenApiExtension > ( ) ;
329
313
314
+ /// <summary>
315
+ /// This object stores any unrecognized keywords found in the schema.
316
+ /// </summary>
317
+ public virtual IDictionary < string , JsonNode > UnrecognizedKeywords { get ; set ; } = new Dictionary < string , JsonNode > ( ) ;
318
+
330
319
/// <summary>
331
320
/// Indicates object is a placeholder reference to an actual object and does not contain valid data.
332
321
/// </summary>
@@ -373,7 +362,7 @@ public OpenApiSchema(OpenApiSchema schema)
373
362
MinLength = schema ? . MinLength ?? MinLength ;
374
363
Pattern = schema ? . Pattern ?? Pattern ;
375
364
MultipleOf = schema ? . MultipleOf ?? MultipleOf ;
376
- _default = schema ? . Default != null ? JsonNodeCloneHelper . Clone ( schema ? . Default ) : null ;
365
+ Default = schema ? . Default != null ? JsonNodeCloneHelper . Clone ( schema ? . Default ) : null ;
377
366
ReadOnly = schema ? . ReadOnly ?? ReadOnly ;
378
367
WriteOnly = schema ? . WriteOnly ?? WriteOnly ;
379
368
AllOf = schema ? . AllOf != null ? new List < OpenApiSchema > ( schema . AllOf ) : null ;
@@ -392,8 +381,8 @@ public OpenApiSchema(OpenApiSchema schema)
392
381
AdditionalPropertiesAllowed = schema ? . AdditionalPropertiesAllowed ?? AdditionalPropertiesAllowed ;
393
382
AdditionalProperties = schema ? . AdditionalProperties != null ? new ( schema ? . AdditionalProperties ) : null ;
394
383
Discriminator = schema ? . Discriminator != null ? new ( schema ? . Discriminator ) : null ;
395
- _example = schema ? . Example != null ? JsonNodeCloneHelper . Clone ( schema ? . Example ) : null ;
396
- _examples = schema ? . Examples != null ? new List < JsonNode > ( schema . Examples ) : null ;
384
+ Example = schema ? . Example != null ? JsonNodeCloneHelper . Clone ( schema ? . Example ) : null ;
385
+ Examples = schema ? . Examples != null ? new List < JsonNode > ( schema . Examples ) : null ;
397
386
Enum = schema ? . Enum != null ? new List < JsonNode > ( schema . Enum ) : null ;
398
387
Nullable = schema ? . Nullable ?? Nullable ;
399
388
ExternalDocs = schema ? . ExternalDocs != null ? new ( schema ? . ExternalDocs ) : null ;
@@ -403,6 +392,7 @@ public OpenApiSchema(OpenApiSchema schema)
403
392
UnresolvedReference = schema ? . UnresolvedReference ?? UnresolvedReference ;
404
393
Reference = schema ? . Reference != null ? new ( schema ? . Reference ) : null ;
405
394
Annotations = schema ? . Annotations != null ? new Dictionary < string , object > ( schema ? . Annotations ) : null ;
395
+ UnrecognizedKeywords = schema ? . UnrecognizedKeywords != null ? new Dictionary < string , JsonNode > ( schema ? . UnrecognizedKeywords ) : null ;
406
396
}
407
397
408
398
/// <summary>
@@ -430,7 +420,7 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
430
420
431
421
if ( version == OpenApiSpecVersion . OpenApi3_1 )
432
422
{
433
- WriteV31Properties ( writer ) ;
423
+ WriteJsonSchemaKeywords ( writer ) ;
434
424
}
435
425
436
426
// title
@@ -554,6 +544,12 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
554
544
// extensions
555
545
writer . WriteExtensions ( Extensions , version ) ;
556
546
547
+ // Unrecognized keywords
548
+ if ( UnrecognizedKeywords . Any ( ) )
549
+ {
550
+ writer . WriteOptionalMap ( OpenApiConstants . UnrecognizedKeywords , UnrecognizedKeywords , ( w , s ) => w . WriteAny ( s ) ) ;
551
+ }
552
+
557
553
writer . WriteEndObject ( ) ;
558
554
}
559
555
@@ -564,7 +560,7 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
564
560
SerializeAsV2 ( writer : writer , parentRequiredProperties : new HashSet < string > ( ) , propertyName : null ) ;
565
561
}
566
562
567
- internal void WriteV31Properties ( IOpenApiWriter writer )
563
+ internal void WriteJsonSchemaKeywords ( IOpenApiWriter writer )
568
564
{
569
565
writer . WriteProperty ( OpenApiConstants . Id , Id ) ;
570
566
writer . WriteProperty ( OpenApiConstants . DollarSchema , Schema ) ;
@@ -577,7 +573,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
577
573
writer . WriteProperty ( OpenApiConstants . V31ExclusiveMaximum , V31ExclusiveMaximum ) ;
578
574
writer . WriteProperty ( OpenApiConstants . V31ExclusiveMinimum , V31ExclusiveMinimum ) ;
579
575
writer . WriteProperty ( OpenApiConstants . UnevaluatedProperties , UnevaluatedProperties , false ) ;
580
- writer . WriteOptionalCollection ( OpenApiConstants . Examples , _examples , ( nodeWriter , s ) => nodeWriter . WriteAny ( s ) ) ;
576
+ writer . WriteOptionalCollection ( OpenApiConstants . Examples , Examples , ( nodeWriter , s ) => nodeWriter . WriteAny ( s ) ) ;
581
577
writer . WriteOptionalMap ( OpenApiConstants . PatternProperties , PatternProperties , ( w , s ) => s . SerializeAsV31 ( w ) ) ;
582
578
}
583
579
@@ -820,15 +816,9 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer,
820
816
}
821
817
else
822
818
{
823
- var list = new List < JsonSchemaType > ( ) ;
824
- foreach ( JsonSchemaType flag in jsonSchemaTypeValues )
825
- {
826
- if ( type . Value . HasFlag ( flag ) )
827
- {
828
- list . Add ( flag ) ;
829
- }
830
- }
831
-
819
+ var list = ( from JsonSchemaType flag in jsonSchemaTypeValues
820
+ where type . Value . HasFlag ( flag )
821
+ select flag ) . ToList ( ) ;
832
822
writer . WriteOptionalCollection ( OpenApiConstants . Type , list , ( w , s ) => w . WriteValue ( s . ToIdentifier ( ) ) ) ;
833
823
}
834
824
}
@@ -850,16 +840,9 @@ private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer)
850
840
{
851
841
// create a new array and insert the type and "null" as values
852
842
Type = type | JsonSchemaType . Null ;
853
- var list = new List < string > ( ) ;
854
- foreach ( JsonSchemaType ? flag in jsonSchemaTypeValues )
855
- {
856
- // Check if the flag is set in 'type' using a bitwise AND operation
857
- if ( Type . Value . HasFlag ( flag ) )
858
- {
859
- list . Add ( flag . ToIdentifier ( ) ) ;
860
- }
861
- }
862
-
843
+ var list = ( from JsonSchemaType ? flag in jsonSchemaTypeValues // Check if the flag is set in 'type' using a bitwise AND operation
844
+ where Type . Value . HasFlag ( flag )
845
+ select flag . ToIdentifier ( ) ) . ToList ( ) ;
863
846
writer . WriteOptionalCollection ( OpenApiConstants . Type , list , ( w , s ) => w . WriteValue ( s ) ) ;
864
847
}
865
848
0 commit comments