Skip to content

Commit 67789f3

Browse files
committed
chore: avoid systematically cloning to reduce performance impact
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent c6a3a10 commit 67789f3

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/Microsoft.OpenApi.YamlReader/YamlConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private static JsonValue ToJsonValue(this YamlScalarNode yaml)
124124
{
125125
ScalarStyle.Plain when decimal.TryParse(yaml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d) => JsonValue.Create(d),
126126
ScalarStyle.Plain when bool.TryParse(yaml.Value, out var b) => JsonValue.Create(b),
127-
ScalarStyle.Plain when YamlNullRepresentations.Contains(yaml.Value) => JsonNullSentinel.JsonNull,
127+
ScalarStyle.Plain when YamlNullRepresentations.Contains(yaml.Value) => (JsonValue)JsonNullSentinel.JsonNull.DeepClone(),
128128
ScalarStyle.Plain => JsonValue.Create(yaml.Value),
129129
ScalarStyle.SingleQuoted or ScalarStyle.DoubleQuoted or ScalarStyle.Literal or ScalarStyle.Folded or ScalarStyle.Any => JsonValue.Create(yaml.Value),
130130
_ => throw new ArgumentOutOfRangeException(nameof(yaml)),

src/Microsoft.OpenApi/JsonNullSentinel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public static class JsonNullSentinel
2020
/// This can only be used for OpenAPI properties of type <see cref="JsonNode"/>.
2121
/// This can only be used for the root level of a JSON structure.
2222
/// Any use outside of these constraints is unsupported and may lead to unexpected behavior.
23-
/// Because this is returning a cloned instance, so the value can be added in a tree, reference equality checks will not work.
23+
/// Because the value might be cloned, so the value can be added in a tree, reference equality checks will not work.
2424
/// You must use the <see cref="IsJsonNullSentinel(JsonNode?)"/> method to check for this sentinel.
2525
/// </summary>
26-
public static JsonValue JsonNull => (JsonValue)SentinelJsonValue.DeepClone();
26+
public static JsonValue JsonNull => SentinelJsonValue;
2727

2828
/// <summary>
2929
/// Determines if the given node is the JSON null sentinel.
@@ -32,7 +32,8 @@ public static class JsonNullSentinel
3232
/// <returns>Whether or not the given node is the JSON null sentinel.</returns>
3333
public static bool IsJsonNullSentinel(this JsonNode? node)
3434
{
35-
return node is JsonValue jsonValue &&
35+
return node == SentinelJsonValue ||
36+
node is JsonValue jsonValue &&
3637
jsonValue.GetValueKind() == JsonValueKind.String &&
3738
jsonValue.TryGetValue<string>(out var value) &&
3839
SentinelValue.Equals(value, StringComparison.Ordinal);

0 commit comments

Comments
 (0)