diff --git a/src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs b/src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs index 1235e68b0..a6df2444b 100644 --- a/src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs +++ b/src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs @@ -11,6 +11,8 @@ using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Writers; using System.Text.Json.Nodes; +using System.Text.Json; +using System.Globalization; namespace Microsoft.OpenApi.MicrosoftExtensions; @@ -97,7 +99,10 @@ public EnumDescription(JsonObject source) { if (source is null) throw new ArgumentNullException(nameof(source)); if (source.TryGetPropertyValue(nameof(Value).ToFirstCharacterLowerCase(), out var rawValue) && rawValue is JsonNode value) - Value = value.GetValue(); + if (value.GetValueKind() == JsonValueKind.Number) + Value = value.GetValue().ToString(CultureInfo.InvariantCulture); + else + Value = value.GetValue(); if (source.TryGetPropertyValue(nameof(Description).ToFirstCharacterLowerCase(), out var rawDescription) && rawDescription is JsonNode description) Description = description.GetValue(); if (source.TryGetPropertyValue(nameof(Name).ToFirstCharacterLowerCase(), out var rawName) && rawName is JsonNode name) diff --git a/test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs b/test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs index 3b9f9964a..f09e07657 100644 --- a/test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs +++ b/test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Text.Json.Nodes; using Microsoft.OpenApi.MicrosoftExtensions; using Microsoft.OpenApi.Writers; using Xunit; @@ -22,7 +23,7 @@ public void WritesNothingWhenNoValues() { // Arrange OpenApiEnumValuesDescriptionExtension extension = new(); - using TextWriter sWriter = new StringWriter(); + using var sWriter = new StringWriter(); OpenApiJsonWriter writer = new(sWriter); // Act @@ -41,16 +42,16 @@ public void WritesEnumDescription() OpenApiEnumValuesDescriptionExtension extension = new() { EnumName = "TestEnum", - ValuesDescriptions = new() - { + ValuesDescriptions = + [ new() { Description = "TestDescription", Value = "TestValue", Name = "TestName" } - } + ] }; - using TextWriter sWriter = new StringWriter(); + using var sWriter = new StringWriter(); OpenApiJsonWriter writer = new(sWriter); // Act @@ -65,5 +66,49 @@ public void WritesEnumDescription() Assert.Contains("value\": \"TestValue", result); Assert.Contains("name\": \"TestName", result); } + [Fact] + public void ParsesEnumDescription() + { + var extensionValue = +""" +{ + "value": "Standard_LRS", + "description": "Locally redundant storage.", + "name": "StandardLocalRedundancy" +} +"""; + var source = JsonNode.Parse(extensionValue); + Assert.NotNull(source); + var sourceAsObject = source.AsObject(); + Assert.NotNull(sourceAsObject); + + var descriptionItem = new EnumDescription(sourceAsObject); + Assert.NotNull(descriptionItem); + Assert.Equal("Standard_LRS", descriptionItem.Value); + Assert.Equal("Locally redundant storage.", descriptionItem.Description); + Assert.Equal("StandardLocalRedundancy", descriptionItem.Name); + } + [Fact] + public void ParsesEnumDescriptionWithDecimalValue() + { + var extensionValue = +""" +{ + "value": -1, + "description": "Locally redundant storage.", + "name": "StandardLocalRedundancy" +} +"""; + var source = JsonNode.Parse(extensionValue); + Assert.NotNull(source); + var sourceAsObject = source.AsObject(); + Assert.NotNull(sourceAsObject); + + var descriptionItem = new EnumDescription(sourceAsObject); + Assert.NotNull(descriptionItem); + Assert.Equal("-1", descriptionItem.Value); + Assert.Equal("Locally redundant storage.", descriptionItem.Description); + Assert.Equal("StandardLocalRedundancy", descriptionItem.Name); + } }