Skip to content

Commit cbbcfbd

Browse files
committed
chore: avoid double enumeration of map nodes in V2
Signed-off-by: Vincent Biret <[email protected]>
1 parent 2729704 commit cbbcfbd

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,6 @@ public MapNode(ParsingContext context, JsonNode node) : base(
3737
_nodes = _node.Select(p => GetPropertyNodeFromJsonNode(p.Key, p.Value)).ToList();
3838
}
3939

40-
public PropertyNode? this[string key]
41-
{
42-
get
43-
{
44-
if (_node.TryGetPropertyValue(key, out var value))
45-
{
46-
return GetPropertyNodeFromJsonNode(key, value);
47-
}
48-
49-
return null;
50-
}
51-
}
52-
5340
public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument)
5441
{
5542
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(T).Name}", Context);

src/Microsoft.OpenApi/Reader/V2/OpenApiV2Deserializer.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
76
using System.Text.Json.Nodes;
87

@@ -19,19 +18,22 @@ private static void ParseMap<T>(
1918
T domainObject,
2019
FixedFieldMap<T> fixedFieldMap,
2120
PatternFieldMap<T> patternFieldMap,
22-
OpenApiDocument doc,
23-
List<string>? requiredFields = null)
21+
OpenApiDocument doc)
2422
{
2523
if (mapNode == null)
2624
{
2725
return;
2826
}
2927

30-
var allFields = fixedFieldMap.Keys.Union(mapNode.Select(static x => x.Name));
31-
foreach (var propertyNode in allFields)
28+
var mapNodeFields = mapNode.ToDictionary(static x => x.Name, static x => x);
29+
var allFields = fixedFieldMap.Keys.Union(mapNodeFields.Keys);
30+
foreach (var propertyNodeName in allFields)
3231
{
33-
mapNode[propertyNode]?.ParseField(domainObject, fixedFieldMap, patternFieldMap, doc);
34-
requiredFields?.Remove(propertyNode);
32+
if (!mapNodeFields.TryGetValue(propertyNodeName, out var propertyNode))
33+
{
34+
continue;
35+
}
36+
propertyNode.ParseField(domainObject, fixedFieldMap, patternFieldMap, doc);
3537
}
3638
}
3739

test/Microsoft.OpenApi.Tests/Reader/MapNodeTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Nodes;
1+
using System.Linq;
2+
using System.Text.Json.Nodes;
23
using Microsoft.OpenApi.Reader;
34
using Xunit;
45

@@ -14,6 +15,6 @@ public void DoesNotFailOnNullValue()
1415

1516
Assert.NotNull(mapNode);
1617
Assert.Single(mapNode);
17-
Assert.True(mapNode["key"].Value.JsonNode.IsJsonNullSentinel());
18+
Assert.True(mapNode.First().Value.JsonNode.IsJsonNullSentinel());
1819
}
1920
}

0 commit comments

Comments
 (0)