Skip to content

Commit 63570c1

Browse files
committed
More code cleanup
1 parent b7ae3f5 commit 63570c1

File tree

5 files changed

+12
-28
lines changed

5 files changed

+12
-28
lines changed

src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,9 @@ public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
132132
try
133133
{
134134
Context.StartObject(key);
135-
JsonValue valueNode = n.Value as JsonValue;
136-
137-
if (valueNode == null)
138-
{
139-
throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context);
140-
}
141-
135+
JsonValue valueNode = n.Value is JsonValue value ? value
136+
: throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context);
137+
142138
return (key, value: map(new ValueNode(Context, (JsonValue)n.Value)));
143139
} finally {
144140
Context.EndObject();
@@ -186,11 +182,9 @@ public string GetReferencePointer()
186182

187183
public string GetScalarValue(ValueNode key)
188184
{
189-
var scalarNode = _node[key.GetScalarValue()] as JsonValue;
190-
if (scalarNode == null)
191-
{
192-
throw new OpenApiReaderException($"Expected scalar for key {key.GetScalarValue()}", Context);
193-
}
185+
var scalarNode = _node[key.GetScalarValue()] is JsonValue jsonValue
186+
? jsonValue
187+
: throw new OpenApiReaderException($"Expected scalar while parsing {key.GetScalarValue()}", Context);
194188

195189
return Convert.ToString(scalarNode?.GetValue<object>(), CultureInfo.InvariantCulture);
196190
}

src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static JsonNode GetSpecificOpenApiAny(OpenApiAny any, OpenApiSchema schem
9090
var type = schema?.Type;
9191
var format = schema?.Format;
9292

93-
if(value.StartsWith("\""))
93+
if(value.StartsWith("\"", StringComparison.OrdinalIgnoreCase))
9494
{
9595
// More narrow type detection for explicit strings, only check types that are passed as strings
9696
if (schema == null)

src/Microsoft.OpenApi.Readers/ParseNodes/RootNode.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,14 @@ public RootNode(
2323

2424
public ParseNode Find(JsonPointer referencePointer)
2525
{
26-
var jsonNode = referencePointer.Find(_jsonNode);
27-
if (jsonNode == null)
28-
{
29-
return null;
30-
}
26+
var jsonNode = referencePointer.Find(_jsonNode) is JsonNode node ? node : null;
3127

3228
return Create(Context, jsonNode);
3329
}
3430

3531
public MapNode GetMap()
3632
{
37-
var jsonNode = _jsonNode;
38-
return new MapNode(Context, jsonNode);
33+
return new MapNode(Context, _jsonNode);
3934
}
4035
}
4136
}

src/Microsoft.OpenApi.Readers/ParsingContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ private static string GetVersion(RootNode rootNode)
119119

120120
if (versionNode != null)
121121
{
122-
return versionNode.GetScalarValue().Replace("\"", "");
122+
return versionNode.GetScalarValue().Replace("\"", string.Empty);
123123
}
124124

125125
versionNode = rootNode.Find(new JsonPointer("/swagger"));
126126

127-
return versionNode?.GetScalarValue().Replace("\"", "");
127+
return versionNode?.GetScalarValue().Replace("\"", string.Empty);
128128
}
129129

130130
/// <summary>

src/Microsoft.OpenApi.Readers/YamlHelper.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.IO;
77
using System.Linq;
88
using System.Text.Json.Nodes;
9-
using System.Xml.Linq;
109
using SharpYaml.Serialization;
1110
using Microsoft.OpenApi.Exceptions;
1211

@@ -17,11 +16,7 @@ internal static class YamlHelper
1716
public static string GetScalarValue(this JsonNode node)
1817
{
1918

20-
var scalarNode = node as JsonValue;
21-
if (node == null)
22-
{
23-
throw new OpenApiException($"Expected scalar value.");
24-
}
19+
var scalarNode = node is JsonValue value ? value : throw new OpenApiException($"Expected scalar value.");
2520

2621
return Convert.ToString(scalarNode?.GetValue<object>(), CultureInfo.InvariantCulture);
2722
}

0 commit comments

Comments
 (0)