Skip to content

Commit 98aeb32

Browse files
authored
Merge pull request microsoft#591 from microsoft/dm/fixapigurutests
Fix error reporting for simple maps
2 parents afe0675 + 62a9fa1 commit 98aeb32

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

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

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
9393

9494
public override Dictionary<string, T> CreateMapWithReference<T>(
9595
ReferenceType referenceType,
96-
Func<MapNode, T> map)
96+
Func<MapNode, T> map)
9797
{
9898
var yamlMap = _node;
9999
if (yamlMap == null)
@@ -104,28 +104,37 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
104104
var nodes = yamlMap.Select(
105105
n =>
106106
{
107-
var entry = new
108-
{
109-
key = n.Key.GetScalarValue(),
110-
value = map(new MapNode(Context, (YamlMappingNode)n.Value))
111-
};
112-
if (entry.value == null)
113-
{
114-
return null; // Body Parameters shouldn't be converted to Parameters
115-
}
116-
// If the component isn't a reference to another component, then point it to itself.
117-
if (entry.value.Reference == null)
107+
var key = n.Key.GetScalarValue();
108+
(string key, T value) entry;
109+
try
118110
{
119-
entry.value.Reference = new OpenApiReference()
111+
Context.StartObject(key);
112+
entry = (
113+
key: key,
114+
value: map(new MapNode(Context, (YamlMappingNode)n.Value))
115+
);
116+
if (entry.value == null)
117+
{
118+
return default; // Body Parameters shouldn't be converted to Parameters
119+
}
120+
// If the component isn't a reference to another component, then point it to itself.
121+
if (entry.value.Reference == null)
120122
{
121-
Type = referenceType,
122-
Id = entry.key
123-
};
123+
entry.value.Reference = new OpenApiReference()
124+
{
125+
Type = referenceType,
126+
Id = entry.key
127+
};
128+
}
129+
}
130+
finally
131+
{
132+
Context.EndObject();
124133
}
125134
return entry;
126135
}
127136
);
128-
return nodes.Where(n => n != null).ToDictionary(k => k.key, v => v.value);
137+
return nodes.Where(n => n != default).ToDictionary(k => k.key, v => v.value);
129138
}
130139

131140
public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
@@ -137,10 +146,21 @@ public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
137146
}
138147

139148
var nodes = yamlMap.Select(
140-
n => new
149+
n =>
141150
{
142-
key = n.Key.GetScalarValue(),
143-
value = map(new ValueNode(Context, (YamlScalarNode)n.Value))
151+
var key = n.Key.GetScalarValue();
152+
try
153+
{
154+
Context.StartObject(key);
155+
YamlScalarNode scalarNode = n.Value as YamlScalarNode;
156+
if (scalarNode == null)
157+
{
158+
throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context);
159+
}
160+
return (key, value: map(new ValueNode(Context, (YamlScalarNode)n.Value)));
161+
} finally {
162+
Context.EndObject();
163+
}
144164
});
145165
return nodes.ToDictionary(k => k.key, v => v.value);
146166
}

test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ JToken GetProp(JToken obj, string prop)
7070
}
7171
}
7272

73-
// Disable as some APIs are currently invalid [Theory(DisplayName = "APIs.guru")]
74-
// [MemberData(nameof(GetSchemas))]
73+
// Disable as some APIs are currently invalid
74+
//[Theory(DisplayName = "APIs.guru")]
75+
//[MemberData(nameof(GetSchemas))]
7576
public async Task EnsureThatICouldParse(string url)
7677
{
7778
var response = await _httpClient.GetAsync(url);

0 commit comments

Comments
 (0)