Skip to content

Commit cca306e

Browse files
committed
Fixes based on PR review
1 parent 2688845 commit cca306e

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public OpenApiReaderException(string message) : base(message) { }
3131
/// <param name="node">Parsing node where error occured</param>
3232
public OpenApiReaderException(string message, YamlNode node) : base(message)
3333
{
34-
Pointer = $"#line={node.Start.Line}"; // This only includes line because using a char range causes tests to break due to CR/LF & LF differences
35-
// See https://tools.ietf.org/html/rfc5147 for syntax
34+
// This only includes line because using a char range causes tests to break due to CR/LF & LF differences
35+
// See https://tools.ietf.org/html/rfc5147 for syntax
36+
Pointer = $"#line={node.Start.Line}";
3637
}
3738

3839
/// <summary>

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
4949
{
5050
yamlDocument = LoadYamlDocument(input);
5151
}
52-
catch (SemanticErrorException ex) {
53-
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Index},{ex.End.Index}", ex.Message));
52+
catch (SemanticErrorException ex)
53+
{
54+
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
5455
return new OpenApiDocument();
5556
}
5657
catch (SyntaxErrorException ex)
5758
{
58-
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Index},{ex.End.Index}", ex.Message));
59+
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
60+
return new OpenApiDocument();
61+
}
62+
catch (YamlException ex)
63+
{
64+
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Line}", ex.Message));
5965
return new OpenApiDocument();
6066
}
6167

@@ -128,7 +134,17 @@ public T ReadFragment<T>(Stream input, OpenApiSpecVersion version, out OpenApiDi
128134
}
129135
catch (SyntaxErrorException ex)
130136
{
131-
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Index},{ex.End.Index}", ex.Message));
137+
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
138+
return default(T);
139+
}
140+
catch (SemanticErrorException ex)
141+
{
142+
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
143+
return default(T);
144+
}
145+
catch (YamlException ex)
146+
{
147+
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
132148
return default(T);
133149
}
134150

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.OpenApi.Exceptions;
99
using Microsoft.OpenApi.Interfaces;
1010
using Microsoft.OpenApi.Models;
11+
using Microsoft.OpenApi.Readers.Exceptions;
1112
using SharpYaml.Serialization;
1213

1314
namespace Microsoft.OpenApi.Readers.ParseNodes
@@ -28,7 +29,7 @@ public MapNode CheckMapNode(string nodeName)
2829
{
2930
if (!(this is MapNode mapNode))
3031
{
31-
throw new OpenApiException($"{nodeName} must be a map/object");
32+
throw new OpenApiReaderException($"{nodeName} must be a map/object");
3233
}
3334

3435
return mapNode;
@@ -52,50 +53,50 @@ public static ParseNode Create(ParsingContext context, OpenApiDiagnostic diagnos
5253

5354
public virtual List<T> CreateList<T>(Func<MapNode, T> map)
5455
{
55-
throw new OpenApiException("Cannot create list from this type of node.");
56+
throw new OpenApiReaderException("Cannot create list from this type of node.");
5657
}
5758

5859
public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
5960
{
60-
throw new OpenApiException("Cannot create map from this type of node.");
61+
throw new OpenApiReaderException("Cannot create map from this type of node.");
6162
}
6263

6364
public virtual Dictionary<string, T> CreateMapWithReference<T>(
6465
ReferenceType referenceType,
6566
Func<MapNode, T> map)
6667
where T : class, IOpenApiReferenceable
6768
{
68-
throw new OpenApiException("Cannot create map from this reference.");
69+
throw new OpenApiReaderException("Cannot create map from this reference.");
6970
}
7071

7172
public virtual List<T> CreateSimpleList<T>(Func<ValueNode, T> map)
7273
{
73-
throw new OpenApiException("Cannot create simple list from this type of node.");
74+
throw new OpenApiReaderException("Cannot create simple list from this type of node.");
7475
}
7576

7677
public virtual Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
7778
{
78-
throw new OpenApiException("Cannot create simple map from this type of node.");
79+
throw new OpenApiReaderException("Cannot create simple map from this type of node.");
7980
}
8081

8182
public virtual IOpenApiAny CreateAny()
8283
{
83-
throw new OpenApiException("Cannot create an Any object this type of node.");
84+
throw new OpenApiReaderException("Cannot create an Any object this type of node.");
8485
}
8586

8687
public virtual string GetRaw()
8788
{
88-
throw new OpenApiException("Cannot get raw value from this type of node.");
89+
throw new OpenApiReaderException("Cannot get raw value from this type of node.");
8990
}
9091

9192
public virtual string GetScalarValue()
9293
{
93-
throw new OpenApiException("Cannot create a scalar value from this type of node.");
94+
throw new OpenApiReaderException("Cannot create a scalar value from this type of node.");
9495
}
9596

9697
public virtual List<IOpenApiAny> CreateListOfAny()
9798
{
98-
throw new OpenApiException("Cannot create a list from this type of node.");
99+
throw new OpenApiReaderException("Cannot create a list from this type of node.");
99100
}
100101

101102
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public void ParseField<T>(
6565
Context.StartObject(Name);
6666
map(parentInstance, Name, Value);
6767
}
68+
catch (OpenApiReaderException ex)
69+
{
70+
Diagnostic.Errors.Add(new OpenApiError(ex));
71+
}
6872
catch (OpenApiException ex)
6973
{
7074
ex.Pointer = Context.GetLocation();

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ public ValueNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode
2626

2727
public override string GetScalarValue()
2828
{
29-
var scalarNode = _node;
30-
31-
if (scalarNode == null)
32-
{
33-
throw new OpenApiException($"Expected scalar at line {_node.Start.Line}");
34-
}
35-
36-
return scalarNode.Value;
29+
return _node.Value;
3730
}
3831

3932
/// <summary>

test/Microsoft.OpenApi.Readers.Tests/NodeTests.cs renamed to test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
24
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
65
using FluentAssertions;
76
using Microsoft.OpenApi.Models;
87
using Microsoft.OpenApi.Readers;
@@ -11,9 +10,8 @@
1110

1211
namespace Microsoft.OpenApi.Tests
1312
{
14-
public class NodeTests
13+
public class ParseNodeTests
1514
{
16-
1715
[Fact]
1816
public void BrokenSimpleList()
1917
{

0 commit comments

Comments
 (0)