Skip to content

Commit e29e24c

Browse files
committedDec 23, 2024··
fix: enum description number values
Signed-off-by: Vincent Biret <[email protected]>
1 parent 7c9199b commit e29e24c

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed
 

‎src/Microsoft.OpenApi/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtension.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.OpenApi.Interfaces;
1212
using Microsoft.OpenApi.Writers;
1313
using System.Text.Json.Nodes;
14+
using System.Text.Json;
15+
using System.Globalization;
1416

1517
namespace Microsoft.OpenApi.MicrosoftExtensions;
1618

@@ -97,7 +99,10 @@ public EnumDescription(JsonObject source)
9799
{
98100
if (source is null) throw new ArgumentNullException(nameof(source));
99101
if (source.TryGetPropertyValue(nameof(Value).ToFirstCharacterLowerCase(), out var rawValue) && rawValue is JsonNode value)
100-
Value = value.GetValue<string>();
102+
if (value.GetValueKind() == JsonValueKind.Number)
103+
Value = value.GetValue<decimal>().ToString(CultureInfo.InvariantCulture);
104+
else
105+
Value = value.GetValue<string>();
101106
if (source.TryGetPropertyValue(nameof(Description).ToFirstCharacterLowerCase(), out var rawDescription) && rawDescription is JsonNode description)
102107
Description = description.GetValue<string>();
103108
if (source.TryGetPropertyValue(nameof(Name).ToFirstCharacterLowerCase(), out var rawName) && rawName is JsonNode name)

‎test/Microsoft.OpenApi.Tests/MicrosoftExtensions/OpenApiEnumValuesDescriptionExtensionTests.cs

+50-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Text.Json.Nodes;
23
using Microsoft.OpenApi.MicrosoftExtensions;
34
using Microsoft.OpenApi.Writers;
45
using Xunit;
@@ -22,7 +23,7 @@ public void WritesNothingWhenNoValues()
2223
{
2324
// Arrange
2425
OpenApiEnumValuesDescriptionExtension extension = new();
25-
using TextWriter sWriter = new StringWriter();
26+
using var sWriter = new StringWriter();
2627
OpenApiJsonWriter writer = new(sWriter);
2728

2829
// Act
@@ -41,16 +42,16 @@ public void WritesEnumDescription()
4142
OpenApiEnumValuesDescriptionExtension extension = new()
4243
{
4344
EnumName = "TestEnum",
44-
ValuesDescriptions = new()
45-
{
45+
ValuesDescriptions =
46+
[
4647
new() {
4748
Description = "TestDescription",
4849
Value = "TestValue",
4950
Name = "TestName"
5051
}
51-
}
52+
]
5253
};
53-
using TextWriter sWriter = new StringWriter();
54+
using var sWriter = new StringWriter();
5455
OpenApiJsonWriter writer = new(sWriter);
5556

5657
// Act
@@ -65,5 +66,49 @@ public void WritesEnumDescription()
6566
Assert.Contains("value\": \"TestValue", result);
6667
Assert.Contains("name\": \"TestName", result);
6768
}
69+
[Fact]
70+
public void ParsesEnumDescription()
71+
{
72+
var extensionValue =
73+
"""
74+
{
75+
"value": "Standard_LRS",
76+
"description": "Locally redundant storage.",
77+
"name": "StandardLocalRedundancy"
78+
}
79+
""";
80+
var source = JsonNode.Parse(extensionValue);
81+
Assert.NotNull(source);
82+
var sourceAsObject = source.AsObject();
83+
Assert.NotNull(sourceAsObject);
84+
85+
var descriptionItem = new EnumDescription(sourceAsObject);
86+
Assert.NotNull(descriptionItem);
87+
Assert.Equal("Standard_LRS", descriptionItem.Value);
88+
Assert.Equal("Locally redundant storage.", descriptionItem.Description);
89+
Assert.Equal("StandardLocalRedundancy", descriptionItem.Name);
90+
}
91+
[Fact]
92+
public void ParsesEnumDescriptionWithDecimalValue()
93+
{
94+
var extensionValue =
95+
"""
96+
{
97+
"value": -1,
98+
"description": "Locally redundant storage.",
99+
"name": "StandardLocalRedundancy"
100+
}
101+
""";
102+
var source = JsonNode.Parse(extensionValue);
103+
Assert.NotNull(source);
104+
var sourceAsObject = source.AsObject();
105+
Assert.NotNull(sourceAsObject);
106+
107+
var descriptionItem = new EnumDescription(sourceAsObject);
108+
Assert.NotNull(descriptionItem);
109+
Assert.Equal("-1", descriptionItem.Value);
110+
Assert.Equal("Locally redundant storage.", descriptionItem.Description);
111+
Assert.Equal("StandardLocalRedundancy", descriptionItem.Name);
112+
}
68113
}
69114

0 commit comments

Comments
 (0)
Please sign in to comment.