Skip to content

Commit 994184b

Browse files
committed
fix: fixes a bug where yaml null values would end up as a string "null" during roundtrip serialization
Signed-off-by: Vincent Biret <[email protected]> chore: reverts internal change Signed-off-by: Vincent Biret <[email protected]>
1 parent a7f0182 commit 994184b

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

src/Microsoft.OpenApi.YamlReader/OpenApiYamlReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ static JsonNode LoadJsonNodesFromYamlDocument(TextReader input)
131131
{
132132
var yamlStream = new YamlStream();
133133
yamlStream.Load(input);
134-
if (yamlStream.Documents.Any())
134+
if (yamlStream.Documents.Any() && yamlStream.Documents[0].ToJsonNode() is { } jsonNode)
135135
{
136-
return yamlStream.Documents[0].ToJsonNode();
136+
return jsonNode;
137137
}
138138

139139
throw new InvalidOperationException("No documents found in the YAML stream.");

src/Microsoft.OpenApi.YamlReader/YamlConverter.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class YamlConverter
1818
/// </summary>
1919
/// <param name="yaml">The YAML stream.</param>
2020
/// <returns>A collection of nodes representing the YAML documents in the stream.</returns>
21-
public static IEnumerable<JsonNode> ToJsonNode(this YamlStream yaml)
21+
public static IEnumerable<JsonNode?> ToJsonNode(this YamlStream yaml)
2222
{
2323
return yaml.Documents.Select(x => x.ToJsonNode());
2424
}
@@ -28,7 +28,7 @@ public static IEnumerable<JsonNode> ToJsonNode(this YamlStream yaml)
2828
/// </summary>
2929
/// <param name="yaml">The YAML document.</param>
3030
/// <returns>A `JsonNode` representative of the YAML document.</returns>
31-
public static JsonNode ToJsonNode(this YamlDocument yaml)
31+
public static JsonNode? ToJsonNode(this YamlDocument yaml)
3232
{
3333
return yaml.RootNode.ToJsonNode();
3434
}
@@ -39,7 +39,7 @@ public static JsonNode ToJsonNode(this YamlDocument yaml)
3939
/// <param name="yaml">The YAML node.</param>
4040
/// <returns>A `JsonNode` representative of the YAML node.</returns>
4141
/// <exception cref="NotSupportedException">Thrown for YAML that is not compatible with JSON.</exception>
42-
public static JsonNode ToJsonNode(this YamlNode yaml)
42+
public static JsonNode? ToJsonNode(this YamlNode yaml)
4343
{
4444
return yaml switch
4545
{
@@ -110,25 +110,25 @@ private static YamlSequenceNode ToYamlSequence(this JsonArray arr)
110110
return new YamlSequenceNode(arr.Select(x => x!.ToYamlNode()));
111111
}
112112

113-
private static JsonValue ToJsonValue(this YamlScalarNode yaml)
113+
private static readonly HashSet<string> YamlNullRepresentations = new(StringComparer.Ordinal)
114114
{
115-
switch (yaml.Style)
115+
"~",
116+
"null",
117+
"Null",
118+
"NULL"
119+
};
120+
121+
private static JsonValue? ToJsonValue(this YamlScalarNode yaml)
122+
{
123+
return yaml.Style switch
116124
{
117-
case ScalarStyle.Plain:
118-
return decimal.TryParse(yaml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d)
119-
? JsonValue.Create(d)
120-
: bool.TryParse(yaml.Value, out var b)
121-
? JsonValue.Create(b)
122-
: JsonValue.Create(yaml.Value)!;
123-
case ScalarStyle.SingleQuoted:
124-
case ScalarStyle.DoubleQuoted:
125-
case ScalarStyle.Literal:
126-
case ScalarStyle.Folded:
127-
case ScalarStyle.Any:
128-
return JsonValue.Create(yaml.Value)!;
129-
default:
130-
throw new ArgumentOutOfRangeException();
131-
}
125+
ScalarStyle.Plain when decimal.TryParse(yaml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d) => JsonValue.Create(d),
126+
ScalarStyle.Plain when bool.TryParse(yaml.Value, out var b) => JsonValue.Create(b),
127+
ScalarStyle.Plain when YamlNullRepresentations.Contains(yaml.Value) => null,
128+
ScalarStyle.Plain => JsonValue.Create(yaml.Value),
129+
ScalarStyle.SingleQuoted or ScalarStyle.DoubleQuoted or ScalarStyle.Literal or ScalarStyle.Folded or ScalarStyle.Any => JsonValue.Create(yaml.Value),
130+
_ => throw new ArgumentOutOfRangeException(nameof(yaml)),
131+
};
132132
}
133133

134134
private static YamlScalarNode ToYamlScalar(this JsonValue val)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using SharpYaml;
2+
using SharpYaml.Serialization;
3+
using Xunit;
4+
using Microsoft.OpenApi.YamlReader;
5+
6+
namespace Microsoft.OpenApi.Readers.Tests;
7+
8+
public class YamlConverterTests
9+
{
10+
[Theory]
11+
[InlineData("~")]
12+
[InlineData("null")]
13+
[InlineData("Null")]
14+
[InlineData("NULL")]
15+
public void YamlNullValuesReturnNullJsonNode(string value)
16+
{
17+
// Given
18+
var yamlNull = new YamlScalarNode(value)
19+
{
20+
Style = ScalarStyle.Plain
21+
};
22+
23+
// When
24+
var jsonNode = yamlNull.ToJsonNode();
25+
26+
// Then
27+
Assert.Null(jsonNode);
28+
}
29+
}

0 commit comments

Comments
 (0)