diff --git a/src/Microsoft.OpenApi.YamlReader/YamlConverter.cs b/src/Microsoft.OpenApi.YamlReader/YamlConverter.cs index 1cafea77b..9239ed926 100644 --- a/src/Microsoft.OpenApi.YamlReader/YamlConverter.cs +++ b/src/Microsoft.OpenApi.YamlReader/YamlConverter.cs @@ -130,7 +130,9 @@ private static JsonValue ToJsonValue(this YamlScalarNode yaml) { return yaml.Style switch { - ScalarStyle.Plain when decimal.TryParse(yaml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d) => JsonValue.Create(d), + // JsonNode.Parse will create a JsonValue that is suitable for representing any numeric value (it's wrapping JsonElement). + // So, if we call '.TryGetValue' on it and the underlying value can be represented as int, it will succeed. + ScalarStyle.Plain when decimal.TryParse(yaml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d) => (JsonNode.Parse(yaml.Value) as JsonValue) ?? JsonValue.Create(d), ScalarStyle.Plain when bool.TryParse(yaml.Value, out var b) => JsonValue.Create(b), ScalarStyle.Plain when YamlNullRepresentations.Contains(yaml.Value) => (JsonValue)JsonNullSentinel.JsonNull.DeepClone(), ScalarStyle.Plain => JsonValue.Create(yaml.Value), diff --git a/src/Microsoft.OpenApi/Reader/JsonNodeHelper.cs b/src/Microsoft.OpenApi/Reader/JsonNodeHelper.cs index 3ccfb2116..97577ec2c 100644 --- a/src/Microsoft.OpenApi/Reader/JsonNodeHelper.cs +++ b/src/Microsoft.OpenApi/Reader/JsonNodeHelper.cs @@ -1,10 +1,11 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text.Json; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader @@ -159,9 +160,47 @@ public static Dictionary> CreateArrayMap(this JsonNode? no { var scalarNode = node is JsonValue value ? value : throw new OpenApiException("Expected scalar value."); + // It's much more efficient to call scalarNode.TryGetValue than to call scalarNode.GetValue() and then convert to string. + // When asking for "object" type, if scalarNode is JsonValueOfElement (internal type in STJ), we will get back + // a boxed JsonElement (paying the cost of unnecessary boxing allocation), and we then call JsonElement.ToString. + // So, we first check if we can get the string value directly, and only if that fails, we fallback to the expensive code. + if (scalarNode.TryGetValue(out var stringValue)) + { + return stringValue; + } + return Convert.ToString(scalarNode.GetValue(), CultureInfo.InvariantCulture); } + public static bool GetScalarBoolValue(this JsonNode? node) + { + var scalarNode = node is JsonValue value ? value : throw new OpenApiException("Expected scalar value."); + return scalarNode.GetValue(); + } + + public static int GetScalarIntValue(this JsonNode? node) + { + var scalarNode = node is JsonValue value && value.GetValueKind() == JsonValueKind.Number ? value : throw new OpenApiException("Expected numeric scalar value."); + + if (scalarNode.TryGetValue(out var intValue)) + { + return intValue; + } + + return Convert.ToInt32(scalarNode.GetValue()); + } + + public static uint GetScalarUIntValue(this JsonNode? node) + { + var scalarNode = node is JsonValue value && value.GetValueKind() == JsonValueKind.Number ? value : throw new OpenApiException("Expected numeric scalar value."); + if (scalarNode.TryGetValue(out var uintValue)) + { + return uintValue; + } + + return Convert.ToUInt32(scalarNode.GetValue()); + } + public static string? GetReferencePointer(this JsonObject jsonObject) { return jsonObject.TryGetPropertyValue("$ref", out var refNode) ? refNode?.GetScalarValue() : null; diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs index c97788ebd..97663e27c 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.Text.Json.Nodes; @@ -67,7 +67,7 @@ internal static partial class OpenApiV2Deserializer }, { "exclusiveMaximum", - (o, n, _, _) => GetOrCreateSchema(o).IsExclusiveMaximum = bool.Parse(n.GetScalarValue()!) + (o, n, _, _) => GetOrCreateSchema(o).IsExclusiveMaximum = n.GetScalarBoolValue() }, { "minimum", @@ -76,34 +76,26 @@ internal static partial class OpenApiV2Deserializer var min = n.GetScalarValue(); if (!string.IsNullOrEmpty(min)) { - GetOrCreateSchema(o).Minimum = min; - } + GetOrCreateSchema(o).Minimum = min; + } } }, { "exclusiveMinimum", - (o, n, _, _) => GetOrCreateSchema(o).IsExclusiveMinimum = bool.Parse(n.GetScalarValue()!) + (o, n, _, _) => GetOrCreateSchema(o).IsExclusiveMinimum = n.GetScalarBoolValue() }, { "maxLength", (o, n, _, _) => { - var maxLength = n.GetScalarValue(); - if (maxLength != null) - { - GetOrCreateSchema(o).MaxLength = int.Parse(maxLength, CultureInfo.InvariantCulture); - } + GetOrCreateSchema(o).MaxLength = n.GetScalarIntValue(); } }, { "minLength", (o, n, _, _) => { - var minLength = n.GetScalarValue(); - if (minLength != null) - { - GetOrCreateSchema(o).MinLength = int.Parse(minLength, CultureInfo.InvariantCulture); - } + GetOrCreateSchema(o).MinLength = n.GetScalarIntValue(); } }, { @@ -114,33 +106,21 @@ internal static partial class OpenApiV2Deserializer "maxItems", (o, n, _, _) => { - var maxItems = n.GetScalarValue(); - if (maxItems != null) - { - GetOrCreateSchema(o).MaxItems = int.Parse(maxItems, CultureInfo.InvariantCulture); - } + GetOrCreateSchema(o).MaxItems = n.GetScalarIntValue(); } }, { "minItems", (o, n, _, _) => { - var minItems = n.GetScalarValue(); - if (minItems != null) - { - GetOrCreateSchema(o).MinItems = int.Parse(minItems, CultureInfo.InvariantCulture); - } + GetOrCreateSchema(o).MinItems = n.GetScalarIntValue(); } }, { "uniqueItems", (o, n, _, _) => { - var uniqueItems = n.GetScalarValue(); - if (uniqueItems != null) - { - GetOrCreateSchema(o).UniqueItems = bool.Parse(uniqueItems); - } + GetOrCreateSchema(o).UniqueItems = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs index 789ca816b..c74671975 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiOperationDeserializer.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; @@ -82,11 +82,7 @@ internal static partial class OpenApiV2Deserializer "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs index e020aedc1..b20f94e98 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.Text.Json.Nodes; @@ -34,33 +34,21 @@ internal static partial class OpenApiV2Deserializer "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, { "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { "allowEmptyValue", (o, n, _, _) => { - var allowEmptyValue = n.GetScalarValue(); - if (allowEmptyValue != null) - { - o.AllowEmptyValue = bool.Parse(allowEmptyValue); - } + o.AllowEmptyValue = n.GetScalarBoolValue(); } }, { @@ -124,33 +112,21 @@ internal static partial class OpenApiV2Deserializer "maxLength", (o, n, _, _) => { - var maxLength = n.GetScalarValue(); - if (maxLength != null) - { - GetOrCreateSchema(o).MaxLength = int.Parse(maxLength, CultureInfo.InvariantCulture); - } + GetOrCreateSchema(o).MaxLength = n.GetScalarIntValue(); } }, { "minLength", (o, n, _, _) => { - var minLength = n.GetScalarValue(); - if (minLength != null) - { - GetOrCreateSchema(o).MinLength = int.Parse(minLength, CultureInfo.InvariantCulture); - } + GetOrCreateSchema(o).MinLength = n.GetScalarIntValue(); } }, { "readOnly", (o, n, _, _) => { - var readOnly = n.GetScalarValue(); - if (readOnly != null) - { - GetOrCreateSchema(o).ReadOnly = bool.Parse(readOnly); - } + GetOrCreateSchema(o).ReadOnly = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs index af7f33d9c..0be43639b 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs @@ -48,7 +48,7 @@ internal static partial class OpenApiV2Deserializer }, { "exclusiveMaximum", - (o, n, _, _) => o.IsExclusiveMaximum = bool.Parse(n.GetScalarValue()!) + (o, n, _, _) => o.IsExclusiveMaximum = n.GetScalarBoolValue() }, { "minimum", @@ -63,7 +63,7 @@ internal static partial class OpenApiV2Deserializer }, { "exclusiveMinimum", - (o, n, _, _) => o.IsExclusiveMinimum = bool.Parse(n.GetScalarValue()!) + (o, n, _, _) => o.IsExclusiveMinimum = n.GetScalarBoolValue() }, { "maxLength", @@ -72,7 +72,7 @@ internal static partial class OpenApiV2Deserializer var maxLength = n.GetScalarValue(); if (maxLength != null) { - o.MaxLength = int.Parse(maxLength, CultureInfo.InvariantCulture); + o.MaxLength = n.GetScalarIntValue(); } } }, @@ -80,11 +80,7 @@ internal static partial class OpenApiV2Deserializer "minLength", (o, n, _, _) => { - var minLength = n.GetScalarValue(); - if (minLength != null) - { - o.MinLength = int.Parse(minLength, CultureInfo.InvariantCulture); - } + o.MinLength = n.GetScalarIntValue(); } }, { @@ -95,55 +91,35 @@ internal static partial class OpenApiV2Deserializer "maxItems", (o, n, _, _) => { - var maxItems = n.GetScalarValue(); - if (maxItems != null) - { - o.MaxItems = int.Parse(maxItems, CultureInfo.InvariantCulture); - } + o.MaxItems = n.GetScalarIntValue(); } }, { "minItems", (o, n, _, _) => { - var minItems = n.GetScalarValue(); - if (minItems != null) - { - o.MinItems = int.Parse(minItems, CultureInfo.InvariantCulture); - } + o.MinItems = n.GetScalarIntValue(); } }, { "uniqueItems", (o, n, _, _) => { - var uniqueItems = n.GetScalarValue(); - if (uniqueItems != null) - { - o.UniqueItems = bool.Parse(uniqueItems); - } + o.UniqueItems = n.GetScalarBoolValue(); } }, { "maxProperties", (o, n, _, _) => { - var maxProps = n.GetScalarValue(); - if (maxProps != null) - { - o.MaxProperties = int.Parse(maxProps, CultureInfo.InvariantCulture); - } + o.MaxProperties = n.GetScalarIntValue(); } }, { "minProperties", (o, n, _, _) => { - var minProps = n.GetScalarValue(); - if (minProps != null) - { - o.MinProperties = int.Parse(minProps, CultureInfo.InvariantCulture); - } + o.MinProperties = n.GetScalarIntValue(); } }, { @@ -188,11 +164,7 @@ internal static partial class OpenApiV2Deserializer { if (n is JsonValue) { - var value = n.GetScalarValue(); - if (value is not null) - { - o.AdditionalPropertiesAllowed = bool.Parse(value); - } + o.AdditionalPropertiesAllowed = n.GetScalarBoolValue(); } else { @@ -216,7 +188,7 @@ internal static partial class OpenApiV2Deserializer OpenApiConstants.NullableExtension, (o, n, _, _) => { - if (bool.TryParse(n.GetScalarValue(), out var parsed) && parsed) + if (n.GetScalarBoolValue()) { if (o.Type is not null && o.Type != 0) { @@ -248,11 +220,7 @@ internal static partial class OpenApiV2Deserializer "readOnly", (o, n, _, _) => { - var readOnly = n.GetScalarValue(); - if (readOnly is not null) - { - o.ReadOnly = bool.Parse(readOnly); - } + o.ReadOnly = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V2/OpenApiXmlDeserializer.cs b/src/Microsoft.OpenApi/Reader/V2/OpenApiXmlDeserializer.cs index 2101964a5..83cc81750 100644 --- a/src/Microsoft.OpenApi/Reader/V2/OpenApiXmlDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V2/OpenApiXmlDeserializer.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.Nodes; @@ -41,26 +41,18 @@ internal static partial class OpenApiV2Deserializer "attribute", (o, n, _, _) => { - var attribute = n.GetScalarValue(); - if (attribute is not null) - { #pragma warning disable CS0618 // Type or member is obsolete - o.Attribute = bool.Parse(attribute); + o.Attribute = n.GetScalarBoolValue(); #pragma warning restore CS0618 // Type or member is obsolete - } } }, { "wrapped", (o, n, _, _) => { - var wrapped = n.GetScalarValue(); - if (wrapped is not null) - { #pragma warning disable CS0618 // Type or member is obsolete - o.Wrapped = bool.Parse(wrapped); + o.Wrapped = n.GetScalarBoolValue(); #pragma warning restore CS0618 // Type or member is obsolete - } } }, }; diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiEncodingDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiEncodingDeserializer.cs index b1e9db44b..04bfd1fca 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiEncodingDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/OpenApiEncodingDeserializer.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.Nodes; @@ -38,22 +38,14 @@ internal static partial class OpenApiV3Deserializer "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode != null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved != null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, }; diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiHeaderDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiHeaderDeserializer.cs index c82b47bd8..c3e24872b 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiHeaderDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/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.Text.Json.Nodes; @@ -23,44 +23,28 @@ internal static partial class OpenApiV3Deserializer "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, { "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { "allowEmptyValue", (o, n, _, _) => { - var allowEmptyVal = n.GetScalarValue(); - if (allowEmptyVal != null) - { - o.AllowEmptyValue = bool.Parse(allowEmptyVal); - } + o.AllowEmptyValue = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved != null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, { @@ -78,11 +62,7 @@ internal static partial class OpenApiV3Deserializer "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode != null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiOperationDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiOperationDeserializer.cs index 80c8c0638..6c19944c5 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiOperationDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/OpenApiOperationDeserializer.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; @@ -72,11 +72,7 @@ internal static partial class OpenApiV3Deserializer "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiParameterDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiParameterDeserializer.cs index 1f589e2ff..68c20ba1c 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiParameterDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/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.Text.Json.Nodes; @@ -38,44 +38,28 @@ internal static partial class OpenApiV3Deserializer "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, { "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { "allowEmptyValue", (o, n, _, _) => { - var allowEmptyValue = n.GetScalarValue(); - if (allowEmptyValue != null) - { - o.AllowEmptyValue = bool.Parse(allowEmptyValue); - } + o.AllowEmptyValue = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved != null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, { @@ -93,11 +77,7 @@ internal static partial class OpenApiV3Deserializer "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode != null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiRequestBodyDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiRequestBodyDeserializer.cs index 8aee2e9a8..15cca6ac7 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiRequestBodyDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/OpenApiRequestBodyDeserializer.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.Nodes; @@ -28,11 +28,7 @@ internal static partial class OpenApiV3Deserializer "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, }; diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs index 48dde9d5e..53a291d7d 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs @@ -49,7 +49,7 @@ internal static partial class OpenApiV3Deserializer }, { "exclusiveMaximum", - (o, n, _, _) => o.IsExclusiveMaximum = bool.Parse(n.GetScalarValue()!) + (o, n, _, _) => o.IsExclusiveMaximum = n.GetScalarBoolValue() }, { "minimum", @@ -64,28 +64,20 @@ internal static partial class OpenApiV3Deserializer }, { "exclusiveMinimum", - (o, n, _, _) => o.IsExclusiveMinimum = bool.Parse(n.GetScalarValue()!) + (o, n, _, _) => o.IsExclusiveMinimum = n.GetScalarBoolValue() }, { "maxLength", (o, n, _, _) => { - var maxLength = n.GetScalarValue(); - if (maxLength != null) - { - o.MaxLength = int.Parse(maxLength, CultureInfo.InvariantCulture); - } + o.MaxLength = n.GetScalarIntValue(); } }, { "minLength", (o, n, _, _) => { - var minLength = n.GetScalarValue(); - if (minLength != null) - { - o.MinLength = int.Parse(minLength, CultureInfo.InvariantCulture); - } + o.MinLength = n.GetScalarIntValue(); } }, { @@ -96,55 +88,35 @@ internal static partial class OpenApiV3Deserializer "maxItems", (o, n, _, _) => { - var maxItems = n.GetScalarValue(); - if (maxItems != null) - { - o.MaxItems = int.Parse(maxItems, CultureInfo.InvariantCulture); - } + o.MaxItems = n.GetScalarIntValue(); } }, { "minItems", (o, n, _, _) => { - var minItems = n.GetScalarValue(); - if (minItems != null) - { - o.MinItems = int.Parse(minItems, CultureInfo.InvariantCulture); - } + o.MinItems = n.GetScalarIntValue(); } }, { "uniqueItems", (o, n, _, _) => { - var uniqueItems = n.GetScalarValue(); - if (uniqueItems != null) - { - o.UniqueItems = bool.Parse(uniqueItems); - } + o.UniqueItems = n.GetScalarBoolValue(); } }, { "maxProperties", (o, n, _, _) => { - var maxProps = n.GetScalarValue(); - if (maxProps != null) - { - o.MaxProperties = int.Parse(maxProps, CultureInfo.InvariantCulture); - } + o.MaxProperties = n.GetScalarIntValue(); } }, { "minProperties", (o, n, _, _) => { - var minProps = n.GetScalarValue(); - if (minProps != null) - { - o.MinProperties = int.Parse(minProps, CultureInfo.InvariantCulture); - } + o.MinProperties = n.GetScalarIntValue(); } }, { @@ -195,11 +167,7 @@ internal static partial class OpenApiV3Deserializer { if (n is JsonValue) { - var value = n.GetScalarValue(); - if (value is not null) - { - o.AdditionalPropertiesAllowed = bool.Parse(value); - } + o.AdditionalPropertiesAllowed = n.GetScalarBoolValue(); } else { @@ -223,7 +191,7 @@ internal static partial class OpenApiV3Deserializer "nullable", (o, n, _, _) => { - if (bool.TryParse(n.GetScalarValue(), out var parsed) && parsed) + if (n.GetScalarBoolValue()) { if (o.Type is not null && o.Type != 0) { @@ -250,22 +218,14 @@ internal static partial class OpenApiV3Deserializer "readOnly", (o, n, _, _) => { - var readOnly = n.GetScalarValue(); - if (readOnly != null) - { - o.ReadOnly = bool.Parse(readOnly); - } + o.ReadOnly = n.GetScalarBoolValue(); } }, { "writeOnly", (o, n, _, _) => { - var writeOnly = n.GetScalarValue(); - if (writeOnly != null) - { - o.WriteOnly = bool.Parse(writeOnly); - } + o.WriteOnly = n.GetScalarBoolValue(); } }, { @@ -284,11 +244,7 @@ internal static partial class OpenApiV3Deserializer "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { @@ -301,11 +257,7 @@ internal static partial class OpenApiV3Deserializer { if (n is JsonValue) { - var value = n.GetScalarValue(); - if (value is not null) - { - o.UnevaluatedProperties = bool.Parse(value); - } + o.UnevaluatedProperties = n.GetScalarBoolValue(); } else { @@ -337,22 +289,14 @@ internal static partial class OpenApiV3Deserializer OpenApiConstants.MaxContainsExtension, (o, n, _, _) => { - var maxContains = n.GetScalarValue(); - if (maxContains != null) - { - o.MaxContains = uint.Parse(maxContains, CultureInfo.InvariantCulture); - } + o.MaxContains = n.GetScalarUIntValue(); } }, { OpenApiConstants.MinContainsExtension, (o, n, _, _) => { - var minContains = n.GetScalarValue(); - if (minContains != null) - { - o.MinContains = uint.Parse(minContains, CultureInfo.InvariantCulture); - } + o.MinContains = n.GetScalarUIntValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiSecuritySchemeDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiSecuritySchemeDeserializer.cs index 4cb13d995..938597aba 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiSecuritySchemeDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/OpenApiSecuritySchemeDeserializer.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.Nodes; @@ -78,11 +78,7 @@ internal static partial class OpenApiV3Deserializer { if (p.Equals("x-oai-deprecated", StringComparison.OrdinalIgnoreCase)) { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } else { diff --git a/src/Microsoft.OpenApi/Reader/V3/OpenApiXmlDeserializer.cs b/src/Microsoft.OpenApi/Reader/V3/OpenApiXmlDeserializer.cs index 3e210e8bc..8f2454eab 100644 --- a/src/Microsoft.OpenApi/Reader/V3/OpenApiXmlDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V3/OpenApiXmlDeserializer.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.Nodes; @@ -38,26 +38,18 @@ internal static partial class OpenApiV3Deserializer "attribute", (o, n, _, _) => { - var attribute = n.GetScalarValue(); - if (attribute is not null) - { #pragma warning disable CS0618 // Type or member is obsolete - o.Attribute = bool.Parse(attribute); + o.Attribute = n.GetScalarBoolValue(); #pragma warning restore CS0618 // Type or member is obsolete - } } }, { "wrapped", (o, n, _, _) => { - var wrapped = n.GetScalarValue(); - if (wrapped is not null) - { #pragma warning disable CS0618 // Type or member is obsolete - o.Wrapped = bool.Parse(wrapped); + o.Wrapped = n.GetScalarBoolValue(); #pragma warning restore CS0618 // Type or member is obsolete - } } }, }; diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiEncodingDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiEncodingDeserializer.cs index 87ee88511..9bca83a60 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiEncodingDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiEncodingDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V31; @@ -35,21 +35,13 @@ internal static partial class OpenApiV31Deserializer { "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode is not null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved is not null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, }; diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiHeaderDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiHeaderDeserializer.cs index 10eac3dea..430c85d20 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiHeaderDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiHeaderDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V31 @@ -21,44 +21,28 @@ internal static partial class OpenApiV31Deserializer "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, { "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { "allowEmptyValue", (o, n, _, _) => { - var allowEmptyVal = n.GetScalarValue(); - if (allowEmptyVal != null) - { - o.AllowEmptyValue = bool.Parse(allowEmptyVal); - } + o.AllowEmptyValue = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved != null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, { @@ -75,11 +59,7 @@ internal static partial class OpenApiV31Deserializer "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode != null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiOAuthFlowDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiOAuthFlowDeserializer.cs index ab995a391..27f8a25aa 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiOAuthFlowDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiOAuthFlowDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; using System.Linq; @@ -10,7 +10,7 @@ namespace Microsoft.OpenApi.Reader.V31 /// internal static partial class OpenApiV31Deserializer { - private static readonly FixedFieldMap _oAuthFlowFixedFileds = + private static readonly FixedFieldMap _oAuthFlowFixedFields = new() { { @@ -74,7 +74,7 @@ public static OpenApiOAuthFlow LoadOAuthFlow(JsonNode node, OpenApiDocument host var jsonObject = node.CheckMapNode("OAuthFlow", context); var oauthFlow = new OpenApiOAuthFlow(); - ParseMap(jsonObject, oauthFlow, _oAuthFlowFixedFileds, _oAuthFlowPatternFields, hostDocument, context); + ParseMap(jsonObject, oauthFlow, _oAuthFlowFixedFields, _oAuthFlowPatternFields, hostDocument, context); return oauthFlow; } diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiOperationDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiOperationDeserializer.cs index e67572cc6..48d4ddad8 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiOperationDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiOperationDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text.Json.Nodes; @@ -85,11 +85,7 @@ internal static partial class OpenApiV31Deserializer "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiParameterDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiParameterDeserializer.cs index 2755e5324..bb4b91f1a 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiParameterDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiParameterDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V31 @@ -38,44 +38,28 @@ internal static partial class OpenApiV31Deserializer "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, { "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { "allowEmptyValue", (o, n, _, _) => { - var allowEmptyValue = n.GetScalarValue(); - if (allowEmptyValue != null) - { - o.AllowEmptyValue = bool.Parse(allowEmptyValue); - } + o.AllowEmptyValue = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved != null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, { @@ -91,11 +75,7 @@ internal static partial class OpenApiV31Deserializer { "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode != null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiRequestBodyDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiRequestBodyDeserializer.cs index 1efb61a82..b0963058e 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiRequestBodyDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiRequestBodyDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V31 @@ -27,11 +27,7 @@ internal static partial class OpenApiV31Deserializer { "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, }; diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs index c14237f83..02bed42d6 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs @@ -32,7 +32,7 @@ internal static partial class OpenApiV31Deserializer }, { "$vocabulary", - (o, n, _, c) => o.Vocabulary = n.CreateSimpleMap(LoadBool, c).ToDictionary(kvp => kvp.Key, kvp => kvp.Value ?? false) + (o, n, _, c) => o.Vocabulary = n.CreateSimpleMap(LoadBool, c) }, { "$dynamicRef", @@ -95,22 +95,14 @@ internal static partial class OpenApiV31Deserializer "maxLength", (o, n, _, _) => { - var maxLength = n.GetScalarValue(); - if (maxLength != null) - { - o.MaxLength = int.Parse(maxLength, CultureInfo.InvariantCulture); - } + o.MaxLength = n.GetScalarIntValue(); } }, { "minLength", (o, n, _, _) => { - var minLength = n.GetScalarValue(); - if (minLength != null) - { - o.MinLength = int.Parse(minLength, CultureInfo.InvariantCulture); - } + o.MinLength = n.GetScalarIntValue(); } }, { @@ -121,33 +113,21 @@ internal static partial class OpenApiV31Deserializer "maxItems", (o, n, _, _) => { - var maxItems = n.GetScalarValue(); - if (maxItems != null) - { - o.MaxItems = int.Parse(maxItems, CultureInfo.InvariantCulture); - } + o.MaxItems = n.GetScalarIntValue(); } }, { "minItems", (o, n, _, _) => { - var minItems = n.GetScalarValue(); - if (minItems != null) - { - o.MinItems = int.Parse(minItems, CultureInfo.InvariantCulture); - } + o.MinItems = n.GetScalarIntValue(); } }, { "uniqueItems", (o, n, _, _) => { - var uniqueItems = n.GetScalarValue(); - if (uniqueItems != null) - { - o.UniqueItems = bool.Parse(uniqueItems); - } + o.UniqueItems = n.GetScalarBoolValue(); } }, { @@ -158,22 +138,14 @@ internal static partial class OpenApiV31Deserializer OpenApiConstants.MaxContains, (o, n, _, _) => { - var maxContains = n.GetScalarValue(); - if (maxContains != null) - { - o.MaxContains = uint.Parse(maxContains, CultureInfo.InvariantCulture); - } + o.MaxContains = n.GetScalarUIntValue(); } }, { OpenApiConstants.MinContains, (o, n, _, _) => { - var minContains = n.GetScalarValue(); - if (minContains != null) - { - o.MinContains = uint.Parse(minContains, CultureInfo.InvariantCulture); - } + o.MinContains = n.GetScalarUIntValue(); } }, { @@ -183,11 +155,7 @@ internal static partial class OpenApiV31Deserializer // Handle both boolean (false/true) and schema object cases if (n is JsonValue) { - var value = n.GetScalarValue(); - if (value is not null) - { - o.UnevaluatedProperties = bool.Parse(value); - } + o.UnevaluatedProperties = n.GetScalarBoolValue(); } else { @@ -212,22 +180,14 @@ internal static partial class OpenApiV31Deserializer "maxProperties", (o, n, _, _) => { - var maxProps = n.GetScalarValue(); - if (maxProps != null) - { - o.MaxProperties = int.Parse(maxProps, CultureInfo.InvariantCulture); - } + o.MaxProperties = n.GetScalarIntValue(); } }, { "minProperties", (o, n, _, _) => { - var minProps = n.GetScalarValue(); - if (minProps != null) - { - o.MinProperties = int.Parse(minProps, CultureInfo.InvariantCulture); - } + o.MinProperties = n.GetScalarIntValue(); } }, { @@ -302,11 +262,7 @@ internal static partial class OpenApiV31Deserializer { if (n is JsonValue) { - var value = n.GetScalarValue(); - if (value is not null) - { - o.AdditionalPropertiesAllowed = bool.Parse(value); - } + o.AdditionalPropertiesAllowed = n.GetScalarBoolValue(); } else { @@ -334,22 +290,14 @@ internal static partial class OpenApiV31Deserializer "readOnly", (o, n, _, _) => { - var readOnly = n.GetScalarValue(); - if (readOnly != null) - { - o.ReadOnly = bool.Parse(readOnly); - } + o.ReadOnly = n.GetScalarBoolValue(); } }, { "writeOnly", (o, n, _, _) => { - var writeOnly = n.GetScalarValue(); - if (writeOnly != null) - { - o.WriteOnly = bool.Parse(writeOnly); - } + o.WriteOnly = n.GetScalarBoolValue(); } }, { @@ -372,11 +320,7 @@ internal static partial class OpenApiV31Deserializer "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiSecuritySchemeDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiSecuritySchemeDeserializer.cs index 4e02a2bf6..e26485d7f 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiSecuritySchemeDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiSecuritySchemeDeserializer.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.Nodes; @@ -85,11 +85,7 @@ internal static partial class OpenApiV31Deserializer { if (p.Equals("x-oai-deprecated", StringComparison.OrdinalIgnoreCase)) { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } else { diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiV31Deserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiV31Deserializer.cs index 81a7811ff..d0d7c688e 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiV31Deserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiV31Deserializer.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; @@ -144,10 +144,9 @@ private static IOpenApiExtension LoadExtension(string name, JsonNode node, Parsi return node.GetScalarValue(); } - private static bool? LoadBool(JsonNode node) + private static bool LoadBool(JsonNode node) { - var value = node.GetScalarValue(); - return value is not null ? bool.Parse(value) : null; + return node.GetScalarBoolValue(); } private static (string, string?) GetReferenceIdAndExternalResource(string pointer) diff --git a/src/Microsoft.OpenApi/Reader/V31/OpenApiXmlDeserializer.cs b/src/Microsoft.OpenApi/Reader/V31/OpenApiXmlDeserializer.cs index 8b76c2ca9..eb5545873 100644 --- a/src/Microsoft.OpenApi/Reader/V31/OpenApiXmlDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V31/OpenApiXmlDeserializer.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.Nodes; @@ -40,26 +40,18 @@ internal static partial class OpenApiV31Deserializer "attribute", (o, n, _, _) => { - var attribute = n.GetScalarValue(); - if (attribute is not null) - { #pragma warning disable CS0618 // Type or member is obsolete - o.Attribute = bool.Parse(attribute); + o.Attribute = n.GetScalarBoolValue(); #pragma warning restore CS0618 // Type or member is obsolete - } } }, { "wrapped", (o, n, _, _) => { - var wrapped = n.GetScalarValue(); - if (wrapped is not null) - { #pragma warning disable CS0618 // Type or member is obsolete - o.Wrapped = bool.Parse(wrapped); + o.Wrapped = n.GetScalarBoolValue(); #pragma warning restore CS0618 // Type or member is obsolete - } } } }; diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiEncodingDeserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiEncodingDeserializer.cs index cf37779a1..d448bfb95 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiEncodingDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiEncodingDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V32 @@ -54,21 +54,13 @@ internal static partial class OpenApiV32Deserializer { "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode is not null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved is not null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, }; diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiHeaderDeserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiHeaderDeserializer.cs index f7cd702d3..d72e7b32e 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiHeaderDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiHeaderDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V32 @@ -21,44 +21,28 @@ internal static partial class OpenApiV32Deserializer "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, { "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { "allowEmptyValue", (o, n, _, _) => { - var allowEmptyVal = n.GetScalarValue(); - if (allowEmptyVal != null) - { - o.AllowEmptyValue = bool.Parse(allowEmptyVal); - } + o.AllowEmptyValue = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved != null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, { @@ -75,11 +59,7 @@ internal static partial class OpenApiV32Deserializer "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode != null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiOAuthFlowDeserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiOAuthFlowDeserializer.cs index fc02e997f..9875de4a9 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiOAuthFlowDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiOAuthFlowDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; using System.Linq; @@ -10,7 +10,7 @@ namespace Microsoft.OpenApi.Reader.V32 /// internal static partial class OpenApiV32Deserializer { - private static readonly FixedFieldMap _oAuthFlowFixedFileds = + private static readonly FixedFieldMap _oAuthFlowFixedFields = new() { { @@ -71,7 +71,7 @@ public static OpenApiOAuthFlow LoadOAuthFlow(JsonNode node, OpenApiDocument host var jsonObject = node.CheckMapNode("OAuthFlow", context); var oauthFlow = new OpenApiOAuthFlow(); - ParseMap(jsonObject, oauthFlow, _oAuthFlowFixedFileds, _oAuthFlowPatternFields, hostDocument, context); + ParseMap(jsonObject, oauthFlow, _oAuthFlowFixedFields, _oAuthFlowPatternFields, hostDocument, context); return oauthFlow; } diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiOperationDeserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiOperationDeserializer.cs index 98adb58c1..1280aaa4c 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiOperationDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiOperationDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text.Json.Nodes; @@ -85,11 +85,7 @@ internal static partial class OpenApiV32Deserializer "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiParameterDeserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiParameterDeserializer.cs index 5eadec1c9..8a7a93407 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiParameterDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiParameterDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V32 @@ -38,44 +38,28 @@ internal static partial class OpenApiV32Deserializer "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, { "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { "allowEmptyValue", (o, n, _, _) => { - var allowEmptyValue = n.GetScalarValue(); - if (allowEmptyValue != null) - { - o.AllowEmptyValue = bool.Parse(allowEmptyValue); - } + o.AllowEmptyValue = n.GetScalarBoolValue(); } }, { "allowReserved", (o, n, _, _) => { - var allowReserved = n.GetScalarValue(); - if (allowReserved != null) - { - o.AllowReserved = bool.Parse(allowReserved); - } + o.AllowReserved = n.GetScalarBoolValue(); } }, { @@ -91,11 +75,7 @@ internal static partial class OpenApiV32Deserializer { "explode", (o, n, _, _) => { - var explode = n.GetScalarValue(); - if (explode != null) - { - o.Explode = bool.Parse(explode); - } + o.Explode = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiRequestBodyDeserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiRequestBodyDeserializer.cs index a3ccbfcda..0a0867483 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiRequestBodyDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiRequestBodyDeserializer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text.Json.Nodes; namespace Microsoft.OpenApi.Reader.V32 @@ -27,11 +27,7 @@ internal static partial class OpenApiV32Deserializer { "required", (o, n, _, _) => { - var required = n.GetScalarValue(); - if (required != null) - { - o.Required = bool.Parse(required); - } + o.Required = n.GetScalarBoolValue(); } }, }; diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiSchemaDeserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiSchemaDeserializer.cs index 892822a45..df8333c6d 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiSchemaDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiSchemaDeserializer.cs @@ -32,7 +32,7 @@ internal static partial class OpenApiV32Deserializer }, { "$vocabulary", - (o, n, _, c) => o.Vocabulary = n.CreateSimpleMap(LoadBool, c).ToDictionary(kvp => kvp.Key, kvp => kvp.Value ?? false) + (o, n, _, c) => o.Vocabulary = n.CreateSimpleMap(LoadBool, c) }, { "$dynamicRef", @@ -95,22 +95,14 @@ internal static partial class OpenApiV32Deserializer "maxLength", (o, n, _, _) => { - var maxLength = n.GetScalarValue(); - if (maxLength != null) - { - o.MaxLength = int.Parse(maxLength, CultureInfo.InvariantCulture); - } + o.MaxLength = n.GetScalarIntValue(); } }, { "minLength", (o, n, _, _) => { - var minLength = n.GetScalarValue(); - if (minLength != null) - { - o.MinLength = int.Parse(minLength, CultureInfo.InvariantCulture); - } + o.MinLength = n.GetScalarIntValue(); } }, { @@ -121,33 +113,21 @@ internal static partial class OpenApiV32Deserializer "maxItems", (o, n, _, _) => { - var maxItems = n.GetScalarValue(); - if (maxItems != null) - { - o.MaxItems = int.Parse(maxItems, CultureInfo.InvariantCulture); - } + o.MaxItems = n.GetScalarIntValue(); } }, { "minItems", (o, n, _, _) => { - var minItems = n.GetScalarValue(); - if (minItems != null) - { - o.MinItems = int.Parse(minItems, CultureInfo.InvariantCulture); - } + o.MinItems = n.GetScalarIntValue(); } }, { "uniqueItems", (o, n, _, _) => { - var uniqueItems = n.GetScalarValue(); - if (uniqueItems != null) - { - o.UniqueItems = bool.Parse(uniqueItems); - } + o.UniqueItems = n.GetScalarBoolValue(); } }, { @@ -158,22 +138,14 @@ internal static partial class OpenApiV32Deserializer OpenApiConstants.MaxContains, (o, n, _, _) => { - var maxContains = n.GetScalarValue(); - if (maxContains != null) - { - o.MaxContains = uint.Parse(maxContains, CultureInfo.InvariantCulture); - } + o.MaxContains = n.GetScalarUIntValue(); } }, { OpenApiConstants.MinContains, (o, n, _, _) => { - var minContains = n.GetScalarValue(); - if (minContains != null) - { - o.MinContains = uint.Parse(minContains, CultureInfo.InvariantCulture); - } + o.MinContains = n.GetScalarUIntValue(); } }, { @@ -183,11 +155,7 @@ internal static partial class OpenApiV32Deserializer // Handle both boolean (false/true) and schema object cases if (n is JsonValue) { - var value = n.GetScalarValue(); - if (value is not null) - { - o.UnevaluatedProperties = bool.Parse(value); - } + o.UnevaluatedProperties = n.GetScalarBoolValue(); } else { @@ -212,22 +180,14 @@ internal static partial class OpenApiV32Deserializer "maxProperties", (o, n, _, _) => { - var maxProps = n.GetScalarValue(); - if (maxProps != null) - { - o.MaxProperties = int.Parse(maxProps, CultureInfo.InvariantCulture); - } + o.MaxProperties = n.GetScalarIntValue(); } }, { "minProperties", (o, n, _, _) => { - var minProps = n.GetScalarValue(); - if (minProps != null) - { - o.MinProperties = int.Parse(minProps, CultureInfo.InvariantCulture); - } + o.MinProperties = n.GetScalarIntValue(); } }, { @@ -302,11 +262,7 @@ internal static partial class OpenApiV32Deserializer { if (n is JsonValue) { - var value = n.GetScalarValue(); - if (value is not null) - { - o.AdditionalPropertiesAllowed = bool.Parse(value); - } + o.AdditionalPropertiesAllowed = n.GetScalarBoolValue(); } else { @@ -334,22 +290,14 @@ internal static partial class OpenApiV32Deserializer "readOnly", (o, n, _, _) => { - var readOnly = n.GetScalarValue(); - if (readOnly != null) - { - o.ReadOnly = bool.Parse(readOnly); - } + o.ReadOnly = n.GetScalarBoolValue(); } }, { "writeOnly", (o, n, _, _) => { - var writeOnly = n.GetScalarValue(); - if (writeOnly != null) - { - o.WriteOnly = bool.Parse(writeOnly); - } + o.WriteOnly = n.GetScalarBoolValue(); } }, { @@ -372,11 +320,7 @@ internal static partial class OpenApiV32Deserializer "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } }, { diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiSecuritySchemeDeserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiSecuritySchemeDeserializer.cs index ca5ad4e48..6906d4f36 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiSecuritySchemeDeserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiSecuritySchemeDeserializer.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.Nodes; @@ -89,11 +89,7 @@ internal static partial class OpenApiV32Deserializer { "deprecated", (o, n, _, _) => { - var deprecated = n.GetScalarValue(); - if (deprecated != null) - { - o.Deprecated = bool.Parse(deprecated); - } + o.Deprecated = n.GetScalarBoolValue(); } } }; diff --git a/src/Microsoft.OpenApi/Reader/V32/OpenApiV32Deserializer.cs b/src/Microsoft.OpenApi/Reader/V32/OpenApiV32Deserializer.cs index 4031b45a1..b9880d770 100644 --- a/src/Microsoft.OpenApi/Reader/V32/OpenApiV32Deserializer.cs +++ b/src/Microsoft.OpenApi/Reader/V32/OpenApiV32Deserializer.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; @@ -144,10 +144,9 @@ private static IOpenApiExtension LoadExtension(string name, JsonNode node, Parsi return node.GetScalarValue(); } - private static bool? LoadBool(JsonNode node) + private static bool LoadBool(JsonNode node) { - var value = node.GetScalarValue(); - return value is not null ? bool.Parse(value) : null; + return node.GetScalarBoolValue(); } private static (string, string?) GetReferenceIdAndExternalResource(string pointer) diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiExampleTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiExampleTests.cs index 1f74aa4b3..40f8a9bd9 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiExampleTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiExampleTests.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.IO; @@ -30,6 +30,9 @@ public async Task ParseExampleWithDataValueExtensionShouldSucceed() Assert.NotNull(example.DataValue); Assert.Equal("Jane Smith", example.DataValue["name"].GetValue()); Assert.Equal(25, example.DataValue["age"].GetValue()); + Assert.Equal(25, example.DataValue["age"].GetValue()); + Assert.Equal(25u, example.DataValue["age"].GetValue()); + Assert.Equal((byte)25, example.DataValue["age"].GetValue()); } [Fact] diff --git a/test/Microsoft.OpenApi.Readers.Tests/V32Tests/OpenApiExampleTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V32Tests/OpenApiExampleTests.cs index 5d586f469..915c2e86a 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V32Tests/OpenApiExampleTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V32Tests/OpenApiExampleTests.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.IO; @@ -30,6 +30,10 @@ public async Task ParseExampleWithDataValueShouldSucceed() Assert.NotNull(example.DataValue); Assert.Equal("John Doe", example.DataValue["name"].GetValue()); Assert.Equal(30, example.DataValue["age"].GetValue()); + Assert.Equal(30, example.DataValue["age"].GetValue()); + Assert.Equal(30u, example.DataValue["age"].GetValue()); + Assert.Equal((byte)30, example.DataValue["age"].GetValue()); + } [Fact] diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs index e888b3d16..30459e12e 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiExampleTests.cs @@ -90,6 +90,10 @@ public async Task ParseExampleWithDataValueExtensionShouldSucceed() Assert.NotNull(example.DataValue); Assert.Equal("Alice Johnson", example.DataValue["name"].GetValue()); Assert.Equal(28, example.DataValue["age"].GetValue()); + Assert.Equal(28, example.DataValue["age"].GetValue()); + Assert.Equal(28u, example.DataValue["age"].GetValue()); + Assert.Equal((byte)28, example.DataValue["age"].GetValue()); + } [Fact]