Skip to content

Commit 39a9f41

Browse files
committed
fix: quote property names in yaml that match boolean values
Signed-off-by: Vincent Biret <[email protected]>
1 parent 234504c commit 39a9f41

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/Microsoft.OpenApi.YamlReader/YamlConverter.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static YamlMappingNode ToYamlMapping(this JsonObject obj)
8989
{
9090
return new YamlMappingNode(obj.ToDictionary(x => (YamlNode)new YamlScalarNode(x.Key)
9191
{
92-
Style = double.TryParse(x.Key, out _) ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain
92+
Style = NeedsQuoting(x.Key) ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain
9393
}, x => x.Value!.ToYamlNode()));
9494
}
9595

@@ -135,6 +135,11 @@ ScalarStyle.Plain when YamlNullRepresentations.Contains(yaml.Value) => (JsonValu
135135
};
136136
}
137137

138+
private static bool NeedsQuoting(string value) =>
139+
decimal.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out _) ||
140+
bool.TryParse(value, out _) ||
141+
YamlNullRepresentations.Contains(value);
142+
138143
private static YamlScalarNode ToYamlScalar(this JsonValue val)
139144
{
140145
// Try to get the underlying value based on its actual type
@@ -145,9 +150,7 @@ private static YamlScalarNode ToYamlScalar(this JsonValue val)
145150
// For string values, we need to determine if they should be quoted in YAML
146151
// Strings that look like numbers, booleans, or null need to be quoted
147152
// to preserve their string type when round-tripping
148-
var needsQuoting = decimal.TryParse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture, out _) ||
149-
bool.TryParse(stringValue, out _) ||
150-
YamlNullRepresentations.Contains(stringValue);
153+
var needsQuoting = NeedsQuoting(stringValue);
151154

152155
return new YamlScalarNode(stringValue)
153156
{

test/Microsoft.OpenApi.Readers.Tests/YamlConverterTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,30 @@ public void NumericPropertyNamesShouldRemainStringsFromYaml()
274274
Assert.Equal(yamlInput.MakeLineBreaksEnvironmentNeutral(), convertedBackOutput.MakeLineBreaksEnvironmentNeutral());
275275
}
276276

277+
[Fact]
278+
public void BooleanPropertyNamesShouldRemainStringsFromYaml()
279+
{
280+
// Given
281+
var yamlInput =
282+
"""
283+
"true": value1
284+
"false": value2
285+
""";
286+
287+
var yamlDocument = new YamlStream();
288+
using var sr = new StringReader(yamlInput);
289+
yamlDocument.Load(sr);
290+
var yamlRoot = yamlDocument.Documents[0].RootNode;
291+
// When
292+
293+
var jsonNode = yamlRoot.ToJsonNode();
294+
295+
var convertedBack = jsonNode.ToYamlNode();
296+
var convertedBackOutput = ConvertYamlNodeToString(convertedBack);
297+
// Then
298+
Assert.Equal(yamlInput.MakeLineBreaksEnvironmentNeutral(), convertedBackOutput.MakeLineBreaksEnvironmentNeutral());
299+
}
300+
277301
private static string ConvertYamlNodeToString(YamlNode yamlNode)
278302
{
279303
using var ms = new MemoryStream();

0 commit comments

Comments
 (0)