diff --git a/src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs b/src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs index 8749e4537..c2bbc97d0 100644 --- a/src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs +++ b/src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs @@ -166,10 +166,10 @@ private static IList ResolveFunctionParameters(IList ResolveFunctionParameters(IList public static class OpenApiTypeMapper { + /// + /// Maps a JsonSchema data type to an identifier. + /// + /// + /// + public static string ToIdentifier(this JsonSchemaType? schemaType) + { + return schemaType switch + { + JsonSchemaType.Null => "null", + JsonSchemaType.Boolean => "boolean", + JsonSchemaType.Integer => "integer", + JsonSchemaType.Number => "number", + JsonSchemaType.String => "string", + JsonSchemaType.Array => "array", + JsonSchemaType.Object => "object", + _ => null, + }; + } + + /// + /// Converts a schema type's identifier into the enum equivalent + /// + /// + /// + public static JsonSchemaType ToJsonSchemaType(this string identifier) + { + return identifier switch + { + "null" => JsonSchemaType.Null, + "boolean" => JsonSchemaType.Boolean, + "integer" or "int" => JsonSchemaType.Integer, + "number" or "double" or "float" or "decimal"=> JsonSchemaType.Number, + "string" => JsonSchemaType.String, + "array" => JsonSchemaType.Array, + "object" => JsonSchemaType.Object, + "file" => JsonSchemaType.String, // File is treated as string + _ => throw new OpenApiException(string.Format("Invalid schema type identifier: {0}", identifier)) + }; + } + private static readonly Dictionary> _simpleTypeToOpenApiSchema = new() { - [typeof(bool)] = () => new() { Type = "boolean" }, - [typeof(byte)] = () => new() { Type = "string", Format = "byte" }, - [typeof(int)] = () => new() { Type = "number", Format = "int32" }, - [typeof(uint)] = () => new() { Type = "number", Format = "int32" }, - [typeof(long)] = () => new() { Type = "number", Format = "int64" }, - [typeof(ulong)] = () => new() { Type = "number", Format = "int64" }, - [typeof(float)] = () => new() { Type = "number", Format = "float" }, - [typeof(double)] = () => new() { Type = "number", Format = "double" }, - [typeof(decimal)] = () => new() { Type = "number", Format = "double" }, - [typeof(DateTime)] = () => new() { Type = "string", Format = "date-time" }, - [typeof(DateTimeOffset)] = () => new() { Type = "string", Format = "date-time" }, - [typeof(Guid)] = () => new() { Type = "string", Format = "uuid" }, - [typeof(char)] = () => new() { Type = "string" }, + [typeof(bool)] = () => new() { Type = JsonSchemaType.Boolean }, + [typeof(byte)] = () => new() { Type = JsonSchemaType.String, Format = "byte" }, + [typeof(int)] = () => new() { Type = JsonSchemaType.Integer, Format = "int32" }, + [typeof(uint)] = () => new() { Type = JsonSchemaType.Integer, Format = "int32" }, + [typeof(long)] = () => new() { Type = JsonSchemaType.Integer, Format = "int64" }, + [typeof(ulong)] = () => new() { Type = JsonSchemaType.Integer, Format = "int64" }, + [typeof(float)] = () => new() { Type = JsonSchemaType.Number, Format = "float" }, + [typeof(double)] = () => new() { Type = JsonSchemaType.Number, Format = "double" }, + [typeof(decimal)] = () => new() { Type = JsonSchemaType.Number, Format = "double" }, + [typeof(DateTime)] = () => new() { Type = JsonSchemaType.String, Format = "date-time" }, + [typeof(DateTimeOffset)] = () => new() { Type = JsonSchemaType.String, Format = "date-time" }, + [typeof(Guid)] = () => new() { Type = JsonSchemaType.String, Format = "uuid" }, + [typeof(char)] = () => new() { Type = JsonSchemaType.String }, // Nullable types - [typeof(bool?)] = () => new() { Type = "boolean", Nullable = true }, - [typeof(byte?)] = () => new() { Type = "string", Format = "byte", Nullable = true }, - [typeof(int?)] = () => new() { Type = "number", Format = "int32", Nullable = true }, - [typeof(uint?)] = () => new() { Type = "number", Format = "int32", Nullable = true }, - [typeof(long?)] = () => new() { Type = "number", Format = "int64", Nullable = true }, - [typeof(ulong?)] = () => new() { Type = "number", Format = "int64", Nullable = true }, - [typeof(float?)] = () => new() { Type = "number", Format = "float", Nullable = true }, - [typeof(double?)] = () => new() { Type = "number", Format = "double", Nullable = true }, - [typeof(decimal?)] = () => new() { Type = "number", Format = "double", Nullable = true }, - [typeof(DateTime?)] = () => new() { Type = "string", Format = "date-time", Nullable = true }, - [typeof(DateTimeOffset?)] = () => new() { Type = "string", Format = "date-time", Nullable = true }, - [typeof(Guid?)] = () => new() { Type = "string", Format = "uuid", Nullable = true }, - [typeof(char?)] = () => new() { Type = "string", Nullable = true }, + [typeof(bool?)] = () => new() { Type = JsonSchemaType.Boolean, Nullable = true }, + [typeof(byte?)] = () => new() { Type = JsonSchemaType.String, Format = "byte", Nullable = true }, + [typeof(int?)] = () => new() { Type = JsonSchemaType.Integer, Format = "int32", Nullable = true }, + [typeof(uint?)] = () => new() { Type = JsonSchemaType.Integer, Format = "int32", Nullable = true }, + [typeof(long?)] = () => new() { Type = JsonSchemaType.Integer, Format = "int64", Nullable = true }, + [typeof(ulong?)] = () => new() { Type = JsonSchemaType.Integer, Format = "int64", Nullable = true }, + [typeof(float?)] = () => new() { Type = JsonSchemaType.Number, Format = "float", Nullable = true }, + [typeof(double?)] = () => new() { Type = JsonSchemaType.Number, Format = "double", Nullable = true }, + [typeof(decimal?)] = () => new() { Type = JsonSchemaType.Number, Format = "double", Nullable = true }, + [typeof(DateTime?)] = () => new() { Type = JsonSchemaType.String, Format = "date-time", Nullable = true }, + [typeof(DateTimeOffset?)] = () => new() { Type = JsonSchemaType.String, Format = "date-time", Nullable = true }, + [typeof(Guid?)] = () => new() { Type = JsonSchemaType.String, Format = "uuid", Nullable = true }, + [typeof(char?)] = () => new() { Type = JsonSchemaType.String, Nullable = true }, - [typeof(Uri)] = () => new() { Type = "string", Format = "uri" }, // Uri is treated as simple string - [typeof(string)] = () => new() { Type = "string" }, - [typeof(object)] = () => new() { Type = "object" } + [typeof(Uri)] = () => new() { Type = JsonSchemaType.String, Format = "uri" }, // Uri is treated as simple string + [typeof(string)] = () => new() { Type = JsonSchemaType.String }, + [typeof(object)] = () => new() { Type = JsonSchemaType.Object } }; /// @@ -79,7 +121,7 @@ public static OpenApiSchema MapTypeToOpenApiPrimitiveType(this Type type) return _simpleTypeToOpenApiSchema.TryGetValue(type, out var result) ? result() - : new() { Type = "string" }; + : new() { Type = JsonSchemaType.String }; } /// @@ -95,7 +137,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema throw new ArgumentNullException(nameof(schema)); } - var type = (schema.Type?.ToString().ToLowerInvariant(), schema.Format?.ToLowerInvariant(), schema.Nullable) switch + var type = (schema.Type.ToIdentifier(), schema.Format?.ToLowerInvariant(), schema.Nullable) switch { ("boolean", null, false) => typeof(bool), // integer is technically not valid with format, but we must provide some compatibility diff --git a/src/Microsoft.OpenApi/Models/JsonSchemaType.cs b/src/Microsoft.OpenApi/Models/JsonSchemaType.cs new file mode 100644 index 000000000..6024aa21b --- /dev/null +++ b/src/Microsoft.OpenApi/Models/JsonSchemaType.cs @@ -0,0 +1,49 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System; + +namespace Microsoft.OpenApi.Models +{ + /// + /// Represents the type of a JSON schema. + /// + [Flags] + public enum JsonSchemaType + { + /// + /// Represents a null type. + /// + Null = 1, + + /// + /// Represents a boolean type. + /// + Boolean = 2, + + /// + /// Represents an integer type. + /// + Integer = 4, + + /// + /// Represents a number type. + /// + Number = 8, + + /// + /// Represents a string type. + /// + String = 16, + + /// + /// Represents an object type. + /// + Object = 32, + + /// + /// Represents an array type. + /// + Array = 64, + } +} diff --git a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs index be68e122a..f3eb6c76f 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; @@ -292,7 +292,7 @@ public virtual void SerializeAsV2(IOpenApiWriter writer) } // In V2 parameter's type can't be a reference to a custom object schema or can't be of type object // So in that case map the type as string. - else if (Schema?.UnresolvedReference == true || "object".Equals(Schema?.Type?.ToString(), StringComparison.OrdinalIgnoreCase)) + else if (Schema?.UnresolvedReference == true || Schema?.Type == JsonSchemaType.Object) { writer.WriteProperty(OpenApiConstants.Type, "string"); } @@ -333,7 +333,7 @@ public virtual void SerializeAsV2(IOpenApiWriter writer) // allowEmptyValue writer.WriteProperty(OpenApiConstants.AllowEmptyValue, AllowEmptyValue, false); - if (this.In == ParameterLocation.Query && "array".Equals(Schema?.Type.ToString(), StringComparison.OrdinalIgnoreCase)) + if (this.In == ParameterLocation.Query && Schema?.Type == JsonSchemaType.Array) { if (this.Style == ParameterStyle.Form && this.Explode == true) { diff --git a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs index b35619a2c..037e7d92c 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.OpenApi.Any; +using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Writers; @@ -141,11 +142,11 @@ internal IEnumerable ConvertToFormDataParameters() foreach (var property in Content.First().Value.Schema.Properties) { var paramSchema = property.Value; - if ("string".Equals(paramSchema.Type.ToString(), StringComparison.OrdinalIgnoreCase) + if ("string".Equals(paramSchema.Type.ToIdentifier(), StringComparison.OrdinalIgnoreCase) && ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase) || "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase))) { - paramSchema.Type = "file"; + paramSchema.Type = "file".ToJsonSchemaType(); paramSchema.Format = null; } yield return new() diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index 4b10a089d..59b7e2025 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Text.Json.Nodes; +using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Helpers; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Writers; @@ -80,7 +81,7 @@ public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiRe /// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00 /// Value MUST be a string in V2 and V3. /// - public virtual object Type { get; set; } + public virtual JsonSchemaType? Type { get; set; } /// /// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00 @@ -355,7 +356,7 @@ public OpenApiSchema(OpenApiSchema schema) UnevaluatedProperties = schema?.UnevaluatedProperties ?? UnevaluatedProperties; V31ExclusiveMaximum = schema?.V31ExclusiveMaximum ?? V31ExclusiveMaximum; V31ExclusiveMinimum = schema?.V31ExclusiveMinimum ?? V31ExclusiveMinimum; - Type = DeepCloneType(schema?.Type); + Type = schema?.Type ?? Type; Format = schema?.Format ?? Format; Description = schema?.Description ?? Description; Maximum = schema?.Maximum ?? Maximum; @@ -475,7 +476,10 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version, writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (nodeWriter, s) => nodeWriter.WriteAny(s)); // type - SerializeTypeProperty(Type, writer, version); + if (Type is not null) + { + SerializeTypeProperty(Type, writer, version); + } // allOf writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, callback); @@ -576,7 +580,7 @@ internal void WriteV31Properties(IOpenApiWriter writer) internal void WriteAsItemsProperties(IOpenApiWriter writer) { // type - writer.WriteProperty(OpenApiConstants.Type, (string)Type); + writer.WriteProperty(OpenApiConstants.Type, Type.ToIdentifier()); // format if (string.IsNullOrEmpty(Format)) @@ -656,13 +660,9 @@ internal void SerializeAsV2( writer.WriteStartObject(); // type - if (Type is string[] array) - { - DowncastTypeArrayToV2OrV3(array, writer, OpenApiSpecVersion.OpenApi2_0); - } - else + if (Type is not null) { - writer.WriteProperty(OpenApiConstants.Type, (string)Type); + SerializeTypeProperty(Type, writer, OpenApiSpecVersion.OpenApi2_0); } // description @@ -792,60 +792,85 @@ internal void SerializeAsV2( writer.WriteEndObject(); } - private void SerializeTypeProperty(object type, IOpenApiWriter writer, OpenApiSpecVersion version) + private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer, OpenApiSpecVersion version) { - if (type?.GetType() == typeof(string)) + var flagsCount = CountEnumSetFlags(type); + if (flagsCount is 1) { // check whether nullable is true for upcasting purposes - if (Nullable || Extensions.ContainsKey(OpenApiConstants.NullableExtension)) + if (version is OpenApiSpecVersion.OpenApi3_1 && (Nullable || Extensions.ContainsKey(OpenApiConstants.NullableExtension))) { - // create a new array and insert the type and "null" as values - Type = new[] { (string)Type, OpenApiConstants.Null }; + UpCastSchemaTypeToV31(type, writer); } else { - writer.WriteProperty(OpenApiConstants.Type, (string)Type); + writer.WriteProperty(OpenApiConstants.Type, type.ToIdentifier()); } } - if (Type is string[] array) + else if(flagsCount > 1) { // type - if (version is OpenApiSpecVersion.OpenApi3_0) + if (version is OpenApiSpecVersion.OpenApi2_0 || version is OpenApiSpecVersion.OpenApi3_0) { - DowncastTypeArrayToV2OrV3(array, writer, OpenApiSpecVersion.OpenApi3_0); + DowncastTypeArrayToV2OrV3(type, writer, version, flagsCount); } else { - writer.WriteOptionalCollection(OpenApiConstants.Type, (string[])Type, (w, s) => w.WriteRaw(s)); + if (type is not null) + { + var list = new List(); + foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) + { + if (type.Value.HasFlag(flag)) + { + list.Add(flag); + } + } + + writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s.ToIdentifier())); + } } - } + } } - private object DeepCloneType(object type) + private static int CountEnumSetFlags(JsonSchemaType? schemaType) { - if (type == null) - return null; + int count = 0; - if (type is string) + if (schemaType != null) { - return type; // Return the string as is - } + // Check each flag in the enum + foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType))) + { + // Check if the flag is set + if (schemaType.Value.HasFlag(value)) + { + count++; + } + } + } + + return count; + } - if (type is Array array) + private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer) + { + // create a new array and insert the type and "null" as values + Type = type | JsonSchemaType.Null; + var list = new List(); + foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType))) { - Type elementType = type.GetType().GetElementType(); - Array copiedArray = Array.CreateInstance(elementType, array.Length); - for (int i = 0; i < array?.Length; i++) + // Check if the flag is set in 'type' using a bitwise AND operation + if (Type.Value.HasFlag(flag)) { - copiedArray.SetValue(DeepCloneType(array?.GetValue(i)), i); + list.Add(flag.ToIdentifier()); } - return copiedArray; } - return null; + writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s)); } - private void DowncastTypeArrayToV2OrV3(string[] array, IOpenApiWriter writer, OpenApiSpecVersion version) + private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWriter writer, OpenApiSpecVersion version, int flagsCount) { /* If the array has one non-null value, emit Type as string * If the array has one null value, emit x-nullable as true @@ -853,27 +878,32 @@ private void DowncastTypeArrayToV2OrV3(string[] array, IOpenApiWriter writer, Op * If the array has more than two values or two non-null values, do not emit type * */ - var nullableProp = version.Equals(OpenApiSpecVersion.OpenApi2_0) + var nullableProp = version.Equals(OpenApiSpecVersion.OpenApi2_0) ? OpenApiConstants.NullableExtension : OpenApiConstants.Nullable; - if (array.Length is 1) + if (flagsCount is 1) { - var value = array[0]; - if (value is OpenApiConstants.Null) + if (schemaType is JsonSchemaType.Null) { writer.WriteProperty(nullableProp, true); } else { - writer.WriteProperty(OpenApiConstants.Type, value); + writer.WriteProperty(OpenApiConstants.Type, schemaType.ToIdentifier()); } } - else if (array.Length is 2 && array.Contains(OpenApiConstants.Null)) + else if (flagsCount is 2 && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null { - // Find the non-null value and write it out - var nonNullValue = array.First(v => v != OpenApiConstants.Null); - writer.WriteProperty(OpenApiConstants.Type, nonNullValue); + foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType))) + { + // Skip if the flag is not set or if it's the Null flag + if (schemaType.Value.HasFlag(flag) && flag != JsonSchemaType.Null) + { + // Write the non-null flag value to the writer + writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier()); + } + } if (!Nullable) { writer.WriteProperty(nullableProp, true); diff --git a/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs b/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs index 759959344..a7b55e109 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs @@ -88,7 +88,7 @@ internal OpenApiSchemaReference(OpenApiSchema target, string referenceId) /// public override bool UnEvaluatedProperties { get => Target.UnEvaluatedProperties; set => Target.UnEvaluatedProperties = value; } /// - public override object Type { get => Target.Type; set => Target.Type = value; } + public override JsonSchemaType? Type { get => Target.Type; set => Target.Type = value; } /// public override string Format { get => Target.Format; set => Target.Format = value; } /// diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs index 5667b8f98..b4ddf7300 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; @@ -24,7 +24,7 @@ internal static partial class OpenApiV2Deserializer }, { "type", - (o, n, _) => GetOrCreateSchema(o).Type = n.GetScalarValue() + (o, n, _) => GetOrCreateSchema(o).Type = n.GetScalarValue().ToJsonSchemaType() }, { "format", diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs index 67e6ecca5..d65f7a16b 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs @@ -173,8 +173,8 @@ private static OpenApiRequestBody CreateFormBody(ParsingContext context, List mediaType) }; - foreach (var value in formBody.Content.Values.Where(static x => x.Schema is not null && x.Schema.Properties.Any() && string.IsNullOrEmpty((string)x.Schema.Type))) - value.Schema.Type = "object"; + foreach (var value in formBody.Content.Values.Where(static x => x.Schema is not null && x.Schema.Properties.Any() && x.Schema.Type == null)) + value.Schema.Type = JsonSchemaType.Object; return formBody; } diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs index 60167f891..149c00fd3 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; @@ -46,7 +46,7 @@ internal static partial class OpenApiV2Deserializer }, { "type", - (o, n, t) => GetOrCreateSchema(o).Type = n.GetScalarValue() + (o, n, t) => GetOrCreateSchema(o).Type = n.GetScalarValue().ToJsonSchemaType() }, { "items", diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs index 66c45c641..53208fd40 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs @@ -85,7 +85,7 @@ internal static partial class OpenApiV2Deserializer { "type", - (o, n, _) => o.Type = n.GetScalarValue() + (o, n, _) => o.Type = n.GetScalarValue().ToJsonSchemaType() }, { "allOf", diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs index 2dd2e4f6a..f3c02a6c8 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs @@ -84,7 +84,7 @@ internal static partial class OpenApiV3Deserializer }, { "type", - (o, n, _) => o.Type = n.GetScalarValue() + (o, n, _) => o.Type = n.GetScalarValue().ToJsonSchemaType() }, { "allOf", diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs index 05ce240c9..5dc76b7fb 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs @@ -114,9 +114,21 @@ internal static partial class OpenApiV31Deserializer "type", (o, n, _) => { - o.Type = n is ValueNode - ? n.GetScalarValue() - : n.CreateSimpleList((n2, p) => n2.GetScalarValue()).ToArray(); + if (n is ValueNode) + { + o.Type = n.GetScalarValue().ToJsonSchemaType(); + } + else + { + var list = n.CreateSimpleList((n2, p) => n2.GetScalarValue()); + JsonSchemaType combinedType = 0; + foreach(var type in list) + { + var schemaType = type.ToJsonSchemaType(); + combinedType |= schemaType; + } + o.Type = combinedType; + } } }, { @@ -179,15 +191,7 @@ internal static partial class OpenApiV31Deserializer var nullable = bool.Parse(n.GetScalarValue()); if (nullable) // if nullable, convert type into an array of type(s) and null { - if (o.Type is string[] typeArray) - { - var typeList = new List(typeArray) { OpenApiConstants.Null }; - o.Type = typeList.ToArray(); - } - else if (o.Type is string typeString) - { - o.Type = new string[]{typeString, OpenApiConstants.Null}; - } + o.Type |= JsonSchemaType.Null; } } }, @@ -251,8 +255,8 @@ public static OpenApiSchema LoadSchema(ParseNode node, OpenApiDocument hostDocum if (schema.Extensions.ContainsKey(OpenApiConstants.NullableExtension)) { - var type = schema.Type; - schema.Type = new string[] {(string)type, OpenApiConstants.Null}; + var type = schema.Type; + schema.Type = type | JsonSchemaType.Null; schema.Extensions.Remove(OpenApiConstants.NullableExtension); } diff --git a/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs b/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs index 5e15b1ead..4e05c44fd 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs @@ -3,6 +3,7 @@ using System.Text.Json; using System.Text.Json.Nodes; +using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Models; namespace Microsoft.OpenApi.Validations.Rules @@ -53,7 +54,7 @@ public static void ValidateDataTypeMismatch( // convert value to JsonElement and access the ValueKind property to determine the type. var jsonElement = JsonDocument.Parse(JsonSerializer.Serialize(value)).RootElement; - var type = (string)schema.Type; + var type = schema.Type.ToIdentifier(); var format = schema.Format; var nullable = schema.Nullable; diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Formatters/PowerShellFormatterTests.cs b/test/Microsoft.OpenApi.Hidi.Tests/Formatters/PowerShellFormatterTests.cs index 214bd47ff..110cac88c 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Formatters/PowerShellFormatterTests.cs +++ b/test/Microsoft.OpenApi.Hidi.Tests/Formatters/PowerShellFormatterTests.cs @@ -66,11 +66,11 @@ public void RemoveAnyOfAndOneOfFromSchema() Assert.NotNull(openApiDocument.Components.Schemas); Assert.NotNull(testSchema); Assert.Null(averageAudioDegradationProperty?.AnyOf); - Assert.Equal("number", averageAudioDegradationProperty?.Type); + Assert.Equal(JsonSchemaType.Number, averageAudioDegradationProperty?.Type); Assert.Equal("float", averageAudioDegradationProperty?.Format); Assert.True(averageAudioDegradationProperty?.Nullable); Assert.Null(defaultPriceProperty?.OneOf); - Assert.Equal("number", defaultPriceProperty?.Type); + Assert.Equal(JsonSchemaType.Number, defaultPriceProperty?.Type); Assert.Equal("double", defaultPriceProperty?.Format); Assert.NotNull(testSchema.AdditionalProperties); } @@ -91,7 +91,7 @@ public void ResolveFunctionParameters() // Assert Assert.Null(idsParameter?.Content); Assert.NotNull(idsParameter?.Schema); - Assert.Equal("array", idsParameter?.Schema.Type); + Assert.Equal(JsonSchemaType.Array, idsParameter?.Schema.Type); } private static OpenApiDocument GetSampleOpenApiDocument() @@ -123,10 +123,10 @@ private static OpenApiDocument GetSampleOpenApiDocument() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -152,7 +152,7 @@ private static OpenApiDocument GetSampleOpenApiDocument() { { "TestSchema", new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { { @@ -160,8 +160,8 @@ private static OpenApiDocument GetSampleOpenApiDocument() { AnyOf = new List { - new() { Type = "number" }, - new() { Type = "string" } + new() { Type = JsonSchemaType.Number }, + new() { Type = JsonSchemaType.String } }, Format = "float", Nullable = true @@ -172,8 +172,8 @@ private static OpenApiDocument GetSampleOpenApiDocument() { OneOf = new List { - new() { Type = "number", Format = "double" }, - new() { Type = "string" } + new() { Type = JsonSchemaType.Number, Format = "double" }, + new() { Type = JsonSchemaType.String } } } } diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs index 99e559e37..3bd9efd2a 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs +++ b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs @@ -131,7 +131,7 @@ public void CreateFilteredDocumentUsingPredicateFromRequestUrl() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } diff --git a/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs b/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs index 98ed181f4..91dd59919 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs +++ b/test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs @@ -85,7 +85,7 @@ public static OpenApiDocument CreateOpenApiDocument() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -104,7 +104,7 @@ public static OpenApiDocument CreateOpenApiDocument() { Schema = new() { - Type = "array" + Type = JsonSchemaType.Array } } } @@ -125,7 +125,7 @@ public static OpenApiDocument CreateOpenApiDocument() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -159,7 +159,7 @@ public static OpenApiDocument CreateOpenApiDocument() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -178,7 +178,7 @@ public static OpenApiDocument CreateOpenApiDocument() { Schema = new() { - Type = "array" + Type = JsonSchemaType.Array } } } @@ -198,7 +198,7 @@ public static OpenApiDocument CreateOpenApiDocument() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -236,14 +236,14 @@ public static OpenApiDocument CreateOpenApiDocument() Schema = new() { Title = "Collection of user", - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { { "value", new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { Reference = new() @@ -368,7 +368,7 @@ public static OpenApiDocument CreateOpenApiDocument() Description = "Select properties to be returned", Schema = new() { - Type = "array" + Type = JsonSchemaType.Array } // missing explode parameter } @@ -432,7 +432,7 @@ public static OpenApiDocument CreateOpenApiDocument() Description = "key: id of administrativeUnit", Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -455,7 +455,7 @@ public static OpenApiDocument CreateOpenApiDocument() { new() { - Type = "string" + Type = JsonSchemaType.String } }, Nullable = true @@ -534,14 +534,14 @@ public static OpenApiDocument CreateOpenApiDocument() Schema = new() { Title = "Collection of hostSecurityProfile", - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { { "value", new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { Reference = new() @@ -592,7 +592,7 @@ public static OpenApiDocument CreateOpenApiDocument() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String }, Extensions = new Dictionary { @@ -647,7 +647,7 @@ public static OpenApiDocument CreateOpenApiDocument() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String }, Extensions = new Dictionary { @@ -664,7 +664,7 @@ public static OpenApiDocument CreateOpenApiDocument() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String }, Extensions = new Dictionary { @@ -688,7 +688,7 @@ public static OpenApiDocument CreateOpenApiDocument() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Reference = new() { Type = ReferenceType.Schema, @@ -740,13 +740,13 @@ public static OpenApiDocument CreateOpenApiDocument() "microsoft.graph.networkInterface", new OpenApiSchema { Title = "networkInterface", - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { { "description", new OpenApiSchema { - Type = "string", + Type = JsonSchemaType.String, Description = "Description of the NIC (e.g. Ethernet adapter, Wireless LAN adapter Local Area Connection <#>, etc.).", Nullable = true } diff --git a/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs b/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs index 010604750..d6fb3b8ba 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/ReferenceService/TryLoadReferenceV2Tests.cs @@ -38,7 +38,7 @@ public void LoadParameterReference() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } @@ -106,11 +106,11 @@ public void LoadResponseAndSchemaReference() Properties = { ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String } }, diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs index 8af3f1f3c..596269644 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs @@ -75,12 +75,12 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture) { ["sampleSchema"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["sampleProperty"] = new() { - Type = "double", + Type = JsonSchemaType.Number, Minimum = (decimal)100.54, Maximum = (decimal)60000000.35, ExclusiveMaximum = true, @@ -119,7 +119,7 @@ public void ShouldParseProducesInAnyOrder() { { "id", new OpenApiSchema { - Type = "string", + Type = JsonSchemaType.String, Description = "Item identifier." } } @@ -132,18 +132,18 @@ public void ShouldParseProducesInAnyOrder() { { "code", new OpenApiSchema { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } }, { "message", new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } }, { "fields", new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } } } @@ -153,7 +153,7 @@ public void ShouldParseProducesInAnyOrder() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new OpenApiSchemaReference("Item", result.OpenApiDocument) } }; @@ -277,7 +277,7 @@ public void ShouldAssignSchemaToAllResponses() var successSchema = new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Items = new OpenApiSchemaReference("Item", result.OpenApiDocument) }; var errorSchema = new OpenApiSchemaReference("Error", result.OpenApiDocument); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiHeaderTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiHeaderTests.cs index 6a2411237..80948f93b 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiHeaderTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiHeaderTests.cs @@ -36,7 +36,7 @@ public void ParseHeaderWithDefaultShouldSucceed() { Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float", Default = new OpenApiAny(5).Node } @@ -65,7 +65,7 @@ public void ParseHeaderWithEnumShouldSucceed() { Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float", Enum = { diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs index 595631e29..4142e9fcd 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs @@ -39,7 +39,7 @@ public class OpenApiOperationTests Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -73,7 +73,7 @@ public class OpenApiOperationTests Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -85,18 +85,18 @@ public class OpenApiOperationTests { Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["name"] = new() { Description = "Updated name of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["status"] = new() { Description = "Updated status of the pet", - Type = "string" + Type = JsonSchemaType.String } }, Required = new HashSet @@ -109,18 +109,18 @@ public class OpenApiOperationTests { Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["name"] = new() { Description = "Updated name of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["status"] = new() { Description = "Updated status of the pet", - Type = "string" + Type = JsonSchemaType.String } }, Required = new HashSet @@ -170,7 +170,7 @@ public class OpenApiOperationTests Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }, }, @@ -184,7 +184,7 @@ public class OpenApiOperationTests { Schema = new() { - Type = "object" + Type = JsonSchemaType.Object } } }, @@ -314,10 +314,10 @@ public void ParseOperationWithResponseExamplesShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float" } }, @@ -332,10 +332,10 @@ public void ParseOperationWithResponseExamplesShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float" } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs index e9eeaa054..0b4b1a77e 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs @@ -59,7 +59,7 @@ public void ParsePathParameterShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -87,10 +87,10 @@ public void ParseQueryParameterShouldSucceed() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } }, Style = ParameterStyle.Form, @@ -121,7 +121,7 @@ public void ParseParameterWithNullLocationShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -149,7 +149,7 @@ public void ParseParameterWithNoLocationShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -201,7 +201,7 @@ public void ParseParameterWithUnknownLocationShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -229,7 +229,7 @@ public void ParseParameterWithDefaultShouldSucceed() Required = true, Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float", Default = new OpenApiAny(5).Node } @@ -256,7 +256,7 @@ public void ParseParameterWithEnumShouldSucceed() Required = true, Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float", Enum = { diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiPathItemTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiPathItemTests.cs index ef85cd712..47f3903fa 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiPathItemTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiPathItemTests.cs @@ -30,10 +30,10 @@ public class OpenApiPathItemTests Required = true, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } }, Style = ParameterStyle.Simple @@ -56,7 +56,7 @@ public class OpenApiPathItemTests Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -68,18 +68,18 @@ public class OpenApiPathItemTests { Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["name"] = new() { Description = "Updated name of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["status"] = new() { Description = "Updated status of the pet", - Type = "string" + Type = JsonSchemaType.String } }, Required = new HashSet @@ -92,18 +92,18 @@ public class OpenApiPathItemTests { Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["name"] = new() { Description = "Updated name of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["status"] = new() { Description = "Updated status of the pet", - Type = "string" + Type = JsonSchemaType.String } }, Required = new HashSet @@ -151,7 +151,7 @@ public class OpenApiPathItemTests Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }, new() @@ -162,7 +162,7 @@ public class OpenApiPathItemTests Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -174,23 +174,23 @@ public class OpenApiPathItemTests { Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["name"] = new() { Description = "Updated name of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["status"] = new() { Description = "Updated status of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["skill"] = new() { Description = "Updated skill of the pet", - Type = "string" + Type = JsonSchemaType.String } }, Required = new HashSet @@ -203,23 +203,23 @@ public class OpenApiPathItemTests { Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["name"] = new() { Description = "Updated name of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["status"] = new() { Description = "Updated status of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["skill"] = new() { Description = "Updated skill of the pet", - Type = "string" + Type = JsonSchemaType.String } }, Required = new HashSet diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiSchemaTests.cs index 4c66a67f8..aee5aab7e 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiSchemaTests.cs @@ -35,7 +35,7 @@ public void ParseSchemaWithDefaultShouldSucceed() // Assert schema.Should().BeEquivalentTo(new OpenApiSchema { - Type = "number", + Type = JsonSchemaType.Number, Format = "float", Default = 5 }, options => options.IgnoringCyclicReferences().Excluding(x => x.Default.Parent)); @@ -58,7 +58,7 @@ public void ParseSchemaWithExampleShouldSucceed() schema.Should().BeEquivalentTo( new OpenApiSchema { - Type = "number", + Type = JsonSchemaType.Number, Format = "float", Example = 5 }, options => options.IgnoringCyclicReferences().Excluding(x => x.Example.Parent)); @@ -80,7 +80,7 @@ public void ParseSchemaWithEnumShouldSucceed() // Assert var expected = new OpenApiSchema { - Type = "number", + Type = JsonSchemaType.Number, Format = "float", Enum = new List { diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.ParseDocumentWith31PropertiesWorks.verified.txt b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.ParseDocumentWith31PropertiesWorks.verified.txt new file mode 100644 index 000000000..6c2f850fe --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.ParseDocumentWith31PropertiesWorks.verified.txt @@ -0,0 +1,110 @@ +openapi: '3.1.0' +jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema +info: + title: Sample OpenAPI 3.1 API + description: A sample API demonstrating OpenAPI 3.1 features + license: + name: Apache 2.0 + url: https://www.apache.org/licenses/LICENSE-2.0.html + identifier: Apache-2.0 + version: 2.0.0 + summary: Sample OpenAPI 3.1 API with the latest features +servers: + - url: https://api.example.com/v2 + description: Main production server +paths: + /pets: + get: + tags: + - pets + summary: List all pets + operationId: listPets + parameters: + - name: limit + in: query + description: How many items to return at one time (max 100) + schema: + exclusiveMaximum: 100 + exclusiveMinimum: 1 + type: integer + responses: + '200': + description: A paged array of pets + content: + application/json: + schema: + $ref: https://example.com/schemas/pet.json + /sample: + get: + summary: Sample endpoint + responses: + '200': + description: Sample response + content: + application/json: + schema: + $id: https://example.com/schemas/person.schema.yaml + $schema: https://json-schema.org/draft/2020-12/schema + $comment: A schema defining a pet object with optional references to dynamic components. + $vocabulary: + https://json-schema.org/draft/2020-12/vocab/core: true + https://json-schema.org/draft/2020-12/vocab/applicator: true + https://json-schema.org/draft/2020-12/vocab/validation: true + https://json-schema.org/draft/2020-12/vocab/meta-data: false + https://json-schema.org/draft/2020-12/vocab/format-annotation: false + $dynamicAnchor: addressDef + title: Pet + required: + - name + type: object + properties: + name: + $comment: The pet's full name + type: string + address: + $comment: Reference to an address definition which can change dynamically + $dynamicRef: '#addressDef' + description: Schema for a pet object +components: + schemas: + Pet: + $id: https://example.com/schemas/pet.json + $comment: This schema represents a pet in the system. + $defs: + ExtraInfo: + type: string + required: + - id + - weight + type: object + properties: + id: + type: string + format: uuid + weight: + exclusiveMinimum: 0 + type: number + description: Weight of the pet in kilograms + attributes: + patternProperties: + '^attr_[A-Za-z]+$': + type: string + type: + - 'null' + - object + description: Dynamic attributes for the pet +security: + - api_key: [ ] +webhooks: + newPetAlert: + post: + summary: Notify about a new pet being added + requestBody: + content: + application/json: + schema: + type: string + required: true + responses: + '200': + description: Webhook processed successfully \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs index c954387a6..638d69667 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs @@ -12,6 +12,7 @@ using Microsoft.OpenApi.Services; using Xunit; using System.Linq; +using VerifyXunit; namespace Microsoft.OpenApi.Readers.Tests.V31Tests { @@ -39,7 +40,7 @@ public void ParseDocumentWithWebhooksShouldSucceed() { ["petSchema"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "id", @@ -49,22 +50,22 @@ public void ParseDocumentWithWebhooksShouldSucceed() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["newPetSchema"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "name" @@ -73,16 +74,16 @@ public void ParseDocumentWithWebhooksShouldSucceed() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } } @@ -116,10 +117,10 @@ public void ParseDocumentWithWebhooksShouldSucceed() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -131,7 +132,7 @@ public void ParseDocumentWithWebhooksShouldSucceed() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -147,7 +148,7 @@ public void ParseDocumentWithWebhooksShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } }, @@ -155,7 +156,7 @@ public void ParseDocumentWithWebhooksShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } } @@ -215,7 +216,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { ["petSchema"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "id", @@ -225,22 +226,22 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["newPetSchema"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "name" @@ -249,16 +250,16 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } } @@ -290,10 +291,10 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -305,7 +306,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -321,7 +322,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { Schema = new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } }, @@ -329,7 +330,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { Schema = new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } } @@ -423,27 +424,27 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks() var expectedSchema = new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { ["prop1"] = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String }, ["prop2"] = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String }, ["prop3"] = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } }, PatternProperties = new Dictionary { ["^x-.*$"] = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } } }; @@ -482,9 +483,9 @@ public void ParseDocumentWithReferenceByIdGetsResolved() var parameterSchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters[0].Schema; // Assert - Assert.Equal("object", responseSchema.Type); - Assert.Equal("object", requestBodySchema.Type); - Assert.Equal("string", parameterSchema.Type); + Assert.Equal(JsonSchemaType.Object, responseSchema.Type); + Assert.Equal(JsonSchemaType.Object, requestBodySchema.Type); + Assert.Equal(JsonSchemaType.String, parameterSchema.Type); } [Fact] @@ -530,5 +531,19 @@ public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks() // Assert requestBodySchema.Properties.Count.Should().Be(2); // reference has been resolved } + + [Fact] + public async Task ParseDocumentWith31PropertiesWorks() + { + var path = Path.Combine(SampleFolderPath, "documentWith31Properties.yaml"); + var doc = OpenApiDocument.Load(path).OpenApiDocument; + var outputStringWriter = new StringWriter(); + doc.SerializeAsV31(new OpenApiYamlWriter(outputStringWriter)); + outputStringWriter.Flush(); + var actual = outputStringWriter.GetStringBuilder().ToString(); + + // Assert + await Verifier.Verify(actual); + } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs index 6df59331e..967bb0f3e 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs @@ -31,27 +31,27 @@ public void ParseBasicV31SchemaShouldSucceed() Id = "https://example.com/arrays.schema.json", Schema = "https://json-schema.org/draft/2020-12/schema", Description = "A representation of a person, company, organization, or place", - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { ["fruits"] = new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Items = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } }, ["vegetables"] = new OpenApiSchema { - Type = "array" + Type = JsonSchemaType.Array } }, Definitions = new Dictionary { ["veggie"] = new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "veggieName", @@ -61,12 +61,12 @@ public void ParseBasicV31SchemaShouldSucceed() { ["veggieName"] = new OpenApiSchema { - Type = "string", + Type = JsonSchemaType.String, Description = "The name of the vegetable." }, ["veggieLike"] = new OpenApiSchema { - Type = "boolean", + Type = JsonSchemaType.Boolean, Description = "Do I like this vegetable?" } } @@ -98,7 +98,7 @@ public void ParseSchemaWithTypeArrayWorks() Id = "https://example.com/arrays.schema.json", Schema = "https://json-schema.org/draft/2020-12/schema", Description = "A representation of a person, company, organization, or place", - Type = new string[] { "object", "null" } + Type = JsonSchemaType.Object | JsonSchemaType.Null }; // Act @@ -116,49 +116,51 @@ public void TestSchemaCopyConstructorWithTypeArrayWorks() */ var schemaWithTypeArray = new OpenApiSchema() { - Type = new string[] { "array", "null" }, + Type = JsonSchemaType.Array | JsonSchemaType.Null, Items = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } }; var simpleSchema = new OpenApiSchema() { - Type = "string" + Type = JsonSchemaType.String }; // Act var schemaWithArrayCopy = new OpenApiSchema(schemaWithTypeArray); - schemaWithArrayCopy.Type = "string"; + schemaWithArrayCopy.Type = JsonSchemaType.String; - var simpleSchemaCopy = new OpenApiSchema(simpleSchema); - simpleSchemaCopy.Type = new string[] { "string", "null" }; + var simpleSchemaCopy = new OpenApiSchema(simpleSchema) + { + Type = JsonSchemaType.String | JsonSchemaType.Null + }; // Assert - schemaWithArrayCopy.Type.Should().NotBeEquivalentTo(schemaWithTypeArray.Type); - schemaWithTypeArray.Type = new string[] { "string", "null" }; + schemaWithArrayCopy.Type.Should().NotBe(schemaWithTypeArray.Type); + schemaWithTypeArray.Type = JsonSchemaType.String | JsonSchemaType.Null; - simpleSchemaCopy.Type.Should().NotBeEquivalentTo(simpleSchema.Type); - simpleSchema.Type = "string"; + simpleSchemaCopy.Type.Should().NotBe(simpleSchema.Type); + simpleSchema.Type = JsonSchemaType.String; } [Fact] public void ParseV31SchemaShouldSucceed() { - var path = System.IO.Path.Combine(SampleFolderPath, "schema.yaml"); + var path = Path.Combine(SampleFolderPath, "schema.yaml"); // Act var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); var expectedSchema = new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { ["one"] = new() { Description = "type array", - Type = new HashSet { "integer", "string" } + Type = JsonSchemaType.Integer | JsonSchemaType.String } } }; @@ -171,38 +173,38 @@ public void ParseV31SchemaShouldSucceed() public void ParseAdvancedV31SchemaShouldSucceed() { // Arrange and Act - var path = System.IO.Path.Combine(SampleFolderPath, "advancedSchema.yaml"); + var path = Path.Combine(SampleFolderPath, "advancedSchema.yaml"); var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); var expectedSchema = new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { ["one"] = new() { Description = "type array", - Type = new HashSet { "integer", "string" } + Type = JsonSchemaType.Integer | JsonSchemaType.String }, ["two"] = new() { Description = "type 'null'", - Type = "null" + Type = JsonSchemaType.Null }, ["three"] = new() { Description = "type array including 'null'", - Type = new HashSet { "string", "null" } + Type = JsonSchemaType.String | JsonSchemaType.Null }, ["four"] = new() { Description = "array with no items", - Type = "array" + Type = JsonSchemaType.Array }, ["five"] = new() { Description = "singular example", - Type = "string", + Type = JsonSchemaType.String, Examples = new List { "exampleValue" @@ -231,12 +233,12 @@ public void ParseAdvancedV31SchemaShouldSucceed() ["ten"] = new() { Description = "nullable string", - Type = new HashSet { "string", "null" } + Type = JsonSchemaType.String | JsonSchemaType.Null }, ["eleven"] = new() { Description = "x-nullable string", - Type = new HashSet { "string", "null" } + Type = JsonSchemaType.String | JsonSchemaType.Null }, ["twelve"] = new() { @@ -275,7 +277,7 @@ public void CloningSchemaWithExamplesAndEnumsShouldSucceed() // Arrange var schema = new OpenApiSchema { - Type = "int", + Type = JsonSchemaType.Integer, Default = 5, Examples = [2, 3], Enum = [1, 2, 3] @@ -335,8 +337,8 @@ public void SerializeV3SchemaWithNullableAsV31Works() { // Arrange var expected = @"type: - - string - - null"; + - 'null' + - string"; var path = Path.Combine(SampleFolderPath, "schemaWithNullable.yaml"); @@ -355,8 +357,8 @@ public void SerializeV2SchemaWithNullableExtensionAsV31Works() { // Arrange var expected = @"type: + - 'null' - string - - null x-nullable: true"; var path = Path.Combine(SampleFolderPath, "schemaWithNullableExtension.yaml"); @@ -402,7 +404,7 @@ public void LoadSchemaWithNullableExtensionAsV31Works(string filePath) var schema = OpenApiModelFactory.Load(path, OpenApiSpecVersion.OpenApi3_1, out _); // Assert - schema.Type.Should().BeEquivalentTo(new string[] { "string", "null" }); + schema.Type.Should().Be(JsonSchemaType.String | JsonSchemaType.Null); } [Fact] diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/documentWith31Properties.yaml b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/documentWith31Properties.yaml new file mode 100644 index 000000000..41817174e --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/documentWith31Properties.yaml @@ -0,0 +1,122 @@ +openapi: 3.1.0 +info: + title: Sample OpenAPI 3.1 API + description: A sample API demonstrating OpenAPI 3.1 features + version: "2.0.0" + summary: Sample OpenAPI 3.1 API with the latest features # OpenAPI 3.1 feature + license: + name: Apache 2.0 + identifier: Apache-2.0 # SPDX license identifier, a new 3.1 feature to define an API's SPDX license expression + url: https://www.apache.org/licenses/LICENSE-2.0.html + +# JSON Schema 2020-12 feature +jsonSchemaDialect: "https://json-schema.org/draft/2020-12/schema" + +servers: + - url: https://api.example.com/v2 + description: Main production server + +# Example Webhooks (OpenAPI 3.1 feature) +webhooks: + newPetAlert: + post: + summary: Notify about a new pet being added + requestBody: + required: true + content: + application/json: + schema: + type: string + responses: + '200': + description: Webhook processed successfully +paths: + /pets: + get: + summary: List all pets + operationId: listPets + tags: + - pets + parameters: + - name: limit + in: query + description: How many items to return at one time (max 100) + required: false + schema: + type: integer + #exclusiveMinimum and exclusiveMaximum now represent distinct values + exclusiveMinimum: 1 + exclusiveMaximum: 100 + responses: + '200': + description: A paged array of pets + content: + application/json: + schema: + # 3.1 feature where we can reference schemas using their identifier + $ref: 'https://example.com/schemas/pet.json' + /sample: + get: + summary: Sample endpoint + responses: + '200': + description: Sample response + content: + application/json: + schema: + #JSON schema keywords + $schema: "https://json-schema.org/draft/2020-12/schema" + $id: "https://example.com/schemas/person.schema.yaml" + $comment: "A schema defining a pet object with optional references to dynamic components." + $vocabulary: + "https://json-schema.org/draft/2020-12/vocab/core": true + "https://json-schema.org/draft/2020-12/vocab/applicator": true + "https://json-schema.org/draft/2020-12/vocab/validation": true + "https://json-schema.org/draft/2020-12/vocab/meta-data": false + "https://json-schema.org/draft/2020-12/vocab/format-annotation": false + + title: "Pet" + description: "Schema for a pet object" + type: "object" + properties: + name: + type: "string" + $comment: "The pet's full name" + address: + $dynamicRef: "#addressDef" + $comment: "Reference to an address definition which can change dynamically" + required: + - name + $dynamicAnchor: "addressDef" +components: + schemas: + Pet: + $id: 'https://example.com/schemas/pet.json' + type: object + required: + - id + - weight + properties: + id: + type: string + format: uuid + weight: + type: number + exclusiveMinimum: 0 + description: Weight of the pet in kilograms + # Pattern properties and Type array feature from JSON Schema + attributes: + type: + - "object" + - "null" + description: Dynamic attributes for the pet + patternProperties: + "^attr_[A-Za-z]+$": + type: string + $comment: "This schema represents a pet in the system." # JSON Schema 2020-12 feature + $defs: # JSON Schema 2020-12 feature + ExtraInfo: + type: string + +security: + - api_key: [] \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs index 544fec90b..cab621c14 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiCallbackTests.cs @@ -97,7 +97,7 @@ public void ParseCallbackWithReferenceShouldSucceed() { Schema = new() { - Type = "object" + Type = JsonSchemaType.Object } } } @@ -153,7 +153,7 @@ public void ParseMultipleCallbacksWithReferenceShouldSucceed() { Schema = new() { - Type = "object" + Type = JsonSchemaType.Object } } } @@ -195,7 +195,7 @@ public void ParseMultipleCallbacksWithReferenceShouldSucceed() { Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -230,7 +230,7 @@ public void ParseMultipleCallbacksWithReferenceShouldSucceed() { Schema = new() { - Type = "object" + Type = JsonSchemaType.Object } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs index 314e22273..712fea099 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs @@ -216,7 +216,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed() { ["pet1"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "id", @@ -226,22 +226,22 @@ public void ParseStandardPetStoreDocumentShouldSucceed() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["newPet"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "name" @@ -250,22 +250,22 @@ public void ParseStandardPetStoreDocumentShouldSucceed() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["errorModel"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "code", @@ -275,12 +275,12 @@ public void ParseStandardPetStoreDocumentShouldSucceed() { ["code"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" }, ["message"] = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -340,10 +340,10 @@ public void ParseStandardPetStoreDocumentShouldSucceed() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -355,7 +355,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -371,7 +371,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } }, @@ -379,7 +379,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } } @@ -483,7 +483,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -543,7 +543,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -602,7 +602,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { ["pet1"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "id", @@ -612,22 +612,22 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["newPet"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "name" @@ -636,22 +636,22 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["errorModel"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "code", @@ -661,12 +661,12 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { ["code"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" }, ["message"] = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -805,10 +805,10 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -820,7 +820,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -836,7 +836,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } }, @@ -844,7 +844,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } } @@ -965,7 +965,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -1025,7 +1025,7 @@ public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -1144,7 +1144,7 @@ public void HeaderParameterShouldAllowExample() Example = "99391c7e-ad88-49ec-a2ad-99ddcb1f7721", Schema = new() { - Type = "string", + Type = JsonSchemaType.String, Format = "uuid" }, }, options => options.IgnoringCyclicReferences() @@ -1178,7 +1178,7 @@ public void HeaderParameterShouldAllowExample() }, Schema = new() { - Type = "string", + Type = JsonSchemaType.String, Format = "uuid" }, }, options => options.IgnoringCyclicReferences() @@ -1268,7 +1268,7 @@ public void ParseDocWithRefsUsingProxyReferencesSucceeds() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32", Default = 10 }, @@ -1296,7 +1296,7 @@ public void ParseDocWithRefsUsingProxyReferencesSucceeds() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32", Default = 10 }, diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs index 01239e415..eaf802d8c 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiEncodingTests.cs @@ -54,7 +54,7 @@ public void ParseAdvancedEncodingShouldSucceed() Description = "The number of allowed requests in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs index 2c368cc22..26de35edb 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs @@ -36,7 +36,7 @@ public void ParseMediaTypeWithExampleShouldSucceed() Example = 5, Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float" } }, options => options.IgnoringCyclicReferences() @@ -67,7 +67,7 @@ public void ParseMediaTypeWithExamplesShouldSucceed() }, Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float" } }, options => options.IgnoringCyclicReferences() diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs index d6570f17b..9ba96bbda 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiOperationTests.cs @@ -54,7 +54,7 @@ public void ParseOperationWithParameterWithNoLocationShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }, new OpenApiParameter @@ -65,7 +65,7 @@ public void ParseOperationWithParameterWithNoLocationShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs index 2ff60b388..e0f6460aa 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiParameterTests.cs @@ -43,7 +43,7 @@ public void ParsePathParameterShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -64,10 +64,10 @@ public void ParseQueryParameterShouldSucceed() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } }, Style = ParameterStyle.Form, @@ -89,10 +89,10 @@ public void ParseQueryParameterWithObjectTypeShouldSucceed() Name = "freeForm", Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, Style = ParameterStyle.Form @@ -120,7 +120,7 @@ public void ParseQueryParameterWithObjectTypeAndContentShouldSucceed() { Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = { "lat", @@ -130,11 +130,11 @@ public void ParseQueryParameterWithObjectTypeAndContentShouldSucceed() { ["lat"] = new() { - Type = "number" + Type = JsonSchemaType.Number }, ["long"] = new() { - Type = "number" + Type = JsonSchemaType.Number } } } @@ -161,10 +161,10 @@ public void ParseHeaderParameterShouldSucceed() Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64", } } @@ -187,7 +187,7 @@ public void ParseParameterWithNullLocationShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -211,7 +211,7 @@ public void ParseParameterWithNoLocationShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -235,7 +235,7 @@ public void ParseParameterWithUnknownLocationShouldSucceed() Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -257,7 +257,7 @@ public void ParseParameterWithExampleShouldSucceed() Example = (float)5.0, Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float" } }, options => options.IgnoringCyclicReferences().Excluding(p => p.Example.Parent)); @@ -290,7 +290,7 @@ public void ParseParameterWithExamplesShouldSucceed() }, Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "float" } }, options => options.IgnoringCyclicReferences() @@ -352,10 +352,10 @@ public void ParseParameterWithReferenceWorks() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } }, Reference = new OpenApiReference diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs index dfd28ded3..81cb4376b 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs @@ -52,7 +52,7 @@ public void ParsePrimitiveSchemaShouldSucceed() schema.Should().BeEquivalentTo( new OpenApiSchema { - Type = "string", + Type = JsonSchemaType.String, Format = "email" }); } @@ -167,10 +167,10 @@ public void ParseDictionarySchemaShouldSucceed() schema.Should().BeEquivalentTo( new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "string" + Type = JsonSchemaType.String } }); } @@ -199,17 +199,17 @@ public void ParseBasicSchemaWithExampleShouldSucceed() schema.Should().BeEquivalentTo( new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String } }, Required = @@ -254,18 +254,18 @@ public void ParseBasicSchemaWithReferenceShouldSucceed() { ["ErrorModel"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["code"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Minimum = 100, Maximum = 600 }, ["message"] = new() { - Type = "string" + Type = JsonSchemaType.String } }, Required = @@ -281,13 +281,13 @@ public void ParseBasicSchemaWithReferenceShouldSucceed() new OpenApiSchemaReference("ErrorModel", result.OpenApiDocument), new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Required = {"rootCause"}, Properties = { ["rootCause"] = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -311,7 +311,7 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed() { ["Pet"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Discriminator = new() { PropertyName = "petType" @@ -320,11 +320,11 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed() { ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["petType"] = new() { - Type = "string" + Type = JsonSchemaType.String } }, Required = @@ -341,13 +341,13 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed() new OpenApiSchemaReference("Pet", result.OpenApiDocument), new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Required = {"huntingSkill"}, Properties = { ["huntingSkill"] = new() { - Type = "string", + Type = JsonSchemaType.String, Description = "The measured skill for hunting", Enum = { @@ -369,13 +369,13 @@ public void ParseAdvancedSchemaWithReferenceShouldSucceed() new OpenApiSchemaReference("Pet", result.OpenApiDocument), new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Required = {"packSize"}, Properties = { ["packSize"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32", Description = "the size of the pack the dog is from", Default = 0, diff --git a/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs b/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs index 7326351da..deec23c4e 100644 --- a/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs +++ b/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs @@ -14,48 +14,48 @@ public class OpenApiTypeMapperTests { public static IEnumerable PrimitiveTypeData => new List { - new object[] { typeof(int), new OpenApiSchema { Type = "number", Format = "int32" } }, - new object[] { typeof(decimal), new OpenApiSchema { Type = "number", Format = "double" } }, - new object[] { typeof(decimal?), new OpenApiSchema { Type = "number", Format = "double", Nullable = true } }, - new object[] { typeof(bool?), new OpenApiSchema { Type = "boolean", Nullable = true } }, - new object[] { typeof(Guid), new OpenApiSchema { Type = "string", Format = "uuid" } }, - new object[] { typeof(Guid?), new OpenApiSchema { Type = "string", Format = "uuid", Nullable = true } }, - new object[] { typeof(uint), new OpenApiSchema { Type = "number", Format = "int32" } }, - new object[] { typeof(long), new OpenApiSchema { Type = "number", Format = "int64" } }, - new object[] { typeof(long?), new OpenApiSchema { Type = "number", Format = "int64", Nullable = true } }, - new object[] { typeof(ulong), new OpenApiSchema { Type = "number", Format = "int64" } }, - new object[] { typeof(string), new OpenApiSchema { Type = "string" } }, - new object[] { typeof(double), new OpenApiSchema { Type = "number", Format = "double" } }, - new object[] { typeof(float?), new OpenApiSchema { Type = "number", Format = "float", Nullable = true } }, - new object[] { typeof(byte?), new OpenApiSchema { Type = "string", Format = "byte", Nullable = true } }, - new object[] { typeof(int?), new OpenApiSchema { Type = "number", Format = "int32", Nullable = true } }, - new object[] { typeof(uint?), new OpenApiSchema { Type = "number", Format = "int32", Nullable = true } }, - new object[] { typeof(DateTimeOffset?), new OpenApiSchema { Type = "string", Format = "date-time", Nullable = true } }, - new object[] { typeof(double?), new OpenApiSchema { Type = "number", Format = "double", Nullable = true } }, - new object[] { typeof(char?), new OpenApiSchema { Type = "string", Nullable = true } }, - new object[] { typeof(DateTimeOffset), new OpenApiSchema { Type = "string", Format = "date-time" } } + new object[] { typeof(int), new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32" } }, + new object[] { typeof(decimal), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "double" } }, + new object[] { typeof(decimal?), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "double", Nullable = true } }, + new object[] { typeof(bool?), new OpenApiSchema { Type = JsonSchemaType.Boolean, Nullable = true } }, + new object[] { typeof(Guid), new OpenApiSchema { Type = JsonSchemaType.String, Format = "uuid" } }, + new object[] { typeof(Guid?), new OpenApiSchema { Type = JsonSchemaType.String, Format = "uuid", Nullable = true } }, + new object[] { typeof(uint), new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32" } }, + new object[] { typeof(long), new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int64" } }, + new object[] { typeof(long?), new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int64", Nullable = true } }, + new object[] { typeof(ulong), new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int64" } }, + new object[] { typeof(string), new OpenApiSchema { Type = JsonSchemaType.String } }, + new object[] { typeof(double), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "double" } }, + new object[] { typeof(float?), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "float", Nullable = true } }, + new object[] { typeof(byte?), new OpenApiSchema { Type = JsonSchemaType.String, Format = "byte", Nullable = true } }, + new object[] { typeof(int?), new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32", Nullable = true } }, + new object[] { typeof(uint?), new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32", Nullable = true } }, + new object[] { typeof(DateTimeOffset?), new OpenApiSchema { Type = JsonSchemaType.String, Format = "date-time", Nullable = true } }, + new object[] { typeof(double?), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "double", Nullable = true } }, + new object[] { typeof(char?), new OpenApiSchema { Type = JsonSchemaType.String, Nullable = true } }, + new object[] { typeof(DateTimeOffset), new OpenApiSchema { Type = JsonSchemaType.String, Format = "date-time" } } }; public static IEnumerable OpenApiDataTypes => new List { - new object[] { new OpenApiSchema { Type = "number", Format = "int32", Nullable = false}, typeof(int) }, - new object[] { new OpenApiSchema { Type = "number", Format = "int32", Nullable = true}, typeof(int?) }, - new object[] { new OpenApiSchema { Type = "number", Format = "int64", Nullable = false}, typeof(long) }, - new object[] { new OpenApiSchema { Type = "number", Format = "int64", Nullable = true}, typeof(long?) }, - new object[] { new OpenApiSchema { Type = "number", Format = "decimal"}, typeof(decimal) }, - new object[] { new OpenApiSchema { Type = "integer", Format = null, Nullable = false}, typeof(long) }, - new object[] { new OpenApiSchema { Type = "integer", Format = null, Nullable = true}, typeof(long?) }, - new object[] { new OpenApiSchema { Type = "number", Format = null, Nullable = false}, typeof(double) }, - new object[] { new OpenApiSchema { Type = "number", Format = null, Nullable = true}, typeof(double?) }, - new object[] { new OpenApiSchema { Type = "number", Format = "decimal", Nullable = true}, typeof(decimal?) }, - new object[] { new OpenApiSchema { Type = "number", Format = "double", Nullable = true}, typeof(double?) }, - new object[] { new OpenApiSchema { Type = "string", Format = "date-time", Nullable = true}, typeof(DateTimeOffset?) }, - new object[] { new OpenApiSchema { Type = "string", Format = "char", Nullable = true}, typeof(char?) }, - new object[] { new OpenApiSchema { Type = "string", Format = "uuid", Nullable = true}, typeof(Guid?) }, - new object[] { new OpenApiSchema { Type = "string" }, typeof(string) }, - new object[] { new OpenApiSchema { Type = "number", Format = "double" }, typeof(double) }, - new object[] { new OpenApiSchema { Type = "number", Format = "float", Nullable = true }, typeof(float?) }, - new object[] { new OpenApiSchema { Type = "string", Format = "date-time" }, typeof(DateTimeOffset) } + new object[] { new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32", Nullable = false}, typeof(int) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int32", Nullable = true}, typeof(int?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int64", Nullable = false}, typeof(long) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Integer, Format = "int64", Nullable = true}, typeof(long?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "decimal"}, typeof(decimal) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Integer, Format = null, Nullable = false}, typeof(long) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Integer, Format = null, Nullable = true}, typeof(long?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = null, Nullable = false}, typeof(double) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = null, Nullable = true}, typeof(double?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "decimal", Nullable = true}, typeof(decimal?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "double", Nullable = true}, typeof(double?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.String, Format = "date-time", Nullable = true}, typeof(DateTimeOffset?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.String, Format = "char", Nullable = true}, typeof(char?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.String, Format = "uuid", Nullable = true}, typeof(Guid?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.String }, typeof(string) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "double" }, typeof(double) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "float", Nullable = true }, typeof(float?) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.String, Format = "date-time" }, typeof(DateTimeOffset) } }; [Theory] diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs index 61485897a..ad2c9ffdb 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs @@ -36,7 +36,7 @@ public class OpenApiCallbackTests { Schema = new() { - Type = "object" + Type = JsonSchemaType.Object } } } @@ -76,7 +76,7 @@ public class OpenApiCallbackTests { Schema = new() { - Type = "object" + Type = JsonSchemaType.Object } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiComponentsTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiComponentsTests.cs index 0f9ace617..a959edbf6 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiComponentsTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiComponentsTests.cs @@ -24,11 +24,11 @@ public class OpenApiComponentsTests { ["property2"] = new() { - Type = "integer" + Type = JsonSchemaType.Integer }, ["property3"] = new() { - Type = "string", + Type = JsonSchemaType.String, MaxLength = 15 } } @@ -73,7 +73,7 @@ public class OpenApiComponentsTests { ["property2"] = new() { - Type = "integer" + Type = JsonSchemaType.Integer }, ["property3"] = new OpenApiSchemaReference("schema2", null) } @@ -84,7 +84,7 @@ public class OpenApiComponentsTests { ["property2"] = new() { - Type = "integer" + Type = JsonSchemaType.Integer } } }, @@ -136,20 +136,20 @@ public class OpenApiComponentsTests { ["schema1"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["schema2"] = null, ["schema3"] = null, ["schema4"] = new() { - Type = "string", + Type = JsonSchemaType.String, AllOf = new List { null, null, new() { - Type = "string" + Type = JsonSchemaType.String }, null, null @@ -165,12 +165,12 @@ public class OpenApiComponentsTests ["schema1"] = new OpenApiSchemaReference("schema2", null), ["schema2"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["property1"] = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -183,23 +183,23 @@ public class OpenApiComponentsTests { ["schema1"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["property1"] = new() { - Type = "string" + Type = JsonSchemaType.String } } }, ["schema2"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["property1"] = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -224,7 +224,7 @@ public class OpenApiComponentsTests { ["property2"] = new OpenApiSchema() { - Type = "integer" + Type = JsonSchemaType.Integer }, ["property3"] = new OpenApiSchemaReference("schema2", null) } @@ -236,7 +236,7 @@ public class OpenApiComponentsTests { ["property2"] = new OpenApiSchema() { - Type = "integer" + Type = JsonSchemaType.Integer } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs index fa7a2048f..b4ba37215 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs @@ -37,12 +37,12 @@ public OpenApiDocumentTests() ["schema1"] = new OpenApiSchemaReference("schema2", null), ["schema2"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["property1"] = new() { - Type = "string", + Type = JsonSchemaType.String, Annotations = new Dictionary { { "key1", "value" } } } } @@ -56,12 +56,12 @@ public OpenApiDocumentTests() { ["schema1"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["property1"] = new() { - Type = "string", + Type = JsonSchemaType.String, Annotations = new Dictionary { { "key1", "value" } } } }, @@ -74,12 +74,12 @@ public OpenApiDocumentTests() }, ["schema2"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["property1"] = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -138,7 +138,7 @@ public OpenApiDocumentTests() { ["pet"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "id", @@ -148,22 +148,22 @@ public OpenApiDocumentTests() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["newPet"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "name" @@ -172,22 +172,22 @@ public OpenApiDocumentTests() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["errorModel"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "code", @@ -197,12 +197,12 @@ public OpenApiDocumentTests() { ["code"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" }, ["message"] = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -264,10 +264,10 @@ public OpenApiDocumentTests() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -279,7 +279,7 @@ public OpenApiDocumentTests() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -295,7 +295,7 @@ public OpenApiDocumentTests() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = PetSchemaWithReference } }, @@ -303,7 +303,7 @@ public OpenApiDocumentTests() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = PetSchemaWithReference } } @@ -407,7 +407,7 @@ public OpenApiDocumentTests() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -467,7 +467,7 @@ public OpenApiDocumentTests() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -515,7 +515,7 @@ public OpenApiDocumentTests() { ["pet"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "id", @@ -525,22 +525,22 @@ public OpenApiDocumentTests() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["newPet"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "name" @@ -549,22 +549,22 @@ public OpenApiDocumentTests() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, } }, ["errorModel"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "code", @@ -574,12 +574,12 @@ public OpenApiDocumentTests() { ["code"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" }, ["message"] = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -640,10 +640,10 @@ public OpenApiDocumentTests() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -655,7 +655,7 @@ public OpenApiDocumentTests() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -671,7 +671,7 @@ public OpenApiDocumentTests() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = PetSchema } }, @@ -679,7 +679,7 @@ public OpenApiDocumentTests() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = PetSchema } } @@ -783,7 +783,7 @@ public OpenApiDocumentTests() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -843,7 +843,7 @@ public OpenApiDocumentTests() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -936,16 +936,16 @@ public OpenApiDocumentTests() { ["id"] = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" }, ["name"] = new() { - Type = "string" + Type = JsonSchemaType.String }, ["tag"] = new() { - Type = "string" + Type = JsonSchemaType.String }, }, } @@ -987,7 +987,7 @@ public OpenApiDocumentTests() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Extensions = new Dictionary { ["my-extension"] = new OpenApiAny(4) @@ -1006,7 +1006,7 @@ public OpenApiDocumentTests() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Extensions = new Dictionary { ["my-extension"] = new OpenApiAny(4) @@ -1029,7 +1029,7 @@ public OpenApiDocumentTests() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = PetSchema } }, @@ -1099,10 +1099,10 @@ public OpenApiDocumentTests() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -1114,7 +1114,7 @@ public OpenApiDocumentTests() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -1130,7 +1130,7 @@ public OpenApiDocumentTests() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = PetSchema } }, @@ -1138,7 +1138,7 @@ public OpenApiDocumentTests() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = PetSchema } } @@ -1242,7 +1242,7 @@ public OpenApiDocumentTests() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -1302,7 +1302,7 @@ public OpenApiDocumentTests() Required = true, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } } @@ -1744,7 +1744,7 @@ public void SerializeV2DocumentWithNonArraySchemaTypeDoesNotWriteOutCollectionFo In = ParameterLocation.Query, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -1813,10 +1813,10 @@ public void SerializeV2DocumentWithStyleAsNullDoesNotWriteOutStyleValue() In = ParameterLocation.Query, Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "integer" + Type = JsonSchemaType.Integer } } } @@ -1832,7 +1832,7 @@ public void SerializeV2DocumentWithStyleAsNullDoesNotWriteOutStyleValue() { Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiHeaderTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiHeaderTests.cs index 9eded3dea..72c4fdfaa 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiHeaderTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiHeaderTests.cs @@ -20,7 +20,7 @@ public class OpenApiHeaderTests Description = "sampleHeader", Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } }; @@ -32,7 +32,7 @@ public class OpenApiHeaderTests Description = "sampleHeader", Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } }; diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs index 53f4d14f7..a65bf24c5 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiOperationTests.cs @@ -48,7 +48,7 @@ public class OpenApiOperationTests { Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Minimum = 5, Maximum = 10 } @@ -66,7 +66,7 @@ public class OpenApiOperationTests { Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Minimum = 5, Maximum = 10 } @@ -127,7 +127,7 @@ public class OpenApiOperationTests { Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Minimum = 5, Maximum = 10 } @@ -145,7 +145,7 @@ public class OpenApiOperationTests { Schema = new() { - Type = "number", + Type = JsonSchemaType.Number, Minimum = 5, Maximum = 10 } @@ -191,7 +191,7 @@ public class OpenApiOperationTests Required = true, Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -208,12 +208,12 @@ public class OpenApiOperationTests ["name"] = new() { Description = "Updated name of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["status"] = new() { Description = "Updated status of the pet", - Type = "string" + Type = JsonSchemaType.String } }, Required = new HashSet @@ -231,12 +231,12 @@ public class OpenApiOperationTests ["name"] = new() { Description = "Updated name of the pet", - Type = "string" + Type = JsonSchemaType.String }, ["status"] = new() { Description = "Updated status of the pet", - Type = "string" + Type = JsonSchemaType.String } }, Required = new HashSet diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs index 6893fe692..95596b787 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs @@ -48,8 +48,8 @@ public class OpenApiParameterTests Description = "description2", OneOf = new List { - new() { Type = "number", Format = "double" }, - new() { Type = "string" } + new() { Type = JsonSchemaType.Number, Format = "double" }, + new() { Type = JsonSchemaType.String } } }, Examples = new Dictionary @@ -71,7 +71,7 @@ public class OpenApiParameterTests Explode = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { Enum = @@ -92,7 +92,7 @@ public class OpenApiParameterTests Explode = true, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { Enum = @@ -110,10 +110,10 @@ public class OpenApiParameterTests In = ParameterLocation.Query, Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new OpenApiSchema { - Type = "integer" + Type = JsonSchemaType.Integer } } }; @@ -159,7 +159,7 @@ public class OpenApiParameterTests Explode = true, Schema = new() { - Type = "object" + Type = JsonSchemaType.Object }, Examples = new Dictionary { diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiRequestBodyTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiRequestBodyTests.cs index d6bd2cc69..ebffa38fd 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiRequestBodyTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiRequestBodyTests.cs @@ -25,7 +25,7 @@ public class OpenApiRequestBodyTests { Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } @@ -42,7 +42,7 @@ public class OpenApiRequestBodyTests { Schema = new() { - Type = "string" + Type = JsonSchemaType.String } } } diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiResponseTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiResponseTests.cs index 2de154306..f09d41da8 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiResponseTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiResponseTests.cs @@ -32,7 +32,7 @@ public class OpenApiResponseTests { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new OpenApiSchemaReference("customType", null) }, Example = "Blabla", @@ -49,7 +49,7 @@ public class OpenApiResponseTests Description = "The number of allowed requests in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, ["X-Rate-Limit-Reset"] = new OpenApiHeader @@ -57,7 +57,7 @@ public class OpenApiResponseTests Description = "The number of seconds left in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, } @@ -71,7 +71,7 @@ public class OpenApiResponseTests { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new OpenApiSchemaReference("customType", null) }, Example = "Blabla", @@ -88,7 +88,7 @@ public class OpenApiResponseTests Description = "The number of allowed requests in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, ["X-Rate-Limit-Reset"] = new OpenApiHeader @@ -96,7 +96,7 @@ public class OpenApiResponseTests Description = "The number of seconds left in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, } @@ -112,7 +112,7 @@ public class OpenApiResponseTests { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new OpenApiSchemaReference("customType", null) } } @@ -124,7 +124,7 @@ public class OpenApiResponseTests Description = "The number of allowed requests in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, ["X-Rate-Limit-Reset"] = new OpenApiHeader @@ -132,7 +132,7 @@ public class OpenApiResponseTests Description = "The number of seconds left in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, } @@ -148,7 +148,7 @@ public class OpenApiResponseTests { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new OpenApiSchemaReference("customType", null) } } @@ -160,7 +160,7 @@ public class OpenApiResponseTests Description = "The number of allowed requests in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, ["X-Rate-Limit-Reset"] = new OpenApiHeader @@ -168,7 +168,7 @@ public class OpenApiResponseTests Description = "The number of seconds left in the current period", Schema = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }, } diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs index 1a19457b4..408173e6e 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs @@ -32,7 +32,7 @@ public class OpenApiSchemaTests ExclusiveMinimum = true, Minimum = 10, Default = 15, - Type = "integer", + Type = JsonSchemaType.Integer, Nullable = true, ExternalDocs = new() @@ -53,11 +53,11 @@ public class OpenApiSchemaTests { ["property2"] = new() { - Type = "integer" + Type = JsonSchemaType.Integer }, ["property3"] = new() { - Type = "string", + Type = JsonSchemaType.String, MaxLength = 15 } }, @@ -72,13 +72,13 @@ public class OpenApiSchemaTests { ["property6"] = new() { - Type = "boolean" + Type = JsonSchemaType.Boolean } } }, ["property7"] = new() { - Type = "string", + Type = JsonSchemaType.String, MinLength = 2 } }, @@ -103,11 +103,11 @@ public class OpenApiSchemaTests { ["property1"] = new() { - Type = "integer" + Type = JsonSchemaType.Integer }, ["property2"] = new() { - Type = "string", + Type = JsonSchemaType.String, MaxLength = 15 } }, @@ -123,13 +123,13 @@ public class OpenApiSchemaTests { ["property4"] = new() { - Type = "boolean" + Type = JsonSchemaType.Boolean } } }, ["property5"] = new() { - Type = "string", + Type = JsonSchemaType.String, MinLength = 2 } }, @@ -151,7 +151,7 @@ public class OpenApiSchemaTests ExclusiveMinimum = true, Minimum = 10, Default = 15, - Type = "integer", + Type = JsonSchemaType.Integer, Nullable = true, ExternalDocs = new() @@ -173,11 +173,11 @@ public class OpenApiSchemaTests { ["property2"] = new() { - Type = "integer" + Type = JsonSchemaType.Integer }, ["property3"] = new() { - Type = "string", + Type = JsonSchemaType.String, MaxLength = 15, ReadOnly = true } @@ -194,13 +194,13 @@ public class OpenApiSchemaTests { ["property6"] = new() { - Type = "boolean" + Type = JsonSchemaType.Boolean } } }, ["property7"] = new() { - Type = "string", + Type = JsonSchemaType.String, MinLength = 2 } }, @@ -427,10 +427,10 @@ public void SerializeAsV2ShouldSetFormatPropertyInParentSchemaIfPresentInChildre { new() { - Type = "number", + Type = JsonSchemaType.Number, Format = "decimal" }, - new() { Type = "string" }, + new() { Type = JsonSchemaType.String }, } }; @@ -466,7 +466,7 @@ public void OpenApiSchemaCopyConstructorSucceeds() { var baseSchema = new OpenApiSchema { - Type = "string", + Type = JsonSchemaType.String, Format = "date" }; @@ -475,7 +475,7 @@ public void OpenApiSchemaCopyConstructorSucceeds() Nullable = true }; - Assert.Equal("string", actualSchema.Type); + Assert.Equal(JsonSchemaType.String, actualSchema.Type); Assert.Equal("date", actualSchema.Format); Assert.True(actualSchema.Nullable); } @@ -571,7 +571,7 @@ public void OpenApiWalkerVisitsOpenApiSchemaNot() Not = new OpenApiSchema() { Title = "Inner Schema", - Type = "string", + Type = JsonSchemaType.String, } }; diff --git a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs index 5773c178e..cfdf4ab1c 100644 --- a/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/References/OpenApiHeaderReferenceTests.cs @@ -102,7 +102,7 @@ public OpenApiHeaderReferenceTests() public void HeaderReferenceResolutionWorks() { // Assert - Assert.Equal("string", _externalHeaderReference.Schema.Type); + Assert.Equal(JsonSchemaType.String, _externalHeaderReference.Schema.Type); Assert.Equal("Location of the locally referenced post", _localHeaderReference.Description); Assert.Equal("Location of the externally referenced post", _externalHeaderReference.Description); Assert.Equal("The URL of the newly created post", diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index fb6d382b1..ef18b4cfb 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -192,6 +192,8 @@ namespace Microsoft.OpenApi.Extensions { public static System.Type MapOpenApiPrimitiveTypeToSimpleType(this Microsoft.OpenApi.Models.OpenApiSchema schema) { } public static Microsoft.OpenApi.Models.OpenApiSchema MapTypeToOpenApiPrimitiveType(this System.Type type) { } + public static string ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType? schemaType) { } + public static Microsoft.OpenApi.Models.JsonSchemaType ToJsonSchemaType(this string identifier) { } } public static class StringExtensions { @@ -329,6 +331,17 @@ namespace Microsoft.OpenApi.MicrosoftExtensions } namespace Microsoft.OpenApi.Models { + [System.Flags] + public enum JsonSchemaType + { + Null = 1, + Boolean = 2, + Integer = 4, + Number = 8, + String = 16, + Object = 32, + Array = 64, + } public class OpenApiCallback : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceable, Microsoft.OpenApi.Interfaces.IOpenApiSerializable { public OpenApiCallback() { } @@ -905,7 +918,7 @@ namespace Microsoft.OpenApi.Models public virtual System.Collections.Generic.ISet Required { get; set; } public virtual string Schema { get; set; } public virtual string Title { get; set; } - public virtual object Type { get; set; } + public virtual Microsoft.OpenApi.Models.JsonSchemaType? Type { get; set; } public virtual bool UnEvaluatedProperties { get; set; } public virtual bool UnevaluatedProperties { get; set; } public virtual bool? UniqueItems { get; set; } @@ -1244,7 +1257,7 @@ namespace Microsoft.OpenApi.Models.References public override System.Collections.Generic.ISet Required { get; set; } public override string Schema { get; set; } public override string Title { get; set; } - public override object Type { get; set; } + public override Microsoft.OpenApi.Models.JsonSchemaType? Type { get; set; } public override bool UnEvaluatedProperties { get; set; } public override bool UnevaluatedProperties { get; set; } public override bool? UniqueItems { get; set; } diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs index 356a233a1..25423ab1f 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs @@ -26,7 +26,7 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema() Example = 55, Schema = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } }; @@ -57,10 +57,10 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema() Required = true, Schema = new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new OpenApiSchema { - Type = "integer" + Type = JsonSchemaType.Integer } }, Examples = diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiMediaTypeValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiMediaTypeValidationTests.cs index 29bd199e1..51a8e1795 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiMediaTypeValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiMediaTypeValidationTests.cs @@ -25,7 +25,7 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema() Example = 55, Schema = new() { - Type = "string", + Type = JsonSchemaType.String, } }; @@ -52,10 +52,10 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema() { Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "integer", + Type = JsonSchemaType.Integer, } }, Examples = diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiParameterValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiParameterValidationTests.cs index ef25808d2..3f380c7f1 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiParameterValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiParameterValidationTests.cs @@ -74,7 +74,7 @@ public void ValidateExampleShouldNotHaveDataTypeMismatchForSimpleSchema() Example = 55, Schema = new() { - Type = "string", + Type = JsonSchemaType.String, } }; @@ -104,10 +104,10 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema() Required = true, Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "integer", + Type = JsonSchemaType.Integer, } }, Examples = @@ -169,7 +169,7 @@ public void PathParameterNotInThePathShouldReturnAnError() Required = true, Schema = new() { - Type = "string", + Type = JsonSchemaType.String, } }; @@ -207,7 +207,7 @@ public void PathParameterInThePathShouldBeOk() Required = true, Schema = new() { - Type = "string", + Type = JsonSchemaType.String, } }; diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiReferenceValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiReferenceValidationTests.cs index f41009fbc..b7597cb31 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiReferenceValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiReferenceValidationTests.cs @@ -20,7 +20,7 @@ public void ReferencedSchemaShouldOnlyBeValidatedOnce() var sharedSchema = new OpenApiSchema { - Type = "string", + Type = JsonSchemaType.String, Reference = new() { Id = "test" @@ -85,7 +85,7 @@ public void UnresolvedSchemaReferencedShouldNotBeValidated() var sharedSchema = new OpenApiSchema { - Type = "string", + Type = JsonSchemaType.String, Reference = new() { Id = "test" diff --git a/test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs index 5f4ba5d6b..d2aa19590 100644 --- a/test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Validations/OpenApiSchemaValidationTests.cs @@ -27,7 +27,7 @@ public void ValidateDefaultShouldNotHaveDataTypeMismatchForSimpleSchema() var schema = new OpenApiSchema { Default = 55, - Type = "string", + Type = JsonSchemaType.String, }; // Act @@ -51,7 +51,7 @@ public void ValidateExampleAndDefaultShouldNotHaveDataTypeMismatchForSimpleSchem { Example = 55, Default = "1234", - Type = "string", + Type = JsonSchemaType.String, }; // Act @@ -90,10 +90,10 @@ public void ValidateEnumShouldNotHaveDataTypeMismatchForSimpleSchema() ["y"] = 40, }).Node }, - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }; @@ -116,38 +116,38 @@ public void ValidateDefaultShouldNotHaveDataTypeMismatchForComplexSchema() IEnumerable warnings; var schema = new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Properties = { ["property1"] = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int64" } }, ["property2"] = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "boolean" + Type = JsonSchemaType.Boolean } } }, ["property3"] = new() { - Type = "string", + Type = JsonSchemaType.String, Format = "password" }, ["property4"] = new() { - Type = "string" + Type = JsonSchemaType.String } }, Default = new JsonObject() @@ -198,7 +198,7 @@ public void ValidateSchemaRequiredFieldListMustContainThePropertySpecifiedInTheD "schema1", new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Discriminator = new() { PropertyName = "property1" }, Reference = new() { Id = "schema1" } } @@ -235,7 +235,7 @@ public void ValidateOneOfSchemaPropertyNameContainsPropertySpecifiedInTheDiscrim "Person", new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Discriminator = new() { PropertyName = "type" @@ -250,7 +250,7 @@ public void ValidateOneOfSchemaPropertyNameContainsPropertySpecifiedInTheDiscrim "type", new OpenApiSchema { - Type = "array" + Type = JsonSchemaType.Array } } }, diff --git a/test/Microsoft.OpenApi.Tests/Walkers/WalkerLocationTests.cs b/test/Microsoft.OpenApi.Tests/Walkers/WalkerLocationTests.cs index 698d3fc5c..bec1f3602 100644 --- a/test/Microsoft.OpenApi.Tests/Walkers/WalkerLocationTests.cs +++ b/test/Microsoft.OpenApi.Tests/Walkers/WalkerLocationTests.cs @@ -82,7 +82,7 @@ public void LocatePathOperationContentSchema() { Schema = new OpenApiSchema { - Type = "string" + Type = JsonSchemaType.String } } } @@ -120,10 +120,10 @@ public void WalkDOMWithCycles() { var loopySchema = new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Properties = new Dictionary { - ["name"] = new() { Type = "string" } + ["name"] = new() { Type = JsonSchemaType.String } } }; diff --git a/test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs b/test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs index c2b956feb..f8bde4f85 100644 --- a/test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs @@ -58,7 +58,7 @@ public void OpenApiWorkspacesCanAddComponentsFromAnotherDocument() Schemas = { ["test"] = new() { - Type = "string", + Type = JsonSchemaType.String, Description = "The referenced one" } } @@ -92,7 +92,7 @@ public void OpenApiWorkspacesCanResolveReferencesToDocumentFragments() var workspace = new OpenApiWorkspace(); var schemaFragment = new OpenApiSchema() { - Type = "string", + Type = JsonSchemaType.String, Description = "Schema from a fragment" }; workspace.RegisterComponent("common#/components/schemas/test", schemaFragment); @@ -138,7 +138,7 @@ private static OpenApiDocument CreateCommonDocument() { ["test"] = new() { - Type = "string", + Type = JsonSchemaType.String, Description = "The referenced one" } } diff --git a/test/Microsoft.OpenApi.Tests/Writers/OpenApiYamlWriterTests.cs b/test/Microsoft.OpenApi.Tests/Writers/OpenApiYamlWriterTests.cs index 977247f7a..de9cbda56 100644 --- a/test/Microsoft.OpenApi.Tests/Writers/OpenApiYamlWriterTests.cs +++ b/test/Microsoft.OpenApi.Tests/Writers/OpenApiYamlWriterTests.cs @@ -440,7 +440,7 @@ private static OpenApiDocument CreateDocWithSimpleSchemaToInline() // Arrange var thingSchema = new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, UnresolvedReference = false, Reference = new() {