Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enum description number values #2023

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
{
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>();
Comment on lines +102 to +105

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);
}
}

Loading