1
- // Copyright (c) Microsoft Corporation. All rights reserved.
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
4
using System ;
@@ -14,8 +14,11 @@ namespace Microsoft.OpenApi.Models
14
14
/// <summary>
15
15
/// The Schema Object allows the definition of input and output data types.
16
16
/// </summary>
17
- public class OpenApiSchema : IOpenApiExtensible , IOpenApiReferenceable
17
+ public class OpenApiSchema : IOpenApiExtensible , IOpenApiReferenceable , IOpenApiSerializable
18
18
{
19
+ private string [ ] _typeArray ;
20
+ private string _type ;
21
+
19
22
/// <summary>
20
23
/// Follow JSON Schema definition. Short text providing information about the data.
21
24
/// </summary>
@@ -86,13 +89,21 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable
86
89
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
87
90
/// Value MUST be a string in V2 and V3.
88
91
/// </summary>
89
- public string Type { get ; set ; }
90
-
91
- /// <summary>
92
- /// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
93
- /// Multiple types via an array are supported in V31.
94
- /// </summary>
95
- public string [ ] TypeArray { get ; set ; }
92
+ public object Type
93
+ {
94
+ get => _type ;
95
+ set
96
+ {
97
+ if ( value is string || value is JsonNode )
98
+ {
99
+ _type = ( string ) value ;
100
+ }
101
+ else
102
+ {
103
+ _typeArray = ( string [ ] ) value ;
104
+ }
105
+ }
106
+ }
96
107
97
108
/// <summary>
98
109
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
@@ -484,7 +495,14 @@ public void SerializeInternalWithoutReference(IOpenApiWriter writer, OpenApiSpec
484
495
writer . WriteOptionalCollection ( OpenApiConstants . Enum , Enum , ( nodeWriter , s ) => nodeWriter . WriteAny ( new OpenApiAny ( s ) ) ) ;
485
496
486
497
// type
487
- writer . WriteProperty ( OpenApiConstants . Type , Type ) ;
498
+ if ( Type . GetType ( ) == typeof ( string ) )
499
+ {
500
+ writer . WriteProperty ( OpenApiConstants . Type , _type ) ;
501
+ }
502
+ else
503
+ {
504
+ writer . WriteOptionalCollection ( OpenApiConstants . Type , _typeArray , ( w , s ) => w . WriteRaw ( s ) ) ;
505
+ }
488
506
489
507
// allOf
490
508
writer . WriteOptionalCollection ( OpenApiConstants . AllOf , AllOf , ( w , s ) => s . SerializeAsV3 ( w ) ) ;
@@ -695,7 +713,7 @@ internal void WriteAsSchemaProperties(
695
713
writer . WriteOptionalCollection ( OpenApiConstants . Enum , Enum , ( w , s ) => w . WriteAny ( new OpenApiAny ( s ) ) ) ;
696
714
697
715
// type
698
- writer . WriteProperty ( OpenApiConstants . Type , Type ) ;
716
+ writer . WriteProperty ( OpenApiConstants . Type , _type ) ;
699
717
700
718
// items
701
719
writer . WriteOptionalObject ( OpenApiConstants . Items , Items , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
0 commit comments