Skip to content

Commit ea0dee1

Browse files
committed
Fixes to syntax related reading errors
1 parent b98b6d0 commit ea0dee1

File tree

10 files changed

+79
-58
lines changed

10 files changed

+79
-58
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using Microsoft.OpenApi.Exceptions;
6+
using SharpYaml.Serialization;
57

68
namespace Microsoft.OpenApi.Readers.Exceptions
79
{
810
/// <summary>
911
/// Defines an exception indicating OpenAPI Reader encountered an issue while reading.
1012
/// </summary>
1113
[Serializable]
12-
public class OpenApiReaderException : Exception
14+
public class OpenApiReaderException : OpenApiException
1315
{
1416
/// <summary>
1517
/// Initializes the <see cref="OpenApiReaderException"/> class.
@@ -22,6 +24,16 @@ public OpenApiReaderException() { }
2224
/// <param name="message">Plain text error message for this exception.</param>
2325
public OpenApiReaderException(string message) : base(message) { }
2426

27+
/// <summary>
28+
/// Initializes the <see cref="OpenApiReaderException"/> class with a message and line, column location of error.
29+
/// </summary>
30+
/// <param name="message">Plain text error message for this exception.</param>
31+
/// <param name="node">Parsing node where error occured</param>
32+
public OpenApiReaderException(string message, YamlNode node) : base(message)
33+
{
34+
Pointer = $"#char={node.Start.Index},{node.End.Index}";
35+
}
36+
2537
/// <summary>
2638
/// Initializes the <see cref="OpenApiReaderException"/> class with a custom message and inner exception.
2739
/// </summary>

src/Microsoft.OpenApi.Readers/OpenApiReaderError.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ 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));
54+
return new OpenApiDocument();
55+
}
5256
catch (SyntaxErrorException ex)
5357
{
54-
diagnostic.Errors.Add(new OpenApiReaderError(ex));
58+
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Index},{ex.End.Index}", ex.Message));
5559
return new OpenApiDocument();
5660
}
5761

@@ -124,7 +128,7 @@ public T ReadFragment<T>(Stream input, OpenApiSpecVersion version, out OpenApiDi
124128
}
125129
catch (SyntaxErrorException ex)
126130
{
127-
diagnostic.Errors.Add(new OpenApiReaderError(ex));
131+
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Index},{ex.End.Index}", ex.Message));
128132
return default(T);
129133
}
130134

@@ -158,7 +162,6 @@ public T ReadFragment<T>(Stream input, OpenApiSpecVersion version, out OpenApiDi
158162
return (T)element;
159163
}
160164

161-
162165
/// <summary>
163166
/// Helper method to turn streams into YamlDocument
164167
/// </summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override List<T> CreateSimpleList<T>(Func<ValueNode, T> map)
5252
$"Expected list at line {_nodeList.Start.Line} while parsing {typeof(T).Name}");
5353
}
5454

55-
return _nodeList.Select(n => map(new ValueNode(Context, Diagnostic, (YamlScalarNode)n))).ToList();
55+
return _nodeList.Select(n => map(new ValueNode(Context, Diagnostic, n))).ToList();
5656
}
5757

5858
public IEnumerator<ParseNode> GetEnumerator()

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.OpenApi.Exceptions;
1010
using Microsoft.OpenApi.Interfaces;
1111
using Microsoft.OpenApi.Models;
12+
using Microsoft.OpenApi.Readers.Exceptions;
1213
using SharpYaml.Schemas;
1314
using SharpYaml.Serialization;
1415

@@ -27,16 +28,16 @@ public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, string yaml
2728
{
2829
}
2930

30-
public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlMappingNode node) : base(
31+
public MapNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node) : base(
3132
context,
3233
diagnostic)
3334
{
34-
if (node == null)
35+
if (!(node is YamlMappingNode mapNode))
3536
{
36-
throw new OpenApiException("Expected map");
37+
throw new OpenApiReaderException("Expected map.", node);
3738
}
3839

39-
this._node = node;
40+
this._node = mapNode;
4041

4142
_nodes = this._node.Children
4243
.Select(kvp => new PropertyNode(Context, Diagnostic, kvp.Key.GetScalarValue(), kvp.Value))

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ protected ParseNode(ParsingContext parsingContext, OpenApiDiagnostic diagnostic)
2626

2727
public MapNode CheckMapNode(string nodeName)
2828
{
29-
var mapNode = this as MapNode;
30-
if (mapNode == null)
29+
if (!(this is MapNode mapNode))
3130
{
3231
throw new OpenApiException($"{nodeName} must be a map/object");
3332
}
@@ -37,15 +36,13 @@ public MapNode CheckMapNode(string nodeName)
3736

3837
public static ParseNode Create(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node)
3938
{
40-
var listNode = node as YamlSequenceNode;
4139

42-
if (listNode != null)
40+
if (node is YamlSequenceNode listNode)
4341
{
4442
return new ListNode(context, diagnostic, listNode);
4543
}
4644

47-
var mapNode = node as YamlMappingNode;
48-
if (mapNode != null)
45+
if (node is YamlMappingNode mapNode)
4946
{
5047
return new MapNode(context, diagnostic, mapNode);
5148
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.OpenApi.Any;
88
using Microsoft.OpenApi.Exceptions;
99
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Readers.Exceptions;
1011
using SharpYaml.Serialization;
1112

1213
namespace Microsoft.OpenApi.Readers.ParseNodes
@@ -40,10 +41,14 @@ public void ParseField<T>(
4041
Context.StartObject(Name);
4142
fixedFieldMap(parentInstance, Value);
4243
}
44+
catch (OpenApiReaderException ex)
45+
{
46+
Diagnostic.Errors.Add(new OpenApiError(ex));
47+
}
4348
catch (OpenApiException ex)
4449
{
4550
ex.Pointer = Context.GetLocation();
46-
Diagnostic.Errors.Add(new OpenApiReaderError(ex));
51+
Diagnostic.Errors.Add(new OpenApiError(ex));
4752
}
4853
finally
4954
{
@@ -63,7 +68,7 @@ public void ParseField<T>(
6368
catch (OpenApiException ex)
6469
{
6570
ex.Pointer = Context.GetLocation();
66-
Diagnostic.Errors.Add(new OpenApiReaderError(ex));
71+
Diagnostic.Errors.Add(new OpenApiError(ex));
6772
}
6873
finally
6974
{

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using Microsoft.OpenApi.Any;
66
using Microsoft.OpenApi.Exceptions;
7+
using Microsoft.OpenApi.Readers.Exceptions;
78
using SharpYaml.Serialization;
89

910
namespace Microsoft.OpenApi.Readers.ParseNodes
@@ -12,10 +13,14 @@ internal class ValueNode : ParseNode
1213
{
1314
private readonly YamlScalarNode _node;
1415

15-
public ValueNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlScalarNode scalarNode) : base(
16+
public ValueNode(ParsingContext context, OpenApiDiagnostic diagnostic, YamlNode node) : base(
1617
context,
1718
diagnostic)
1819
{
20+
if (!(node is YamlScalarNode scalarNode))
21+
{
22+
throw new OpenApiReaderException("Expected a value.", node);
23+
}
1924
_node = scalarNode;
2025
}
2126

src/Microsoft.OpenApi/Models/OpenApiError.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public OpenApiError(string pointer, string message)
4141
/// </summary>
4242
public override string ToString()
4343
{
44-
return Message + (!string.IsNullOrEmpty(Pointer) ? " at " + Pointer : "");
44+
return Message + (!string.IsNullOrEmpty(Pointer) ? " [" + Pointer + "]" : "" );
4545
}
4646
}
4747
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
using Microsoft.OpenApi.Models;
8+
using Microsoft.OpenApi.Readers;
9+
using Microsoft.OpenApi.Readers.Exceptions;
10+
using Xunit;
11+
12+
namespace Microsoft.OpenApi.Tests
13+
{
14+
public class NodeTests
15+
{
16+
17+
[Fact]
18+
public void BrokenSimpleList()
19+
{
20+
var input = @"swagger: 2.0
21+
info:
22+
title: hey
23+
version: 1.0.0
24+
schemes: [ { ""hello"" }]
25+
paths: { }";
26+
27+
var reader = new OpenApiStringReader();
28+
reader.Read(input, out var diagnostic);
29+
30+
diagnostic.Errors.ShouldBeEquivalentTo(new List<OpenApiError>() {
31+
new OpenApiError(new OpenApiReaderException("Expected a value. Line: 4 Column: 11") {
32+
Pointer = "#char=64,64"
33+
})
34+
});
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)