Skip to content

Commit 57ca1cc

Browse files
committed
Clean up type array support logic
1 parent ee2495f commit 57ca1cc

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -16,9 +16,6 @@ namespace Microsoft.OpenApi.Models
1616
/// </summary>
1717
public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApiSerializable
1818
{
19-
private string[] _typeArray;
20-
private string _type;
21-
2219
/// <summary>
2320
/// Follow JSON Schema definition. Short text providing information about the data.
2421
/// </summary>
@@ -81,29 +78,15 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
8178
public decimal? V31ExclusiveMinimum { get; set; }
8279

8380
/// <summary>
84-
///
81+
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
8582
/// </summary>
8683
public bool UnEvaluatedProperties { get; set; }
8784

8885
/// <summary>
8986
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
9087
/// Value MUST be a string in V2 and V3.
9188
/// </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; }
10790

10891
/// <summary>
10992
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
@@ -349,8 +332,7 @@ public OpenApiSchema(OpenApiSchema schema)
349332
UnevaluatedProperties = schema?.UnevaluatedProperties ?? UnevaluatedProperties;
350333
V31ExclusiveMaximum = schema?.V31ExclusiveMaximum ?? V31ExclusiveMaximum;
351334
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);
354336
Format = schema?.Format ?? Format;
355337
Description = schema?.Description ?? Description;
356338
Maximum = schema?.Maximum ?? Maximum;
@@ -497,11 +479,11 @@ public void SerializeInternalWithoutReference(IOpenApiWriter writer, OpenApiSpec
497479
// type
498480
if (Type.GetType() == typeof(string))
499481
{
500-
writer.WriteProperty(OpenApiConstants.Type, _type);
482+
writer.WriteProperty(OpenApiConstants.Type, (string)Type);
501483
}
502484
else
503485
{
504-
writer.WriteOptionalCollection(OpenApiConstants.Type, _typeArray, (w, s) => w.WriteRaw(s));
486+
writer.WriteOptionalCollection(OpenApiConstants.Type, (string[])Type, (w, s) => w.WriteRaw(s));
505487
}
506488

507489
// allOf
@@ -712,7 +694,7 @@ internal void WriteAsSchemaProperties(
712694
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(new OpenApiAny(s)));
713695

714696
// type
715-
writer.WriteProperty(OpenApiConstants.Type, _type);
697+
writer.WriteProperty(OpenApiConstants.Type, (string)Type);
716698

717699
// items
718700
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));
@@ -774,5 +756,28 @@ internal void WriteAsSchemaProperties(
774756
// extensions
775757
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
776758
}
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+
}
777782
}
778783
}

0 commit comments

Comments
 (0)