Skip to content

Commit abfebb1

Browse files
authored
Merge pull request #433 from VitaliyKurokhtin/vvk/diagnostic-in-context
Diagnostic moved from MapNode to ParsingContext
2 parents 38287d7 + 1c8398c commit abfebb1

27 files changed

+180
-200
lines changed

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
5555
return new OpenApiDocument();
5656
}
5757

58-
context = new ParsingContext
58+
context = new ParsingContext(diagnostic)
5959
{
6060
ExtensionParsers = _settings.ExtensionParsers,
6161
BaseUrl = _settings.BaseUrl
@@ -66,7 +66,7 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
6666
try
6767
{
6868
// Parse the OpenAPI Document
69-
document = context.Parse(yamlDocument, diagnostic);
69+
document = context.Parse(yamlDocument);
7070

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

131-
context = new ParsingContext
131+
context = new ParsingContext(diagnostic)
132132
{
133133
ExtensionParsers = _settings.ExtensionParsers
134134
};
@@ -138,7 +138,7 @@ public T ReadFragment<T>(Stream input, OpenApiSpecVersion version, out OpenApiDi
138138
try
139139
{
140140
// Parse the OpenAPI element
141-
element = context.ParseFragment<T>(yamlDocument, version, diagnostic);
141+
element = context.ParseFragment<T>(yamlDocument, version);
142142
}
143143
catch (OpenApiException ex)
144144
{

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ internal class ListNode : ParseNode, IEnumerable<ParseNode>
1717
{
1818
private readonly YamlSequenceNode _nodeList;
1919

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

35-
return _nodeList.Select(n => map(new MapNode(Context, Diagnostic, n as YamlMappingNode)))
34+
return _nodeList.Select(n => map(new MapNode(Context, n as YamlMappingNode)))
3635
.Where(i => i != null)
3736
.ToList();
3837
}
3938

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

55-
return _nodeList.Select(n => map(new ValueNode(Context, Diagnostic, n))).ToList();
54+
return _nodeList.Select(n => map(new ValueNode(Context, n))).ToList();
5655
}
5756

5857
public IEnumerator<ParseNode> GetEnumerator()
5958
{
60-
return _nodeList.Select(n => Create(Context, Diagnostic, n)).ToList().GetEnumerator();
59+
return _nodeList.Select(n => Create(Context, n)).ToList().GetEnumerator();
6160
}
6261

6362
IEnumerator IEnumerable.GetEnumerator()

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

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ internal class MapNode : ParseNode, IEnumerable<PropertyNode>
2323
private readonly YamlMappingNode _node;
2424
private readonly List<PropertyNode> _nodes;
2525

26-
public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, string yamlString) :
27-
this(context, diagnostic, (YamlMappingNode)YamlHelper.ParseYamlString(yamlString))
26+
public MapNode(ParsingContext context, string yamlString) :
27+
this(context, (YamlMappingNode)YamlHelper.ParseYamlString(yamlString))
2828
{
2929
}
3030

31-
public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node) : base(
32-
context,
33-
diagnostic)
31+
public MapNode(ParsingContext context, YamlNode node) : base(
32+
context)
3433
{
3534
if (!(node is YamlMappingNode mapNode))
3635
{
@@ -40,7 +39,7 @@ public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode no
4039
this._node = mapNode;
4140

4241
_nodes = this._node.Children
43-
.Select(kvp => new PropertyNode(Context, Diagnostic, kvp.Key.GetScalarValue(), kvp.Value))
42+
.Select(kvp => new PropertyNode(Context, kvp.Key.GetScalarValue(), kvp.Value))
4443
.Cast<PropertyNode>()
4544
.ToList();
4645
}
@@ -52,7 +51,7 @@ public PropertyNode this[string key]
5251
YamlNode node = null;
5352
if (this._node.Children.TryGetValue(new YamlScalarNode(key), out node))
5453
{
55-
return new PropertyNode(Context, Diagnostic, key, this._node.Children[new YamlScalarNode(key)]);
54+
return new PropertyNode(Context, key, this._node.Children[new YamlScalarNode(key)]);
5655
}
5756

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

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

82-
public override Dictionary<string, T> CreateMapWithReference<T>(
83-
ReferenceType referenceType,
84-
Func<MapNode, T> map)
85-
{
86-
var yamlMap = _node;
87-
if (yamlMap == null)
88-
{
89-
throw new OpenApiException($"Expected map at line {yamlMap.Start.Line} while parsing {typeof(T).Name}");
90-
}
91-
92-
var nodes = yamlMap.Select(
93-
n => {
94-
var entry = new
95-
{
96-
key = n.Key.GetScalarValue(),
97-
value = map(new MapNode(Context, Diagnostic, (YamlMappingNode)n.Value))
98-
};
99-
if (entry.value == null)
100-
{
101-
return null; // Body Parameters shouldn't be converted to Parameters
102-
}
103-
entry.value.Reference = new OpenApiReference()
104-
{
105-
Type = referenceType,
106-
Id = entry.key
107-
};
108-
return entry;
109-
}
110-
);
111-
return nodes.Where(n => n!= null).ToDictionary(k => k.key, v => v.value);
112-
}
81+
public override Dictionary<string, T> CreateMapWithReference<T>(
82+
ReferenceType referenceType,
83+
Func<MapNode, T> map)
84+
{
85+
var yamlMap = _node;
86+
if (yamlMap == null)
87+
{
88+
throw new OpenApiException($"Expected map at line {yamlMap.Start.Line} while parsing {typeof(T).Name}");
89+
}
90+
91+
var nodes = yamlMap.Select(
92+
n =>
93+
{
94+
var entry = new
95+
{
96+
key = n.Key.GetScalarValue(),
97+
value = map(new MapNode(Context, (YamlMappingNode)n.Value))
98+
};
99+
if (entry.value == null)
100+
{
101+
return null; // Body Parameters shouldn't be converted to Parameters
102+
}
103+
entry.value.Reference = new OpenApiReference()
104+
{
105+
Type = referenceType,
106+
Id = entry.key
107+
};
108+
return entry;
109+
}
110+
);
111+
return nodes.Where(n => n != null).ToDictionary(k => k.key, v => v.value);
112+
}
113113

114114
public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
115115
{
@@ -123,7 +123,7 @@ public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
123123
n => new
124124
{
125125
key = n.Key.GetScalarValue(),
126-
value = map(new ValueNode(Context, Diagnostic, (YamlScalarNode)n.Value))
126+
value = map(new ValueNode(Context, (YamlScalarNode)n.Value))
127127
});
128128
return nodes.ToDictionary(k => k.key, v => v.value);
129129
}
@@ -140,18 +140,18 @@ IEnumerator IEnumerable.GetEnumerator()
140140

141141
public override string GetRaw()
142142
{
143-
var x = new Serializer(new SerializerSettings(new JsonSchema()) {EmitJsonComptible = true});
143+
var x = new Serializer(new SerializerSettings(new JsonSchema()) { EmitJsonComptible = true });
144144
return x.Serialize(_node);
145145
}
146146

147147
public T GetReferencedObject<T>(ReferenceType referenceType, string referenceId)
148148
where T : IOpenApiReferenceable, new()
149149
{
150150
return new T()
151-
{
152-
UnresolvedReference = true,
153-
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId,referenceType)
154-
};
151+
{
152+
UnresolvedReference = true,
153+
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId, referenceType)
154+
};
155155
}
156156

157157
public string GetReferencePointer()

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ namespace Microsoft.OpenApi.Readers.ParseNodes
1515
{
1616
internal abstract class ParseNode
1717
{
18-
protected ParseNode(ParsingContext parsingContext, OpenApiDiagnostic diagnostic)
18+
protected ParseNode(ParsingContext parsingContext)
1919
{
2020
Context = parsingContext;
21-
Diagnostic = diagnostic;
2221
}
2322

2423
public ParsingContext Context { get; }
2524

26-
public OpenApiDiagnostic Diagnostic { get; }
27-
2825
public MapNode CheckMapNode(string nodeName)
2926
{
3027
if (!(this is MapNode mapNode))
@@ -35,20 +32,20 @@ public MapNode CheckMapNode(string nodeName)
3532
return mapNode;
3633
}
3734

38-
public static ParseNode Create(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node)
35+
public static ParseNode Create(ParsingContext context, YamlNode node)
3936
{
4037

4138
if (node is YamlSequenceNode listNode)
4239
{
43-
return new ListNode(context, diagnostic, listNode);
40+
return new ListNode(context, listNode);
4441
}
4542

4643
if (node is YamlMappingNode mapNode)
4744
{
48-
return new MapNode(context, diagnostic, mapNode);
45+
return new MapNode(context, mapNode);
4946
}
5047

51-
return new ValueNode(context, diagnostic, node as YamlScalarNode);
48+
return new ValueNode(context, node as YamlScalarNode);
5249
}
5350

5451
public virtual List<T> CreateList<T>(Func<MapNode, T> map)

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ namespace Microsoft.OpenApi.Readers.ParseNodes
1414
{
1515
internal class PropertyNode : ParseNode
1616
{
17-
public PropertyNode(ParsingContext context, OpenApiDiagnostic diagnostic, string name, YamlNode node) : base(
18-
context,
19-
diagnostic)
17+
public PropertyNode(ParsingContext context, string name, YamlNode node) : base(
18+
context)
2019
{
2120
Name = name;
22-
Value = Create(context, diagnostic, node);
21+
Value = Create(context, node);
2322
}
2423

2524
public string Name { get; set; }
@@ -43,12 +42,12 @@ public void ParseField<T>(
4342
}
4443
catch (OpenApiReaderException ex)
4544
{
46-
Diagnostic.Errors.Add(new OpenApiError(ex));
45+
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
4746
}
4847
catch (OpenApiException ex)
4948
{
5049
ex.Pointer = Context.GetLocation();
51-
Diagnostic.Errors.Add(new OpenApiError(ex));
50+
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
5251
}
5352
finally
5453
{
@@ -67,12 +66,12 @@ public void ParseField<T>(
6766
}
6867
catch (OpenApiReaderException ex)
6968
{
70-
Diagnostic.Errors.Add(new OpenApiError(ex));
69+
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
7170
}
7271
catch (OpenApiException ex)
7372
{
7473
ex.Pointer = Context.GetLocation();
75-
Diagnostic.Errors.Add(new OpenApiError(ex));
74+
Context.Diagnostic.Errors.Add(new OpenApiError(ex));
7675
}
7776
finally
7877
{
@@ -81,7 +80,7 @@ public void ParseField<T>(
8180
}
8281
else
8382
{
84-
Diagnostic.Errors.Add(
83+
Context.Diagnostic.Errors.Add(
8584
new OpenApiError("", $"{Name} is not a valid property at {Context.GetLocation()}"));
8685
}
8786
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ internal class RootNode : ParseNode
1414

1515
public RootNode(
1616
ParsingContext context,
17-
OpenApiDiagnostic diagnostic,
18-
YamlDocument yamlDocument) : base(context, diagnostic)
17+
YamlDocument yamlDocument) : base(context)
1918
{
2019
_yamlDocument = yamlDocument;
2120
}
@@ -28,12 +27,12 @@ public ParseNode Find(JsonPointer referencePointer)
2827
return null;
2928
}
3029

31-
return Create(Context, Diagnostic, yamlNode);
30+
return Create(Context, yamlNode);
3231
}
3332

3433
public MapNode GetMap()
3534
{
36-
return new MapNode(Context, Diagnostic, (YamlMappingNode)_yamlDocument.RootNode);
35+
return new MapNode(Context, (YamlMappingNode)_yamlDocument.RootNode);
3736
}
3837
}
3938
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ internal class ValueNode : ParseNode
1313
{
1414
private readonly YamlScalarNode _node;
1515

16-
public ValueNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node) : base(
17-
context,
18-
diagnostic)
16+
public ValueNode(ParsingContext context, YamlNode node) : base(
17+
context)
1918
{
2019
if (!(node is YamlScalarNode scalarNode))
2120
{

0 commit comments

Comments
 (0)