Skip to content

Commit

Permalink
Merge pull request #2023 from microsoft/fix/enum-description-int-values
Browse files Browse the repository at this point in the history
fix: enum description number values
  • Loading branch information
baywet authored Dec 24, 2024
2 parents 5bc683b + e29e24c commit ff7b4a9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<string>();
if (value.GetValueKind() == JsonValueKind.Number)
Value = value.GetValue<decimal>().ToString(CultureInfo.InvariantCulture);
else
Value = value.GetValue<string>();

Check notice

Code scanning / CodeQL

Missed ternary opportunity Note

Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.
if (source.TryGetPropertyValue(nameof(Description).ToFirstCharacterLowerCase(), out var rawDescription) && rawDescription is JsonNode description)
Description = description.GetValue<string>();
if (source.TryGetPropertyValue(nameof(Name).ToFirstCharacterLowerCase(), out var rawName) && rawName is JsonNode name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.MicrosoftExtensions;
using Microsoft.OpenApi.Writers;
using Xunit;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}

0 comments on commit ff7b4a9

Please sign in to comment.