Skip to content

Commit 8d215f9

Browse files
authored
Merge pull request #2612 from microsoft/fix/yaml-empty-strings-to-main
fix: empty strings should be quoted in yaml
2 parents eec7792 + 3b0d10a commit 8d215f9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Microsoft.OpenApi.YamlReader/YamlConverter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ ScalarStyle.Plain when YamlNullRepresentations.Contains(yaml.Value) => (JsonValu
140140
}
141141

142142
private static bool NeedsQuoting(string value) =>
143+
string.IsNullOrEmpty(value) ||
143144
decimal.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out _) ||
144-
bool.TryParse(value, out _) ||
145-
YamlNullRepresentations.Contains(value);
145+
bool.TryParse(value, out _) ||
146+
YamlNullRepresentations.Contains(value);
146147

147148
private static YamlScalarNode ToYamlScalar(this JsonValue val)
148149
{

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,23 @@ public void ItDoesNotSerializeTheSentinelValue()
315315
var scalarNode = Assert.IsType<YamlScalarNode>(yamlValue);
316316
Assert.Equal("null", scalarNode.Value, StringComparer.OrdinalIgnoreCase);
317317
}
318+
[Fact]
319+
public void RoundTripEmptyStringsValues()
320+
{
321+
// Given
322+
var yamlInput =
323+
"""
324+
emptyString: ""
325+
""";
326+
327+
// When
328+
var jsonNode = ConvertYamlStringToJsonNode(yamlInput);
329+
var convertedBack = jsonNode.ToYamlNode();
330+
var convertedBackOutput = ConvertYamlNodeToString(convertedBack);
331+
332+
// Then
333+
Assert.Equal(yamlInput.MakeLineBreaksEnvironmentNeutral(), convertedBackOutput.MakeLineBreaksEnvironmentNeutral());
334+
}
318335

319336
private static JsonNode ConvertYamlStringToJsonNode(string yamlInput)
320337
{

0 commit comments

Comments
 (0)