Skip to content

Commit 15618e1

Browse files
committed
fix: a bug where null sentinel value would appear in YAML documents
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent e6c5674 commit 15618e1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Microsoft.OpenApi.YamlReader/YamlConverter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public static YamlNode ToYamlNode(this JsonNode json)
6363
{
6464
JsonObject obj => obj.ToYamlMapping(),
6565
JsonArray arr => arr.ToYamlSequence(),
66+
JsonValue nullVal when JsonNullSentinel.IsJsonNullSentinel(nullVal) => new YamlScalarNode("null")
67+
{
68+
Style = ScalarStyle.Plain
69+
},
6670
JsonValue val => val.ToYamlScalar(),
6771
_ => throw new NotSupportedException("This isn't a supported JsonNode")
6872
};

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.OpenApi.YamlReader;
33
using SharpYaml;
44
using SharpYaml.Serialization;
5+
using System;
56
using System.IO;
67
using System.Text.Json.Nodes;
78
using Xunit;
@@ -302,10 +303,18 @@ from openai import OpenAI
302303
var jsonNode = ConvertYamlStringToJsonNode(yamlInput);
303304
var convertedBack = jsonNode.ToYamlNode();
304305
var convertedBackOutput = ConvertYamlNodeToString(convertedBack);
305-
306+
306307
// Then
307308
Assert.Equal(yamlInput.MakeLineBreaksEnvironmentNeutral(), convertedBackOutput.MakeLineBreaksEnvironmentNeutral());
308309
}
310+
[Fact]
311+
public void ItDoesNotSerializeTheSentinelValue()
312+
{
313+
var yamlValue = JsonNullSentinel.JsonNull.ToYamlNode();
314+
315+
var scalarNode = Assert.IsType<YamlScalarNode>(yamlValue);
316+
Assert.Equal("null", scalarNode.Value, StringComparer.OrdinalIgnoreCase);
317+
}
309318

310319
private static JsonNode ConvertYamlStringToJsonNode(string yamlInput)
311320
{

0 commit comments

Comments
 (0)