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 ;
@@ -16,9 +16,6 @@ namespace Microsoft.OpenApi.Models
16
16
/// </summary>
17
17
public class OpenApiSchema : IOpenApiExtensible , IOpenApiReferenceable , IOpenApiSerializable
18
18
{
19
- private string [ ] _typeArray ;
20
- private string _type ;
21
-
22
19
/// <summary>
23
20
/// Follow JSON Schema definition. Short text providing information about the data.
24
21
/// </summary>
@@ -81,29 +78,15 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
81
78
public decimal ? V31ExclusiveMinimum { get ; set ; }
82
79
83
80
/// <summary>
84
- ///
81
+ /// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
85
82
/// </summary>
86
83
public bool UnEvaluatedProperties { get ; set ; }
87
84
88
85
/// <summary>
89
86
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
90
87
/// Value MUST be a string in V2 and V3.
91
88
/// </summary>
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
- }
89
+ public object Type { get ; set ; }
107
90
108
91
/// <summary>
109
92
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
@@ -349,8 +332,7 @@ public OpenApiSchema(OpenApiSchema schema)
349
332
UnevaluatedProperties = schema ? . UnevaluatedProperties ?? UnevaluatedProperties ;
350
333
V31ExclusiveMaximum = schema ? . V31ExclusiveMaximum ?? V31ExclusiveMaximum ;
351
334
V31ExclusiveMinimum = schema ? . V31ExclusiveMinimum ?? V31ExclusiveMinimum ;
352
- Type = schema ? . Type ?? Type ;
353
- TypeArray = schema ? . TypeArray != null ? new string [ schema . TypeArray . Length ] : null ;
335
+ Type = DeepCloneType ( schema ? . Type ) ;
354
336
Format = schema ? . Format ?? Format ;
355
337
Description = schema ? . Description ?? Description ;
356
338
Maximum = schema ? . Maximum ?? Maximum ;
@@ -497,11 +479,11 @@ public void SerializeInternalWithoutReference(IOpenApiWriter writer, OpenApiSpec
497
479
// type
498
480
if ( Type . GetType ( ) == typeof ( string ) )
499
481
{
500
- writer . WriteProperty ( OpenApiConstants . Type , _type ) ;
482
+ writer . WriteProperty ( OpenApiConstants . Type , ( string ) Type ) ;
501
483
}
502
484
else
503
485
{
504
- writer . WriteOptionalCollection ( OpenApiConstants . Type , _typeArray , ( w , s ) => w . WriteRaw ( s ) ) ;
486
+ writer . WriteOptionalCollection ( OpenApiConstants . Type , ( string [ ] ) Type , ( w , s ) => w . WriteRaw ( s ) ) ;
505
487
}
506
488
507
489
// allOf
@@ -712,7 +694,7 @@ internal void WriteAsSchemaProperties(
712
694
writer . WriteOptionalCollection ( OpenApiConstants . Enum , Enum , ( w , s ) => w . WriteAny ( new OpenApiAny ( s ) ) ) ;
713
695
714
696
// type
715
- writer . WriteProperty ( OpenApiConstants . Type , _type ) ;
697
+ writer . WriteProperty ( OpenApiConstants . Type , ( string ) Type ) ;
716
698
717
699
// items
718
700
writer . WriteOptionalObject ( OpenApiConstants . Items , Items , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
@@ -774,5 +756,28 @@ internal void WriteAsSchemaProperties(
774
756
// extensions
775
757
writer . WriteExtensions ( Extensions , OpenApiSpecVersion . OpenApi2_0 ) ;
776
758
}
759
+
760
+ private object DeepCloneType ( object type )
761
+ {
762
+ if ( type == null )
763
+ return null ;
764
+
765
+ if ( type is string )
766
+ {
767
+ return type ; // Return the string as is
768
+ }
769
+
770
+ else
771
+ {
772
+ var array = type as Array ;
773
+ Type elementType = type . GetType ( ) . GetElementType ( ) ;
774
+ Array copiedArray = Array . CreateInstance ( elementType , array . Length ) ;
775
+ for ( int i = 0 ; i < array . Length ; i ++ )
776
+ {
777
+ copiedArray . SetValue ( DeepCloneType ( array . GetValue ( i ) ) , i ) ;
778
+ }
779
+ return copiedArray ;
780
+ }
781
+ }
777
782
}
778
783
}
0 commit comments