Skip to content

Fix error reporting for simple maps #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)

public override Dictionary<string, T> CreateMapWithReference<T>(
ReferenceType referenceType,
Func<MapNode, T> map)
Func<MapNode, T> map)
{
var yamlMap = _node;
if (yamlMap == null)
Expand All @@ -104,28 +104,37 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
var nodes = yamlMap.Select(
n =>
{
var entry = new
{
key = n.Key.GetScalarValue(),
value = map(new MapNode(Context, (YamlMappingNode)n.Value))
};
if (entry.value == null)
{
return null; // Body Parameters shouldn't be converted to Parameters
}
// If the component isn't a reference to another component, then point it to itself.
if (entry.value.Reference == null)
var key = n.Key.GetScalarValue();
(string key, T value) entry;
try
{
entry.value.Reference = new OpenApiReference()
Context.StartObject(key);
entry = (
key: key,
value: map(new MapNode(Context, (YamlMappingNode)n.Value))
);
if (entry.value == null)
{
return default; // Body Parameters shouldn't be converted to Parameters
}
// If the component isn't a reference to another component, then point it to itself.
if (entry.value.Reference == null)
{
Type = referenceType,
Id = entry.key
};
entry.value.Reference = new OpenApiReference()
{
Type = referenceType,
Id = entry.key
};
}
}
finally
{
Context.EndObject();
}
return entry;
}
);
return nodes.Where(n => n != null).ToDictionary(k => k.key, v => v.value);
return nodes.Where(n => n != default).ToDictionary(k => k.key, v => v.value);
}

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

var nodes = yamlMap.Select(
n => new
n =>
{
key = n.Key.GetScalarValue(),
value = map(new ValueNode(Context, (YamlScalarNode)n.Value))
var key = n.Key.GetScalarValue();
try
{
Context.StartObject(key);
YamlScalarNode scalarNode = n.Value as YamlScalarNode;
if (scalarNode == null)
{
throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context);
}
return (key, value: map(new ValueNode(Context, (YamlScalarNode)n.Value)));
} finally {
Context.EndObject();
}
});
return nodes.ToDictionary(k => k.key, v => v.value);
}
Expand Down
5 changes: 3 additions & 2 deletions test/Microsoft.OpenApi.SmokeTests/ApiGurus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ JToken GetProp(JToken obj, string prop)
}
}

// Disable as some APIs are currently invalid [Theory(DisplayName = "APIs.guru")]
// [MemberData(nameof(GetSchemas))]
// Disable as some APIs are currently invalid
//[Theory(DisplayName = "APIs.guru")]
//[MemberData(nameof(GetSchemas))]
public async Task EnsureThatICouldParse(string url)
{
var response = await _httpClient.GetAsync(url);
Expand Down