From 79f3e9464a9b4ca1db7ed816c55f5dad418d4d42 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Mon, 28 Oct 2024 20:34:08 +0300 Subject: [PATCH 01/17] Define JSON schema type as a flaggable enum to allow storing strings or array of strings --- .../Formatters/PowerShellFormatter.cs | 8 +- .../Models/JsonSchemaType.cs | 54 +++++++++ .../Models/OpenApiParameter.cs | 6 +- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 106 ++++++++++-------- .../References/OpenApiSchemaReference.cs | 2 +- .../Reader/V2/OpenApiOperationDeserializer.cs | 4 +- 6 files changed, 126 insertions(+), 54 deletions(-) create mode 100644 src/Microsoft.OpenApi/Models/JsonSchemaType.cs 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 + /// Represents the type of a JSON schema. + /// + [Flags] + public enum JsonSchemaType + { + /// + /// Represents any type. + /// + Any = 0, + + /// + /// 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/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index 1adfc8c01..f7fc6f0c7 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; @@ -90,7 +91,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 @@ -367,7 +368,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; @@ -590,7 +591,7 @@ internal void WriteV31Properties(IOpenApiWriter writer) internal void WriteAsItemsProperties(IOpenApiWriter writer) { // type - writer.WriteProperty(OpenApiConstants.Type, (string)Type); + writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(Type)); // format if (string.IsNullOrEmpty(Format)) @@ -670,14 +671,7 @@ internal void SerializeAsV2( writer.WriteStartObject(); // type - if (Type is string[] array) - { - DowncastTypeArrayToV2OrV3(array, writer, OpenApiSpecVersion.OpenApi2_0); - } - else - { - writer.WriteProperty(OpenApiConstants.Type, (string)Type); - } + SerializeTypeProperty(Type, writer, OpenApiSpecVersion.OpenApi2_0); // description writer.WriteProperty(OpenApiConstants.Description, Description); @@ -806,60 +800,79 @@ 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, OpenApiTypeMapper.ToIdentifier(type)); } } - 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)); + var list = new List(); + foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) + { + list.Add(flag); + } + + writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteRaw(OpenApiTypeMapper.ToIdentifier(s))); } } } - 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))) + { + // Ignore the None flag and check if the flag is set + if (value != JsonSchemaType.Any && (schemaType & value) == 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 & flag) == flag && flag != JsonSchemaType.Any) { - copiedArray.SetValue(DeepCloneType(array?.GetValue(i)), i); + list.Add(OpenApiTypeMapper.ToIdentifier(flag)); } - return copiedArray; } - return null; + writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteRaw(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 @@ -867,27 +880,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, OpenApiTypeMapper.ToIdentifier(schemaType)); } } - else if (array.Length is 2 && array.Contains(OpenApiConstants.Null)) + else if (flagsCount is 2 && (schemaType & JsonSchemaType.Null) == JsonSchemaType.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 & flag) == flag && flag != JsonSchemaType.Null && flag != JsonSchemaType.Any) + { + // Write the non-null flag value to the writer + writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(flag)); + } + } 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 535a6a522..d1100adf2 100644 --- a/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs +++ b/src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs @@ -92,7 +92,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/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; } From 8c9047f2a0addf6d6f554eedaa50ca8dd1341a34 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Mon, 28 Oct 2024 20:36:39 +0300 Subject: [PATCH 02/17] Implement helper methods for switching between enum types and their equivalent identifiers --- .../Extensions/OpenApiTypeMapper.cs | 103 ++++++++++++------ .../Models/OpenApiRequestBody.cs | 5 +- .../Reader/V2/OpenApiHeaderDeserializer.cs | 4 +- .../Reader/V2/OpenApiParameterDeserializer.cs | 4 +- .../Reader/V2/OpenApiSchemaDeserializer.cs | 2 +- .../Reader/V3/OpenApiSchemaDeserializer.cs | 2 +- .../Reader/V31/OpenApiSchemaDeserializer.cs | 32 +++--- .../Validations/Rules/RuleHelpers.cs | 3 +- 8 files changed, 101 insertions(+), 54 deletions(-) diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs index 61fac7197..c10c04ca6 100644 --- a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs @@ -12,40 +12,81 @@ namespace Microsoft.OpenApi.Extensions /// public static class OpenApiTypeMapper { + /// + /// Maps a JsonSchema data type to an identifier. + /// + /// + /// + public static string ToIdentifier(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 IdentifierToEnumType(string identifier) + { + return identifier switch + { + "null" => JsonSchemaType.Null, + "boolean" => JsonSchemaType.Boolean, + "integer" or "int" => JsonSchemaType.Integer, + "number" or "double" => JsonSchemaType.Number, + "string" => JsonSchemaType.String, + "array" => JsonSchemaType.Array, + "object" => JsonSchemaType.Object, + "file" => JsonSchemaType.String, // File is treated as string + _ => JsonSchemaType.Any, + }; + } + private static readonly Dictionary> _simpleTypeToOpenApiSchema = new() { - [typeof(bool)] = () => new() { Type = "boolean" }, - [typeof(byte)] = () => new() { Type = "string", Format = "byte" }, - [typeof(int)] = () => new() { Type = "integer", Format = "int32" }, - [typeof(uint)] = () => new() { Type = "integer", Format = "int32" }, - [typeof(long)] = () => new() { Type = "integer", Format = "int64" }, - [typeof(ulong)] = () => new() { Type = "integer", 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 = "integer", Format = "int32", Nullable = true }, - [typeof(uint?)] = () => new() { Type = "integer", Format = "int32", Nullable = true }, - [typeof(long?)] = () => new() { Type = "integer", Format = "int64", Nullable = true }, - [typeof(ulong?)] = () => new() { Type = "integer", 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 +120,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 +136,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 = (ToIdentifier(schema.Type), schema.Format?.ToLowerInvariant(), schema.Nullable) switch { ("boolean", null, false) => typeof(bool), ("integer", "int32", false) => typeof(int), diff --git a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs index b35619a2c..59ca2ab5a 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(OpenApiTypeMapper.ToIdentifier(paramSchema.Type), StringComparison.OrdinalIgnoreCase) && ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase) || "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase))) { - paramSchema.Type = "file"; + paramSchema.Type = OpenApiTypeMapper.IdentifierToEnumType("file"); paramSchema.Format = null; } yield return new() diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs index 5667b8f98..479fd6f39 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 = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue()) }, { "format", diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs index 60167f891..185f93fac 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 = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue()) }, { "items", diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs index 66c45c641..f53e2d85e 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 = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue()) }, { "allOf", diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs index 2dd2e4f6a..2d86be7d4 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 = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue()) }, { "allOf", diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs index 7757c710f..eae150013 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs @@ -122,9 +122,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 = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue()); + } + else + { + var list = n.CreateSimpleList((n2, p) => n2.GetScalarValue()); + JsonSchemaType combinedType = JsonSchemaType.Any; + foreach(var type in list) + { + var schemaType = OpenApiTypeMapper.IdentifierToEnumType(type); + combinedType |= schemaType; + } + o.Type = combinedType; + } } }, { @@ -187,15 +199,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; } } }, @@ -259,8 +263,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 9902360ec..f0b41c6d3 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs @@ -5,6 +5,7 @@ using System.Text.Json; using System.Text.Json.Nodes; using Microsoft.OpenApi.Any; +using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Models; namespace Microsoft.OpenApi.Validations.Rules @@ -55,7 +56,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 = OpenApiTypeMapper.ToIdentifier(schema.Type); var format = schema.Format; var nullable = schema.Nullable; From 2e03505627875bb49bf9d66cdf93e809d8e5297c Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Mon, 28 Oct 2024 20:37:28 +0300 Subject: [PATCH 03/17] Refactor tests to reflect change --- .../Formatters/PowerShellFormatterTests.cs | 20 +-- .../Services/OpenApiFilterServiceTests.cs | 2 +- .../UtilityFiles/OpenApiDocumentMock.cs | 38 +++--- .../TryLoadReferenceV2Tests.cs | 6 +- .../V2Tests/OpenApiDocumentTests.cs | 16 +-- .../V2Tests/OpenApiHeaderTests.cs | 4 +- .../V2Tests/OpenApiOperationTests.cs | 28 ++--- .../V2Tests/OpenApiParameterTests.cs | 16 +-- .../V2Tests/OpenApiPathItemTests.cs | 38 +++--- .../V2Tests/OpenApiSchemaTests.cs | 6 +- .../V31Tests/OpenApiDocumentTests.cs | 68 +++++----- .../V31Tests/OpenApiSchemaTests.cs | 74 +++++------ .../V3Tests/OpenApiCallbackTests.cs | 8 +- .../V3Tests/OpenApiDocumentTests.cs | 80 ++++++------ .../V3Tests/OpenApiEncodingTests.cs | 2 +- .../V3Tests/OpenApiMediaTypeTests.cs | 4 +- .../V3Tests/OpenApiOperationTests.cs | 4 +- .../V3Tests/OpenApiParameterTests.cs | 34 ++--- .../V3Tests/OpenApiSchemaTests.cs | 36 +++--- .../Extensions/OpenApiTypeMapperTests.cs | 62 ++++----- .../Models/OpenApiCallbackTests.cs | 6 +- .../Models/OpenApiComponentsTests.cs | 30 ++--- .../Models/OpenApiDocumentTests.cs | 118 +++++++++--------- .../Models/OpenApiHeaderTests.cs | 4 +- .../Models/OpenApiOperationTests.cs | 18 +-- .../Models/OpenApiParameterTests.cs | 14 +-- .../Models/OpenApiRequestBodyTests.cs | 4 +- .../Models/OpenApiResponseTests.cs | 24 ++-- .../Models/OpenApiSchemaTests.cs | 38 +++--- .../References/OpenApiHeaderReferenceTests.cs | 2 +- .../OpenApiHeaderValidationTests.cs | 6 +- .../OpenApiMediaTypeValidationTests.cs | 6 +- .../OpenApiParameterValidationTests.cs | 10 +- .../OpenApiReferenceValidationTests.cs | 4 +- .../OpenApiSchemaValidationTests.cs | 30 ++--- .../Walkers/WalkerLocationTests.cs | 6 +- .../Workspaces/OpenApiWorkspaceTests.cs | 6 +- .../Writers/OpenApiYamlWriterTests.cs | 2 +- 38 files changed, 438 insertions(+), 436 deletions(-) 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.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs index c954387a6..e9f962c28 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs @@ -39,7 +39,7 @@ public void ParseDocumentWithWebhooksShouldSucceed() { ["petSchema"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "id", @@ -49,22 +49,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 +73,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 +116,10 @@ public void ParseDocumentWithWebhooksShouldSucceed() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -131,7 +131,7 @@ public void ParseDocumentWithWebhooksShouldSucceed() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -147,7 +147,7 @@ public void ParseDocumentWithWebhooksShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } }, @@ -155,7 +155,7 @@ public void ParseDocumentWithWebhooksShouldSucceed() { Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } } @@ -215,7 +215,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { ["petSchema"] = new() { - Type = "object", + Type = JsonSchemaType.Object, Required = new HashSet { "id", @@ -225,22 +225,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 +249,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 +290,10 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() Required = false, Schema = new() { - Type = "array", + Type = JsonSchemaType.Array, Items = new() { - Type = "string" + Type = JsonSchemaType.String } } }, @@ -305,7 +305,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() Required = false, Schema = new() { - Type = "integer", + Type = JsonSchemaType.Integer, Format = "int32" } } @@ -321,7 +321,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { Schema = new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } }, @@ -329,7 +329,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() { Schema = new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Items = petSchema } } @@ -423,27 +423,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 +482,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] diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs index cacb1ed86..bca4ad866 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.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.Collections.Generic; @@ -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: - - string - null + - string 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); } } } 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 bb42a9c2a..e97ce2e73 100644 --- a/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs +++ b/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs @@ -14,41 +14,41 @@ public class OpenApiTypeMapperTests { public static IEnumerable PrimitiveTypeData => new List { - new object[] { typeof(int), new OpenApiSchema { Type = "integer", Format = "int32" } }, - new object[] { typeof(decimal), new OpenApiSchema { Type = "number", Format = "double" } }, - new object[] { typeof(bool?), new OpenApiSchema { Type = "boolean", Nullable = true } }, - new object[] { typeof(Guid), new OpenApiSchema { Type = "string", Format = "uuid" } }, - new object[] { typeof(uint), new OpenApiSchema { Type = "integer", Format = "int32" } }, - new object[] { typeof(long), new OpenApiSchema { Type = "integer", Format = "int64" } }, - new object[] { typeof(ulong), new OpenApiSchema { Type = "integer", 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 = "integer", Format = "int32", Nullable = true } }, - new object[] { typeof(uint?), new OpenApiSchema { Type = "integer", 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(bool?), new OpenApiSchema { Type = JsonSchemaType.Boolean, Nullable = true } }, + new object[] { typeof(Guid), new OpenApiSchema { Type = JsonSchemaType.String, Format = "uuid" } }, + 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(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 = "integer", Format = "int32"}, typeof(int) }, - new object[] { new OpenApiSchema { Type = "number", Format = "decimal"}, typeof(decimal) }, - new object[] { new OpenApiSchema { Type = "number", Format = null, Nullable = false}, typeof(double) }, - new object[] { new OpenApiSchema { Type = "integer", Format = null, Nullable = false}, typeof(int) }, - new object[] { new OpenApiSchema { Type = "integer", Format = null, Nullable = true}, typeof(int?) }, - 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"}, typeof(int) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "decimal"}, typeof(decimal) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = null, Nullable = false}, typeof(double) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Integer, Format = null, Nullable = false}, typeof(int) }, + new object[] { new OpenApiSchema { Type = JsonSchemaType.Integer, Format = null, Nullable = true}, typeof(int?) }, + 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..e4f33871e 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 } } } @@ -96,7 +96,7 @@ public class OpenApiCallbackTests [Theory] [InlineData(true)] - [InlineData(false)] + //[InlineData(false)] public async Task SerializeAdvancedCallbackAsV3JsonWorksAsync(bool produceTerseOutput) { // Arrange 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/Validations/OpenApiHeaderValidationTests.cs b/test/Microsoft.OpenApi.Tests/Validations/OpenApiHeaderValidationTests.cs index bbc9dfe35..3d04795b8 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 } }; @@ -62,10 +62,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 9f42cb21b..f52913aef 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, } }; @@ -60,10 +60,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 beac66d74..5e88c067e 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, } }; @@ -112,10 +112,10 @@ public void ValidateExamplesShouldNotHaveDataTypeMismatchForSimpleSchema() Required = true, Schema = new() { - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "integer", + Type = JsonSchemaType.Integer, } }, Examples = @@ -188,7 +188,7 @@ public void PathParameterNotInThePathShouldReturnAnError() Required = true, Schema = new() { - Type = "string", + Type = JsonSchemaType.String, } }; @@ -226,7 +226,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 5885377ed..5848f94ad 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 @@ -59,7 +59,7 @@ public void ValidateExampleAndDefaultShouldNotHaveDataTypeMismatchForSimpleSchem { Example = 55, Default = "1234", - Type = "string", + Type = JsonSchemaType.String, }; // Act @@ -106,10 +106,10 @@ public void ValidateEnumShouldNotHaveDataTypeMismatchForSimpleSchema() ["y"] = 40, }).Node }, - Type = "object", + Type = JsonSchemaType.Object, AdditionalProperties = new() { - Type = "integer" + Type = JsonSchemaType.Integer } }; @@ -146,38 +146,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() @@ -238,7 +238,7 @@ public void ValidateSchemaRequiredFieldListMustContainThePropertySpecifiedInTheD "schema1", new OpenApiSchema { - Type = "object", + Type = JsonSchemaType.Object, Discriminator = new() { PropertyName = "property1" }, Reference = new() { Id = "schema1" } } @@ -275,7 +275,7 @@ public void ValidateOneOfSchemaPropertyNameContainsPropertySpecifiedInTheDiscrim "Person", new OpenApiSchema { - Type = "array", + Type = JsonSchemaType.Array, Discriminator = new() { PropertyName = "type" @@ -290,7 +290,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() { From d0fb2c7b758e12c4203350a447617daf36ce06b4 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Mon, 28 Oct 2024 20:37:40 +0300 Subject: [PATCH 04/17] Update API interface --- .../PublicApi/PublicApi.approved.txt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index 3a7fdbd57..d2ff052d1 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -190,8 +190,10 @@ namespace Microsoft.OpenApi.Extensions } public static class OpenApiTypeMapper { + public static Microsoft.OpenApi.Models.JsonSchemaType IdentifierToEnumType(string identifier) { } 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(Microsoft.OpenApi.Models.JsonSchemaType? schemaType) { } } public static class StringExtensions { @@ -329,6 +331,18 @@ namespace Microsoft.OpenApi.MicrosoftExtensions } namespace Microsoft.OpenApi.Models { + [System.Flags] + public enum JsonSchemaType + { + Any = 0, + 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() { } @@ -907,7 +921,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; } @@ -1248,7 +1262,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; } From 314880d8fc53d24bcb8a04a0874910f26ca993b9 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Tue, 29 Oct 2024 12:17:55 +0300 Subject: [PATCH 05/17] Fix failing tests --- .../Extensions/OpenApiTypeMapperTests.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs b/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs index a1c97fb01..deec23c4e 100644 --- a/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs +++ b/test/Microsoft.OpenApi.Tests/Extensions/OpenApiTypeMapperTests.cs @@ -14,22 +14,22 @@ public class OpenApiTypeMapperTests { public static IEnumerable PrimitiveTypeData => new List { - new object[] { typeof(int), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int32" } }, + 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.Number, Format = "int32" } }, - new object[] { typeof(long), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int64" } }, - new object[] { typeof(long?), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int64", Nullable = true } }, - new object[] { typeof(ulong), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int64" } }, + 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.Number, Format = "int32", Nullable = true } }, - new object[] { typeof(uint?), new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int32", 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 } }, @@ -38,10 +38,10 @@ public class OpenApiTypeMapperTests public static IEnumerable OpenApiDataTypes => new List { - new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int32", Nullable = false}, typeof(int) }, - new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int32", Nullable = true}, typeof(int?) }, - new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int64", Nullable = false}, typeof(long) }, - new object[] { new OpenApiSchema { Type = JsonSchemaType.Number, Format = "int64", Nullable = true}, typeof(long?) }, + 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?) }, From b2e1026ba2608d0cc5a7f0d3eb6c74600d7a593a Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Tue, 29 Oct 2024 13:01:49 +0300 Subject: [PATCH 06/17] code cleanup --- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index f7fc6f0c7..34575a285 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -839,7 +839,7 @@ private static int CountEnumSetFlags(JsonSchemaType? schemaType) { int count = 0; - if(schemaType != null) + if (schemaType != null) { // Check each flag in the enum foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType))) @@ -895,7 +895,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(schemaType)); } } - else if (flagsCount is 2 && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) + else if (flagsCount is 2 && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null { foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) { From 56c47cb374c18fd0f67f7983815cccc7cda03af3 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Tue, 29 Oct 2024 17:05:04 +0300 Subject: [PATCH 07/17] Remove JsonSchemaType.Any type --- src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs | 6 +++--- src/Microsoft.OpenApi/Models/JsonSchemaType.cs | 5 ----- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 6 +++--- .../Reader/V31/OpenApiSchemaDeserializer.cs | 2 +- .../PublicApi/PublicApi.approved.txt | 1 - 5 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs index ec032aa6b..53dc2021f 100644 --- a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.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; @@ -44,12 +44,12 @@ public static JsonSchemaType IdentifierToEnumType(string identifier) "null" => JsonSchemaType.Null, "boolean" => JsonSchemaType.Boolean, "integer" or "int" => JsonSchemaType.Integer, - "number" or "double" => JsonSchemaType.Number, + "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 - _ => JsonSchemaType.Any, + _ => throw new ArgumentException("Invalid schema type identifier", nameof(identifier)) }; } diff --git a/src/Microsoft.OpenApi/Models/JsonSchemaType.cs b/src/Microsoft.OpenApi/Models/JsonSchemaType.cs index 6e1816a9a..6024aa21b 100644 --- a/src/Microsoft.OpenApi/Models/JsonSchemaType.cs +++ b/src/Microsoft.OpenApi/Models/JsonSchemaType.cs @@ -11,11 +11,6 @@ namespace Microsoft.OpenApi.Models [Flags] public enum JsonSchemaType { - /// - /// Represents any type. - /// - Any = 0, - /// /// Represents a null type. /// diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index d00173e11..3714c875a 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -831,7 +831,7 @@ private static int CountEnumSetFlags(JsonSchemaType? schemaType) foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType))) { // Ignore the None flag and check if the flag is set - if (value != JsonSchemaType.Any && (schemaType & value) == value) + if ((schemaType & value) == value) { count++; } @@ -849,7 +849,7 @@ private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer) foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) { // Check if the flag is set in 'type' using a bitwise AND operation - if ((Type & flag) == flag && flag != JsonSchemaType.Any) + if ((Type & flag) == flag) { list.Add(OpenApiTypeMapper.ToIdentifier(flag)); } @@ -886,7 +886,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite 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 & flag) == flag && flag != JsonSchemaType.Null && flag != JsonSchemaType.Any) + if ((schemaType & flag) == flag && flag != JsonSchemaType.Null) { // Write the non-null flag value to the writer writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(flag)); diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs index 3da35ace1..9e34b3cef 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs @@ -121,7 +121,7 @@ internal static partial class OpenApiV31Deserializer else { var list = n.CreateSimpleList((n2, p) => n2.GetScalarValue()); - JsonSchemaType combinedType = JsonSchemaType.Any; + JsonSchemaType combinedType = 0; foreach(var type in list) { var schemaType = OpenApiTypeMapper.IdentifierToEnumType(type); diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index 66eae6f0b..f6ea38fc9 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -334,7 +334,6 @@ namespace Microsoft.OpenApi.Models [System.Flags] public enum JsonSchemaType { - Any = 0, Null = 1, Boolean = 2, Integer = 4, From 5af79d4b55185c4fb6f926791caf9ab81f7f8d15 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 11:15:06 +0300 Subject: [PATCH 08/17] Add condition to check whether the flag matches the type before appending it to a list; write string values --- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index 3714c875a..f95148962 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -813,10 +813,13 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer, var list = new List(); foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) { - list.Add(flag); + if ((type & flag) == flag) + { + list.Add(flag); + } } - writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteRaw(OpenApiTypeMapper.ToIdentifier(s))); + writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(OpenApiTypeMapper.ToIdentifier(s))); } } } @@ -855,7 +858,7 @@ private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer) } } - writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteRaw(s)); + writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s)); } private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWriter writer, OpenApiSpecVersion version, int flagsCount) From e12301cb69dcd05f063c8fc75dd07081befd5c73 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 11:31:33 +0300 Subject: [PATCH 09/17] Add test for sample document with 3.1 features --- ...DocumentWith31PropertiesWorks.verified.txt | 110 ++++++++++++++++ .../V31Tests/OpenApiDocumentTests.cs | 15 +++ .../documentWith31Properties.yaml | 122 ++++++++++++++++++ 3 files changed, 247 insertions(+) create mode 100644 test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.ParseDocumentWith31PropertiesWorks.verified.txt create mode 100644 test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/documentWith31Properties.yaml 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 e9f962c28..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 { @@ -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/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 From e13fb0144e7bae2d22f01e6e9bcef685200185a5 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 12:14:52 +0300 Subject: [PATCH 10/17] throw OpenApiException for it to be caught gracefully and appended to diagnostics --- src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs index 53dc2021f..07f40ef2c 100644 --- a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using Microsoft.OpenApi.Exceptions; using Microsoft.OpenApi.Models; namespace Microsoft.OpenApi.Extensions @@ -49,7 +50,7 @@ public static JsonSchemaType IdentifierToEnumType(string identifier) "array" => JsonSchemaType.Array, "object" => JsonSchemaType.Object, "file" => JsonSchemaType.String, // File is treated as string - _ => throw new ArgumentException("Invalid schema type identifier", nameof(identifier)) + _ => throw new OpenApiException(string.Format("Invalid schema type identifier", identifier)) }; } From 2e2ad57dd30be497de97e2b892342e66123850bb Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 12:26:08 +0300 Subject: [PATCH 11/17] Fix string formatting --- src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs index 07f40ef2c..59df7d1d6 100644 --- a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs @@ -50,7 +50,7 @@ public static JsonSchemaType IdentifierToEnumType(string identifier) "array" => JsonSchemaType.Array, "object" => JsonSchemaType.Object, "file" => JsonSchemaType.String, // File is treated as string - _ => throw new OpenApiException(string.Format("Invalid schema type identifier", identifier)) + _ => throw new OpenApiException(string.Format("Invalid schema type identifier: {0}", identifier)) }; } From 656259a8d6f74bb90e0623a666c9eed2e918ecbc Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 12:33:23 +0300 Subject: [PATCH 12/17] Fix failing tests --- .../V31Tests/OpenApiSchemaTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs index ab17f947d..967bb0f3e 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs @@ -337,7 +337,7 @@ public void SerializeV3SchemaWithNullableAsV31Works() { // Arrange var expected = @"type: - - null + - 'null' - string"; var path = Path.Combine(SampleFolderPath, "schemaWithNullable.yaml"); @@ -357,7 +357,7 @@ public void SerializeV2SchemaWithNullableExtensionAsV31Works() { // Arrange var expected = @"type: - - null + - 'null' - string x-nullable: true"; From 07987aeb64e7c0e6bb82bcfb9690b4faedef335f Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Wed, 30 Oct 2024 17:24:31 +0300 Subject: [PATCH 13/17] Update src/Microsoft.OpenApi/Models/OpenApiSchema.cs Co-authored-by: Andrew Omondi --- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index f95148962..d754024a2 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -834,7 +834,7 @@ private static int CountEnumSetFlags(JsonSchemaType? schemaType) foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType))) { // Ignore the None flag and check if the flag is set - if ((schemaType & value) == value) + if ((schemaType.Value.HasFlag(value)) { count++; } From 3abe36c3d7c96f08edcf9048b8276a8fbbdb4569 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 18:17:39 +0300 Subject: [PATCH 14/17] Convert the JsonSchemaType helper methods to extensions --- src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs | 6 +++--- src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs | 4 ++-- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 6 +++--- .../Reader/V2/OpenApiHeaderDeserializer.cs | 2 +- .../Reader/V2/OpenApiParameterDeserializer.cs | 2 +- .../Reader/V2/OpenApiSchemaDeserializer.cs | 2 +- .../Reader/V3/OpenApiSchemaDeserializer.cs | 2 +- .../Reader/V31/OpenApiSchemaDeserializer.cs | 4 ++-- src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs | 4 ++-- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs index 59df7d1d6..13bfd4723 100644 --- a/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs @@ -18,7 +18,7 @@ public static class OpenApiTypeMapper /// /// /// - public static string ToIdentifier(JsonSchemaType? schemaType) + public static string ToIdentifier(this JsonSchemaType? schemaType) { return schemaType switch { @@ -38,7 +38,7 @@ public static string ToIdentifier(JsonSchemaType? schemaType) /// /// /// - public static JsonSchemaType IdentifierToEnumType(string identifier) + public static JsonSchemaType ToJsonSchemaType(this string identifier) { return identifier switch { @@ -137,7 +137,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema throw new ArgumentNullException(nameof(schema)); } - var type = (ToIdentifier(schema.Type), 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/OpenApiRequestBody.cs b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs index 59ca2ab5a..037e7d92c 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs @@ -142,11 +142,11 @@ internal IEnumerable ConvertToFormDataParameters() foreach (var property in Content.First().Value.Schema.Properties) { var paramSchema = property.Value; - if ("string".Equals(OpenApiTypeMapper.ToIdentifier(paramSchema.Type), StringComparison.OrdinalIgnoreCase) + if ("string".Equals(paramSchema.Type.ToIdentifier(), StringComparison.OrdinalIgnoreCase) && ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase) || "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase))) { - paramSchema.Type = OpenApiTypeMapper.IdentifierToEnumType("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 d754024a2..377739324 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.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; @@ -577,7 +577,7 @@ internal void WriteV31Properties(IOpenApiWriter writer) internal void WriteAsItemsProperties(IOpenApiWriter writer) { // type - writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(Type)); + writer.WriteProperty(OpenApiConstants.Type, Type.ToIdentifier()); // format if (string.IsNullOrEmpty(Format)) @@ -798,7 +798,7 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer, } else { - writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(type)); + writer.WriteProperty(OpenApiConstants.Type, type.ToIdentifier()); } } else if(flagsCount > 1) diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs index 479fd6f39..b4ddf7300 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs @@ -24,7 +24,7 @@ internal static partial class OpenApiV2Deserializer }, { "type", - (o, n, _) => GetOrCreateSchema(o).Type = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue()) + (o, n, _) => GetOrCreateSchema(o).Type = n.GetScalarValue().ToJsonSchemaType() }, { "format", diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs index 185f93fac..149c00fd3 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs @@ -46,7 +46,7 @@ internal static partial class OpenApiV2Deserializer }, { "type", - (o, n, t) => GetOrCreateSchema(o).Type = OpenApiTypeMapper.IdentifierToEnumType(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 f53e2d85e..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 = OpenApiTypeMapper.IdentifierToEnumType(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 2d86be7d4..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 = OpenApiTypeMapper.IdentifierToEnumType(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 9e34b3cef..5dc76b7fb 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs @@ -116,7 +116,7 @@ internal static partial class OpenApiV31Deserializer { if (n is ValueNode) { - o.Type = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue()); + o.Type = n.GetScalarValue().ToJsonSchemaType(); } else { @@ -124,7 +124,7 @@ internal static partial class OpenApiV31Deserializer JsonSchemaType combinedType = 0; foreach(var type in list) { - var schemaType = OpenApiTypeMapper.IdentifierToEnumType(type); + var schemaType = type.ToJsonSchemaType(); combinedType |= schemaType; } o.Type = combinedType; diff --git a/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs b/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs index 6ed04a781..4e05c44fd 100644 --- a/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs +++ b/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.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.Text.Json; @@ -54,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 = OpenApiTypeMapper.ToIdentifier(schema.Type); + var type = schema.Type.ToIdentifier(); var format = schema.Format; var nullable = schema.Nullable; From 38954dce7ddeac66b37132a832e698da798509bd Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 18:18:34 +0300 Subject: [PATCH 15/17] Add null check for type; simplify condition to verify flag has been set --- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index 377739324..8df393ea6 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.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; @@ -810,18 +810,21 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer, } else { - var list = new List(); - foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) + if (type is not null) { - if ((type & flag) == flag) + var list = new List(); + foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) { - list.Add(flag); + if (type.Value.HasFlag(flag)) + { + list.Add(flag); + } } - } - - writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(OpenApiTypeMapper.ToIdentifier(s))); + + writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s.ToIdentifier())); + } } - } + } } private static int CountEnumSetFlags(JsonSchemaType? schemaType) @@ -834,7 +837,7 @@ private static int CountEnumSetFlags(JsonSchemaType? schemaType) foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType))) { // Ignore the None flag and check if the flag is set - if ((schemaType.Value.HasFlag(value)) + if (schemaType.Value.HasFlag(value)) { count++; } @@ -849,12 +852,12 @@ 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))) + foreach (JsonSchemaType? flag in System.Enum.GetValues(typeof(JsonSchemaType))) { // Check if the flag is set in 'type' using a bitwise AND operation - if ((Type & flag) == flag) + if (Type.Value.HasFlag(flag)) { - list.Add(OpenApiTypeMapper.ToIdentifier(flag)); + list.Add(flag.ToIdentifier()); } } @@ -881,18 +884,18 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite } else { - writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(schemaType)); + writer.WriteProperty(OpenApiConstants.Type, schemaType.ToIdentifier()); } } else if (flagsCount is 2 && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null { - foreach (JsonSchemaType flag in System.Enum.GetValues(typeof(JsonSchemaType))) + 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 & flag) == flag && flag != JsonSchemaType.Null) + if (schemaType.Value.HasFlag(flag) && flag != JsonSchemaType.Null) { // Write the non-null flag value to the writer - writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(flag)); + writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier()); } } if (!Nullable) From b8063234a3a8fdca1d5b84de58455290b4934d24 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 18:18:52 +0300 Subject: [PATCH 16/17] Clean up tests --- test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs | 2 +- test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs index e4f33871e..ad2c9ffdb 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiCallbackTests.cs @@ -96,7 +96,7 @@ public class OpenApiCallbackTests [Theory] [InlineData(true)] - //[InlineData(false)] + [InlineData(false)] public async Task SerializeAdvancedCallbackAsV3JsonWorksAsync(bool produceTerseOutput) { // Arrange diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index f6ea38fc9..ef18b4cfb 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -190,10 +190,10 @@ namespace Microsoft.OpenApi.Extensions } public static class OpenApiTypeMapper { - public static Microsoft.OpenApi.Models.JsonSchemaType IdentifierToEnumType(string identifier) { } 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(Microsoft.OpenApi.Models.JsonSchemaType? schemaType) { } + public static string ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType? schemaType) { } + public static Microsoft.OpenApi.Models.JsonSchemaType ToJsonSchemaType(this string identifier) { } } public static class StringExtensions { From 5eb0010503a48ade154dcf5f67895a78ff8b3ed9 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 30 Oct 2024 18:36:55 +0300 Subject: [PATCH 17/17] Add null check --- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index 8df393ea6..59b7e2025 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -476,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); @@ -657,7 +660,10 @@ internal void SerializeAsV2( writer.WriteStartObject(); // type - SerializeTypeProperty(Type, writer, OpenApiSpecVersion.OpenApi2_0); + if (Type is not null) + { + SerializeTypeProperty(Type, writer, OpenApiSpecVersion.OpenApi2_0); + } // description writer.WriteProperty(OpenApiConstants.Description, Description); @@ -836,7 +842,7 @@ private static int CountEnumSetFlags(JsonSchemaType? schemaType) // Check each flag in the enum foreach (JsonSchemaType value in System.Enum.GetValues(typeof(JsonSchemaType))) { - // Ignore the None flag and check if the flag is set + // Check if the flag is set if (schemaType.Value.HasFlag(value)) { count++;