Skip to content

Commit 81b41e9

Browse files
committed
Write string value for Type for V2 and V3, allow type array for V3
1 parent 2ffebe7 commit 81b41e9

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 29 additions & 11 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;
@@ -14,8 +14,11 @@ namespace Microsoft.OpenApi.Models
1414
/// <summary>
1515
/// The Schema Object allows the definition of input and output data types.
1616
/// </summary>
17-
public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable
17+
public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApiSerializable
1818
{
19+
private string[] _typeArray;
20+
private string _type;
21+
1922
/// <summary>
2023
/// Follow JSON Schema definition. Short text providing information about the data.
2124
/// </summary>
@@ -86,13 +89,21 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable
8689
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
8790
/// Value MUST be a string in V2 and V3.
8891
/// </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+
}
96107

97108
/// <summary>
98109
/// 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
484495
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(new OpenApiAny(s)));
485496

486497
// 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+
}
488506

489507
// allOf
490508
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV3(w));
@@ -695,7 +713,7 @@ internal void WriteAsSchemaProperties(
695713
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(new OpenApiAny(s)));
696714

697715
// type
698-
writer.WriteProperty(OpenApiConstants.Type, Type);
716+
writer.WriteProperty(OpenApiConstants.Type, _type);
699717

700718
// items
701719
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));

0 commit comments

Comments
 (0)