@@ -15,12 +15,8 @@ namespace Microsoft.OpenApi.Models
1515 /// <summary>
1616 /// The Schema Object allows the definition of input and output data types.
1717 /// </summary>
18- public class OpenApiSchema : IOpenApiAnnotatable , IOpenApiExtensible , IOpenApiReferenceable , IOpenApiSerializable
18+ public class OpenApiSchema : IOpenApiAnnotatable , IOpenApiExtensible , IOpenApiReferenceable
1919 {
20- private JsonNode _example ;
21- private JsonNode _default ;
22- private IList < JsonNode > _examples ;
23-
2420 /// <summary>
2521 /// Follow JSON Schema definition. Short text providing information about the data.
2622 /// </summary>
@@ -148,11 +144,7 @@ public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiRe
148144 /// Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level.
149145 /// For example, if type is string, then default can be "foo" but cannot be 1.
150146 /// </summary>
151- public virtual JsonNode Default
152- {
153- get => _default ;
154- set => _default = value ;
155- }
147+ public virtual JsonNode Default { get ; set ; }
156148
157149 /// <summary>
158150 /// Relevant only for Schema "properties" definitions. Declares the property as "read only".
@@ -273,22 +265,14 @@ public virtual JsonNode Default
273265 /// To represent examples that cannot be naturally represented in JSON or YAML,
274266 /// a string value can be used to contain the example with escaping where necessary.
275267 /// </summary>
276- public virtual JsonNode Example
277- {
278- get => _example ;
279- set => _example = value ;
280- }
268+ public virtual JsonNode Example { get ; set ; }
281269
282270 /// <summary>
283271 /// A free-form property to include examples of an instance for this schema.
284272 /// To represent examples that cannot be naturally represented in JSON or YAML,
285273 /// a list of values can be used to contain the examples with escaping where necessary.
286274 /// </summary>
287- public virtual IList < JsonNode > Examples
288- {
289- get => _examples ;
290- set => _examples = value ;
291- }
275+ public virtual IList < JsonNode > Examples { get ; set ; }
292276
293277 /// <summary>
294278 /// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
@@ -327,6 +311,11 @@ public virtual IList<JsonNode> Examples
327311 /// </summary>
328312 public virtual IDictionary < string , IOpenApiExtension > Extensions { get ; set ; } = new Dictionary < string , IOpenApiExtension > ( ) ;
329313
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+
330319 /// <summary>
331320 /// Indicates object is a placeholder reference to an actual object and does not contain valid data.
332321 /// </summary>
@@ -373,7 +362,7 @@ public OpenApiSchema(OpenApiSchema schema)
373362 MinLength = schema ? . MinLength ?? MinLength ;
374363 Pattern = schema ? . Pattern ?? Pattern ;
375364 MultipleOf = schema ? . MultipleOf ?? MultipleOf ;
376- _default = schema ? . Default != null ? JsonNodeCloneHelper . Clone ( schema ? . Default ) : null ;
365+ Default = schema ? . Default != null ? JsonNodeCloneHelper . Clone ( schema ? . Default ) : null ;
377366 ReadOnly = schema ? . ReadOnly ?? ReadOnly ;
378367 WriteOnly = schema ? . WriteOnly ?? WriteOnly ;
379368 AllOf = schema ? . AllOf != null ? new List < OpenApiSchema > ( schema . AllOf ) : null ;
@@ -392,8 +381,8 @@ public OpenApiSchema(OpenApiSchema schema)
392381 AdditionalPropertiesAllowed = schema ? . AdditionalPropertiesAllowed ?? AdditionalPropertiesAllowed ;
393382 AdditionalProperties = schema ? . AdditionalProperties != null ? new ( schema ? . AdditionalProperties ) : null ;
394383 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 ;
397386 Enum = schema ? . Enum != null ? new List < JsonNode > ( schema . Enum ) : null ;
398387 Nullable = schema ? . Nullable ?? Nullable ;
399388 ExternalDocs = schema ? . ExternalDocs != null ? new ( schema ? . ExternalDocs ) : null ;
@@ -403,6 +392,7 @@ public OpenApiSchema(OpenApiSchema schema)
403392 UnresolvedReference = schema ? . UnresolvedReference ?? UnresolvedReference ;
404393 Reference = schema ? . Reference != null ? new ( schema ? . Reference ) : null ;
405394 Annotations = schema ? . Annotations != null ? new Dictionary < string , object > ( schema ? . Annotations ) : null ;
395+ UnrecognizedKeywords = schema ? . UnrecognizedKeywords != null ? new Dictionary < string , JsonNode > ( schema ? . UnrecognizedKeywords ) : null ;
406396 }
407397
408398 /// <summary>
@@ -430,7 +420,7 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
430420
431421 if ( version == OpenApiSpecVersion . OpenApi3_1 )
432422 {
433- WriteV31Properties ( writer ) ;
423+ WriteJsonSchemaKeywords ( writer ) ;
434424 }
435425
436426 // title
@@ -554,6 +544,12 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
554544 // extensions
555545 writer . WriteExtensions ( Extensions , version ) ;
556546
547+ // Unrecognized keywords
548+ if ( UnrecognizedKeywords . Any ( ) )
549+ {
550+ writer . WriteOptionalMap ( OpenApiConstants . UnrecognizedKeywords , UnrecognizedKeywords , ( w , s ) => w . WriteAny ( s ) ) ;
551+ }
552+
557553 writer . WriteEndObject ( ) ;
558554 }
559555
@@ -564,7 +560,7 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
564560 SerializeAsV2 ( writer : writer , parentRequiredProperties : new HashSet < string > ( ) , propertyName : null ) ;
565561 }
566562
567- internal void WriteV31Properties ( IOpenApiWriter writer )
563+ internal void WriteJsonSchemaKeywords ( IOpenApiWriter writer )
568564 {
569565 writer . WriteProperty ( OpenApiConstants . Id , Id ) ;
570566 writer . WriteProperty ( OpenApiConstants . DollarSchema , Schema ) ;
@@ -577,7 +573,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
577573 writer . WriteProperty ( OpenApiConstants . V31ExclusiveMaximum , V31ExclusiveMaximum ) ;
578574 writer . WriteProperty ( OpenApiConstants . V31ExclusiveMinimum , V31ExclusiveMinimum ) ;
579575 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 ) ) ;
581577 writer . WriteOptionalMap ( OpenApiConstants . PatternProperties , PatternProperties , ( w , s ) => s . SerializeAsV31 ( w ) ) ;
582578 }
583579
@@ -820,15 +816,9 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer,
820816 }
821817 else
822818 {
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 ( ) ;
832822 writer . WriteOptionalCollection ( OpenApiConstants . Type , list , ( w , s ) => w . WriteValue ( s . ToIdentifier ( ) ) ) ;
833823 }
834824 }
@@ -850,16 +840,9 @@ private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer)
850840 {
851841 // create a new array and insert the type and "null" as values
852842 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 ( ) ;
863846 writer . WriteOptionalCollection ( OpenApiConstants . Type , list , ( w , s ) => w . WriteValue ( s ) ) ;
864847 }
865848
0 commit comments