Skip to content
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
8 changes: 4 additions & 4 deletions src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
return new OpenApiDocument();
}

context = new ParsingContext
context = new ParsingContext(diagnostic)
{
ExtensionParsers = _settings.ExtensionParsers,
BaseUrl = _settings.BaseUrl
Expand All @@ -66,7 +66,7 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
try
{
// Parse the OpenAPI Document
document = context.Parse(yamlDocument, diagnostic);
document = context.Parse(yamlDocument);

// Resolve References if requested
switch (_settings.ReferenceResolution)
Expand Down Expand Up @@ -128,7 +128,7 @@ public T ReadFragment<T>(Stream input, OpenApiSpecVersion version, out OpenApiDi
return default(T);
}

context = new ParsingContext
context = new ParsingContext(diagnostic)
{
ExtensionParsers = _settings.ExtensionParsers
};
Expand All @@ -138,7 +138,7 @@ public T ReadFragment<T>(Stream input, OpenApiSpecVersion version, out OpenApiDi
try
{
// Parse the OpenAPI element
element = context.ParseFragment<T>(yamlDocument, version, diagnostic);
element = context.ParseFragment<T>(yamlDocument, version);
}
catch (OpenApiException ex)
{
Expand Down
13 changes: 6 additions & 7 deletions src/Microsoft.OpenApi.Readers/ParseNodes/ListNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ internal class ListNode : ParseNode, IEnumerable<ParseNode>
{
private readonly YamlSequenceNode _nodeList;

public ListNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlSequenceNode sequenceNode) : base(
context,
diagnostic)
public ListNode(ParsingContext context, YamlSequenceNode sequenceNode) : base(
context)
{
_nodeList = sequenceNode;
}
Expand All @@ -32,14 +31,14 @@ public override List<T> CreateList<T>(Func<MapNode, T> map)
$"Expected list at line {_nodeList.Start.Line} while parsing {typeof(T).Name}");
}

return _nodeList.Select(n => map(new MapNode(Context, Diagnostic, n as YamlMappingNode)))
return _nodeList.Select(n => map(new MapNode(Context, n as YamlMappingNode)))
.Where(i => i != null)
.ToList();
}

public override List<IOpenApiAny> CreateListOfAny()
{
return _nodeList.Select(n => ParseNode.Create(Context, Diagnostic,n).CreateAny())
return _nodeList.Select(n => ParseNode.Create(Context, n).CreateAny())
.Where(i => i != null)
.ToList();
}
Expand All @@ -52,12 +51,12 @@ public override List<T> CreateSimpleList<T>(Func<ValueNode, T> map)
$"Expected list at line {_nodeList.Start.Line} while parsing {typeof(T).Name}");
}

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

public IEnumerator<ParseNode> GetEnumerator()
{
return _nodeList.Select(n => Create(Context, Diagnostic, n)).ToList().GetEnumerator();
return _nodeList.Select(n => Create(Context, n)).ToList().GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
Expand Down
90 changes: 45 additions & 45 deletions src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ internal class MapNode : ParseNode, IEnumerable<PropertyNode>
private readonly YamlMappingNode _node;
private readonly List<PropertyNode> _nodes;

public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, string yamlString) :
this(context, diagnostic, (YamlMappingNode)YamlHelper.ParseYamlString(yamlString))
public MapNode(ParsingContext context, string yamlString) :
this(context, (YamlMappingNode)YamlHelper.ParseYamlString(yamlString))
{
}

public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node) : base(
context,
diagnostic)
public MapNode(ParsingContext context, YamlNode node) : base(
context)
{
if (!(node is YamlMappingNode mapNode))
{
Expand All @@ -40,7 +39,7 @@ public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode no
this._node = mapNode;

_nodes = this._node.Children
.Select(kvp => new PropertyNode(Context, Diagnostic, kvp.Key.GetScalarValue(), kvp.Value))
.Select(kvp => new PropertyNode(Context, kvp.Key.GetScalarValue(), kvp.Value))
.Cast<PropertyNode>()
.ToList();
}
Expand All @@ -52,7 +51,7 @@ public PropertyNode this[string key]
YamlNode node = null;
if (this._node.Children.TryGetValue(new YamlScalarNode(key), out node))
{
return new PropertyNode(Context, Diagnostic, key, this._node.Children[new YamlScalarNode(key)]);
return new PropertyNode(Context, key, this._node.Children[new YamlScalarNode(key)]);
}

return null;
Expand All @@ -73,43 +72,44 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
key = n.Key.GetScalarValue(),
value = n.Value as YamlMappingNode == null
? default(T)
: map(new MapNode(Context, Diagnostic, n.Value as YamlMappingNode))
: map(new MapNode(Context, n.Value as YamlMappingNode))
});

return nodes.ToDictionary(k => k.key, v => v.value);
}

public override Dictionary<string, T> CreateMapWithReference<T>(
ReferenceType referenceType,
Func<MapNode, T> map)
{
var yamlMap = _node;
if (yamlMap == null)
{
throw new OpenApiException($"Expected map at line {yamlMap.Start.Line} while parsing {typeof(T).Name}");
}

var nodes = yamlMap.Select(
n => {
var entry = new
{
key = n.Key.GetScalarValue(),
value = map(new MapNode(Context, Diagnostic, (YamlMappingNode)n.Value))
};
if (entry.value == null)
{
return null; // Body Parameters shouldn't be converted to Parameters
}
entry.value.Reference = new OpenApiReference()
{
Type = referenceType,
Id = entry.key
};
return entry;
}
);
return nodes.Where(n => n!= null).ToDictionary(k => k.key, v => v.value);
}
public override Dictionary<string, T> CreateMapWithReference<T>(
ReferenceType referenceType,
Func<MapNode, T> map)
{
var yamlMap = _node;
if (yamlMap == null)
{
throw new OpenApiException($"Expected map at line {yamlMap.Start.Line} while parsing {typeof(T).Name}");
}

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
}
entry.value.Reference = new OpenApiReference()
{
Type = referenceType,
Id = entry.key
};
return entry;
}
);
return nodes.Where(n => n != null).ToDictionary(k => k.key, v => v.value);
}

public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
{
Expand All @@ -123,7 +123,7 @@ public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
n => new
{
key = n.Key.GetScalarValue(),
value = map(new ValueNode(Context, Diagnostic, (YamlScalarNode)n.Value))
value = map(new ValueNode(Context, (YamlScalarNode)n.Value))
});
return nodes.ToDictionary(k => k.key, v => v.value);
}
Expand All @@ -140,18 +140,18 @@ IEnumerator IEnumerable.GetEnumerator()

public override string GetRaw()
{
var x = new Serializer(new SerializerSettings(new JsonSchema()) {EmitJsonComptible = true});
var x = new Serializer(new SerializerSettings(new JsonSchema()) { EmitJsonComptible = true });
return x.Serialize(_node);
}

public T GetReferencedObject<T>(ReferenceType referenceType, string referenceId)
where T : IOpenApiReferenceable, new()
{
return new T()
{
UnresolvedReference = true,
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId,referenceType)
};
{
UnresolvedReference = true,
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId, referenceType)
};
}

public string GetReferencePointer()
Expand Down
13 changes: 5 additions & 8 deletions src/Microsoft.OpenApi.Readers/ParseNodes/ParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal abstract class ParseNode
{
protected ParseNode(ParsingContext parsingContext, OpenApiDiagnostic diagnostic)
protected ParseNode(ParsingContext parsingContext)
{
Context = parsingContext;
Diagnostic = diagnostic;
}

public ParsingContext Context { get; }

public OpenApiDiagnostic Diagnostic { get; }

public MapNode CheckMapNode(string nodeName)
{
if (!(this is MapNode mapNode))
Expand All @@ -35,20 +32,20 @@ public MapNode CheckMapNode(string nodeName)
return mapNode;
}

public static ParseNode Create(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node)
public static ParseNode Create(ParsingContext context, YamlNode node)
{

if (node is YamlSequenceNode listNode)
{
return new ListNode(context, diagnostic, listNode);
return new ListNode(context, listNode);
}

if (node is YamlMappingNode mapNode)
{
return new MapNode(context, diagnostic, mapNode);
return new MapNode(context, mapNode);
}

return new ValueNode(context, diagnostic, node as YamlScalarNode);
return new ValueNode(context, node as YamlScalarNode);
}

public virtual List<T> CreateList<T>(Func<MapNode, T> map)
Expand Down
17 changes: 8 additions & 9 deletions src/Microsoft.OpenApi.Readers/ParseNodes/PropertyNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal class PropertyNode : ParseNode
{
public PropertyNode(ParsingContext context, OpenApiDiagnostic diagnostic, string name, YamlNode node) : base(
context,
diagnostic)
public PropertyNode(ParsingContext context, string name, YamlNode node) : base(
context)
{
Name = name;
Value = Create(context, diagnostic, node);
Value = Create(context, node);
}

public string Name { get; set; }
Expand All @@ -43,12 +42,12 @@ public void ParseField<T>(
}
catch (OpenApiReaderException ex)
{
Diagnostic.Errors.Add(new OpenApiError(ex));
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
}
catch (OpenApiException ex)
{
ex.Pointer = Context.GetLocation();
Diagnostic.Errors.Add(new OpenApiError(ex));
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
}
finally
{
Expand All @@ -67,12 +66,12 @@ public void ParseField<T>(
}
catch (OpenApiReaderException ex)
{
Diagnostic.Errors.Add(new OpenApiError(ex));
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
}
catch (OpenApiException ex)
{
ex.Pointer = Context.GetLocation();
Diagnostic.Errors.Add(new OpenApiError(ex));
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
}
finally
{
Expand All @@ -81,7 +80,7 @@ public void ParseField<T>(
}
else
{
Diagnostic.Errors.Add(
Context.Diagnostic.Errors.Add(
new OpenApiError("", $"{Name} is not a valid property at {Context.GetLocation()}"));
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/Microsoft.OpenApi.Readers/ParseNodes/RootNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ internal class RootNode : ParseNode

public RootNode(
ParsingContext context,
OpenApiDiagnostic diagnostic,
YamlDocument yamlDocument) : base(context, diagnostic)
YamlDocument yamlDocument) : base(context)
{
_yamlDocument = yamlDocument;
}
Expand All @@ -28,12 +27,12 @@ public ParseNode Find(JsonPointer referencePointer)
return null;
}

return Create(Context, Diagnostic, yamlNode);
return Create(Context, yamlNode);
}

public MapNode GetMap()
{
return new MapNode(Context, Diagnostic, (YamlMappingNode)_yamlDocument.RootNode);
return new MapNode(Context, (YamlMappingNode)_yamlDocument.RootNode);
}
}
}
5 changes: 2 additions & 3 deletions src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ internal class ValueNode : ParseNode
{
private readonly YamlScalarNode _node;

public ValueNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node) : base(
context,
diagnostic)
public ValueNode(ParsingContext context, YamlNode node) : base(
context)
{
if (!(node is YamlScalarNode scalarNode))
{
Expand Down
Loading