Skip to content

Commit b7ae3f5

Browse files
committed
Implement PR feedback
1 parent fcfd82f commit b7ae3f5

File tree

11 files changed

+51
-77
lines changed

11 files changed

+51
-77
lines changed

src/Microsoft.OpenApi.Readers/OpenApiTextReaderReader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.OpenApi.Models;
1212
using Microsoft.OpenApi.Readers.Interface;
1313
using SharpYaml;
14-
//using SharpYaml;
1514
using SharpYaml.Serialization;
1615

1716
namespace Microsoft.OpenApi.Readers

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Text.Json.Nodes;
99
using Microsoft.OpenApi.Any;
10+
using Microsoft.OpenApi.Readers.Exceptions;
1011

1112
namespace Microsoft.OpenApi.Readers.ParseNodes
1213
{
@@ -24,7 +25,7 @@ public override List<T> CreateList<T>(Func<MapNode, T> map)
2425
{
2526
if (_nodeList == null)
2627
{
27-
//throw new OpenApiReaderException($"Expected list at line {_nodeList.Start.Line} while parsing {typeof(T).Name}", _nodeList);
28+
throw new OpenApiReaderException($"Expected list while parsing {typeof(T).Name}", _nodeList);
2829
}
2930

3031
return _nodeList?.Select(n => map(new MapNode(Context, n as JsonObject)))
@@ -43,7 +44,7 @@ public override List<T> CreateSimpleList<T>(Func<ValueNode, T> map)
4344
{
4445
if (_nodeList == null)
4546
{
46-
//throw new OpenApiReaderException($"Expected list at line {_nodeList.Start.Line} while parsing {typeof(T).Name}", _nodeList);
47+
throw new OpenApiReaderException($"Expected list while parsing {typeof(T).Name}", _nodeList);
4748
}
4849

4950
return _nodeList.Select(n => map(new ValueNode(Context, n))).ToList();

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Globalization;
78
using System.Linq;
89
using System.Text.Json;
910
using System.Text.Json.Nodes;
@@ -62,9 +63,9 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
6263
try
6364
{
6465
Context.StartObject(key);
65-
value = n.Value as JsonObject == null
66-
? default
67-
: map(new MapNode(Context, n.Value as JsonObject));
66+
value = n.Value is JsonObject jsonObject
67+
? map(new MapNode(Context, jsonObject))
68+
: default;
6869
}
6970
finally
7071
{
@@ -159,7 +160,7 @@ IEnumerator IEnumerable.GetEnumerator()
159160

160161
public override string GetRaw()
161162
{
162-
var x = JsonSerializer.Serialize(_node); // (new SerializerSettings(new JsonSchema()) { EmitJsonComptible = true });
163+
var x = JsonSerializer.Serialize(_node);
163164
return x;
164165
}
165166

@@ -188,10 +189,10 @@ public string GetScalarValue(ValueNode key)
188189
var scalarNode = _node[key.GetScalarValue()] as JsonValue;
189190
if (scalarNode == null)
190191
{
191-
//throw new OpenApiReaderException($"Expected scalar at line {_node.Start.Line} for key {key.GetScalarValue()}", Context);
192+
throw new OpenApiReaderException($"Expected scalar for key {key.GetScalarValue()}", Context);
192193
}
193-
194-
return scalarNode?.GetValue<string>();
194+
195+
return Convert.ToString(scalarNode?.GetValue<object>(), CultureInfo.InvariantCulture);
195196
}
196197

197198
/// <summary>

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using System.Text.Json;
88
using System.Text.Json.Nodes;
9+
using Microsoft.OpenApi.Any;
910
using Microsoft.OpenApi.Models;
1011

1112
namespace Microsoft.OpenApi.Readers.ParseNodes
@@ -18,9 +19,10 @@ internal static class OpenApiAnyConverter
1819
/// For those strings that the schema does not specify the type for, convert them into
1920
/// the most specific type based on the value.
2021
/// </summary>
21-
public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema schema = null)
22+
public static JsonNode GetSpecificOpenApiAny(OpenApiAny any, OpenApiSchema schema = null)
2223
{
23-
if(jsonNode == null)
24+
var jsonNode = any?.Node;
25+
if (jsonNode == null)
2426
{
2527
return jsonNode;
2628
}
@@ -32,12 +34,12 @@ public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema sc
3234
if(element.Parent != null)
3335
{
3436
var newNode = element;
35-
newArray.Add(GetSpecificOpenApiAny(newNode, schema?.Items));
37+
newArray.Add(GetSpecificOpenApiAny(new OpenApiAny(newNode), schema?.Items));
3638

3739
}
3840
else
3941
{
40-
newArray.Add(GetSpecificOpenApiAny(element, schema?.Items));
42+
newArray.Add(GetSpecificOpenApiAny(new OpenApiAny(element), schema?.Items));
4143
}
4244
}
4345

@@ -54,11 +56,11 @@ public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema sc
5456
if (jsonObject[property.Key].Parent != null)
5557
{
5658
var node = jsonObject[property.Key];
57-
newObject.Add(property.Key, GetSpecificOpenApiAny(node, propertySchema));
59+
newObject.Add(property.Key, GetSpecificOpenApiAny(new OpenApiAny(node), propertySchema));
5860
}
5961
else
6062
{
61-
newObject.Add(property.Key, GetSpecificOpenApiAny(property.Value, propertySchema));
63+
newObject.Add(property.Key, GetSpecificOpenApiAny(new OpenApiAny(property.Value), propertySchema));
6264

6365
}
6466
}
@@ -67,11 +69,11 @@ public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema sc
6769
if (jsonObject[property.Key].Parent != null)
6870
{
6971
var node = jsonObject[property.Key].Deserialize<JsonNode>();
70-
newObject[property.Key] = GetSpecificOpenApiAny(node, schema?.AdditionalProperties);
72+
newObject[property.Key] = GetSpecificOpenApiAny(new OpenApiAny(node), schema?.AdditionalProperties);
7173
}
7274
else
7375
{
74-
newObject[property.Key] = GetSpecificOpenApiAny(jsonObject[property.Key], schema?.AdditionalProperties);
76+
newObject[property.Key] = GetSpecificOpenApiAny(new OpenApiAny(jsonObject[property.Key]), schema?.AdditionalProperties);
7577
}
7678
}
7779
}
@@ -83,14 +85,12 @@ public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema sc
8385
{
8486
return jsonNode;
8587
}
86-
88+
8789
var value = jsonValue.GetScalarValue();
8890
var type = schema?.Type;
8991
var format = schema?.Format;
90-
//var jsonElement = JsonSerializer.Deserialize<JsonElement>(value);
91-
var valueType = value.GetType();
9292

93-
if (jsonValue.ToJsonString().StartsWith("\""))
93+
if(value.StartsWith("\""))
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/V2/OpenApiV2Deserializer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static void ProcessAnyFields<T>(
5151
var anyFieldSchema = anyFieldMap[anyFieldName].SchemaGetter(domainObject);
5252

5353
var convertedOpenApiAny = OpenApiAnyConverter.GetSpecificOpenApiAny(
54-
anyFieldValue, anyFieldSchema);
54+
new OpenApiAny(anyFieldValue), anyFieldSchema);
5555

5656
if(convertedOpenApiAny == null)
5757
{
@@ -94,7 +94,7 @@ private static void ProcessAnyListFields<T>(
9494
{
9595
newProperty.Add(new OpenApiAny(
9696
OpenApiAnyConverter.GetSpecificOpenApiAny(
97-
propertyElement.Node,
97+
propertyElement,
9898
anyListFieldMap[anyListFieldName].SchemaGetter(domainObject))));
9999
}
100100
}
@@ -133,7 +133,7 @@ private static void ProcessAnyMapFields<T, U>(
133133
var any = anyMapFieldMap[anyMapFieldName].PropertyGetter(propertyMapElement.Value);
134134

135135
var newAny = OpenApiAnyConverter.GetSpecificOpenApiAny(
136-
any.Node,
136+
any,
137137
anyMapFieldMap[anyMapFieldName].SchemaGetter(domainObject));
138138

139139
anyMapFieldMap[anyMapFieldName].PropertySetter(propertyMapElement.Value, new OpenApiAny(newAny));
@@ -154,20 +154,20 @@ private static void ProcessAnyMapFields<T, U>(
154154

155155
public static OpenApiAny LoadAny(ParseNode node)
156156
{
157-
return new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny().Node));
157+
return new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()));
158158
}
159159

160160
private static IOpenApiExtension LoadExtension(string name, ParseNode node)
161161
{
162162
if (node.Context.ExtensionParsers.TryGetValue(name, out var parser))
163163
{
164164
return parser(new OpenApiAny(
165-
OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny().Node)),
165+
OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny())),
166166
OpenApiSpecVersion.OpenApi2_0);
167167
}
168168
else
169169
{
170-
return new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny().Node));
170+
return new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()));
171171
}
172172
}
173173

src/Microsoft.OpenApi.Readers/V3/OpenApiV3Deserializer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private static void ProcessAnyFields<T>(
5050

5151
var any = anyFieldMap[anyFieldName].PropertyGetter(domainObject);
5252
var schema = anyFieldMap[anyFieldName].SchemaGetter(domainObject);
53-
var convertedOpenApiAny = OpenApiAnyConverter.GetSpecificOpenApiAny(any?.Node, schema);
53+
var convertedOpenApiAny = OpenApiAnyConverter.GetSpecificOpenApiAny(any, schema);
5454

5555
if (convertedOpenApiAny == null)
5656
{
@@ -90,7 +90,7 @@ private static void ProcessAnyListFields<T>(
9090
{
9191
newProperty.Add(new OpenApiAny(
9292
OpenApiAnyConverter.GetSpecificOpenApiAny(
93-
propertyElement.Node,
93+
propertyElement,
9494
anyListFieldMap[anyListFieldName].SchemaGetter(domainObject))));
9595
}
9696

@@ -128,7 +128,7 @@ private static void ProcessAnyMapFields<T, U>(
128128
var any = anyMapFieldMap[anyMapFieldName].PropertyGetter(propertyMapElement.Value);
129129

130130
var newAny = OpenApiAnyConverter.GetSpecificOpenApiAny(
131-
any.Node,
131+
any,
132132
anyMapFieldMap[anyMapFieldName].SchemaGetter(domainObject));
133133

134134
anyMapFieldMap[anyMapFieldName].PropertySetter(propertyMapElement.Value, new OpenApiAny(newAny));
@@ -167,25 +167,25 @@ private static RuntimeExpressionAnyWrapper LoadRuntimeExpressionAnyWrapper(Parse
167167

168168
return new RuntimeExpressionAnyWrapper
169169
{
170-
Any = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny().Node))
170+
Any = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()))
171171

172172
};
173173
}
174174

175175
public static OpenApiAny LoadAny(ParseNode node)
176176
{
177-
return new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny().Node));
177+
return new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()));
178178
}
179179

180180
private static IOpenApiExtension LoadExtension(string name, ParseNode node)
181181
{
182182
if (node.Context.ExtensionParsers.TryGetValue(name, out var parser))
183183
{
184-
return parser(new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny().Node)), OpenApiSpecVersion.OpenApi3_0);
184+
return parser(new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny())), OpenApiSpecVersion.OpenApi3_0);
185185
}
186186
else
187187
{
188-
return new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny().Node));
188+
return new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()));
189189
}
190190
}
191191

src/Microsoft.OpenApi.Readers/YamlHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text.Json.Nodes;
99
using System.Xml.Linq;
1010
using SharpYaml.Serialization;
11+
using Microsoft.OpenApi.Exceptions;
1112

1213
namespace Microsoft.OpenApi.Readers
1314
{
@@ -19,11 +20,10 @@ public static string GetScalarValue(this JsonNode node)
1920
var scalarNode = node as JsonValue;
2021
if (node == null)
2122
{
22-
//throw new OpenApiException($"Expected scalar at line {node.Start.Line}");
23+
throw new OpenApiException($"Expected scalar value.");
2324
}
2425

25-
return scalarNode?.GetValue<string>();
26-
//return Convert.ToString(scalarNode?.GetValue<object>(), CultureInfo.InvariantCulture);
26+
return Convert.ToString(scalarNode?.GetValue<object>(), CultureInfo.InvariantCulture);
2727
}
2828

2929
public static JsonNode ParseJsonString(string yamlString)

src/Microsoft.OpenApi/Extensions/JsonNodeExtension.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ internal OpenApiBodyParameter ConvertToBodyParameter()
191191
};
192192
if (bodyParameter.Extensions.ContainsKey(OpenApiConstants.BodyName))
193193
{
194-
var bodyName = bodyParameter.Extensions[OpenApiConstants.BodyName] as OpenApiAny;
195-
bodyParameter.Name = bodyName.Node.ToString() ?? "body";
194+
var bodyName = bodyParameter.Extensions[OpenApiConstants.BodyName].ToString();
195+
bodyParameter.Name = string.IsNullOrEmpty(bodyName) ? "body" : bodyName;
196196
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
197197
}
198198
return bodyParameter;

test/Microsoft.OpenApi.Readers.Tests/ParseNodes/OpenApiAnyConverterTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void ParseObjectAsAnyShouldSucceed()
7373
}
7474
};
7575

76-
anyMap = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap.Node, schema));
76+
anyMap = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap, schema));
7777
var expected = new OpenApiAny(new JsonObject
7878
{
7979
["aString"] = "fooBar",
@@ -213,7 +213,7 @@ public void ParseNestedObjectAsAnyShouldSucceed()
213213
}
214214
};
215215

216-
anyMap = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap.Node, schema));
216+
anyMap = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap, schema));
217217

218218
diagnostic.Errors.Should().BeEmpty();
219219
var expected = new OpenApiAny(
@@ -368,7 +368,7 @@ public void ParseNestedObjectAsAnyWithPartialSchemaShouldSucceed()
368368
}
369369
};
370370

371-
anyMap = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap.Node, schema));
371+
anyMap = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap, schema));
372372

373373
diagnostic.Errors.Should().BeEmpty();
374374

@@ -460,7 +460,7 @@ public void ParseNestedObjectAsAnyWithoutUsingSchemaShouldSucceed()
460460

461461
var anyMap = node.CreateAny();
462462

463-
anyMap = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap.Node));
463+
anyMap = new OpenApiAny(OpenApiAnyConverter.GetSpecificOpenApiAny(anyMap));
464464

465465
diagnostic.Errors.Should().BeEmpty();
466466

0 commit comments

Comments
 (0)