Skip to content

Commit f3267bc

Browse files
committed
Add location context to all messages
1 parent d623586 commit f3267bc

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public OpenApiReaderException() { }
2424
/// <param name="message">Plain text error message for this exception.</param>
2525
public OpenApiReaderException(string message) : base(message) { }
2626

27+
/// <summary>
28+
/// Initializes the <see cref="OpenApiReaderException"/> class with a custom message.
29+
/// </summary>
30+
/// <param name="message">Plain text error message for this exception.</param>
31+
/// <param name="context">Context of current parsing process.</param>
32+
public OpenApiReaderException(string message, ParsingContext context) : base(message) {
33+
Pointer = context.GetLocation();
34+
}
35+
2736
/// <summary>
2837
/// Initializes the <see cref="OpenApiReaderException"/> class with a message and line, column location of error.
2938
/// </summary>

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public MapNode CheckMapNode(string nodeName)
2626
{
2727
if (!(this is MapNode mapNode))
2828
{
29-
throw new OpenApiReaderException($"{nodeName} must be a map/object");
29+
throw new OpenApiReaderException($"{nodeName} must be a map/object", Context);
3030
}
3131

3232
return mapNode;
@@ -50,51 +50,50 @@ public static ParseNode Create(ParsingContext context, YamlNode node)
5050

5151
public virtual List<T> CreateList<T>(Func<MapNode, T> map)
5252
{
53-
throw new OpenApiReaderException("Cannot create list from this type of node.");
53+
throw new OpenApiReaderException("Cannot create list from this type of node.", Context);
5454
}
5555

5656
public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
5757
{
58-
throw new OpenApiReaderException("Cannot create map from this type of node.");
58+
throw new OpenApiReaderException("Cannot create map from this type of node.", Context);
5959
}
6060

6161
public virtual Dictionary<string, T> CreateMapWithReference<T>(
6262
ReferenceType referenceType,
6363
Func<MapNode, T> map)
6464
where T : class, IOpenApiReferenceable
6565
{
66-
throw new OpenApiReaderException("Cannot create map from this reference.");
66+
throw new OpenApiReaderException("Cannot create map from this reference.", Context);
6767
}
6868

6969
public virtual List<T> CreateSimpleList<T>(Func<ValueNode, T> map)
7070
{
71-
throw new OpenApiReaderException("Cannot create simple list from this type of node.");
71+
throw new OpenApiReaderException("Cannot create simple list from this type of node.", Context);
7272
}
7373

7474
public virtual Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
7575
{
76-
throw new OpenApiReaderException("Cannot create simple map from this type of node.");
76+
throw new OpenApiReaderException("Cannot create simple map from this type of node.", Context);
7777
}
7878

7979
public virtual IOpenApiAny CreateAny()
8080
{
81-
throw new OpenApiReaderException("Cannot create an Any object this type of node.");
81+
throw new OpenApiReaderException("Cannot create an Any object this type of node.", Context);
8282
}
8383

8484
public virtual string GetRaw()
8585
{
86-
throw new OpenApiReaderException("Cannot get raw value from this type of node.");
86+
throw new OpenApiReaderException("Cannot get raw value from this type of node.", Context);
8787
}
8888

8989
public virtual string GetScalarValue()
9090
{
91-
throw new OpenApiReaderException("Cannot create a scalar value from this type of node.");
91+
throw new OpenApiReaderException("Cannot create a scalar value from this type of node.", Context);
9292
}
9393

9494
public virtual List<IOpenApiAny> CreateListOfAny()
9595
{
96-
throw new OpenApiReaderException("Cannot create a list from this type of node.");
96+
throw new OpenApiReaderException("Cannot create a list from this type of node.", Context);
9797
}
98-
9998
}
10099
}

src/Microsoft.OpenApi.Readers/ParsingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void EndObject()
141141
/// </summary>
142142
public string GetLocation()
143143
{
144-
return "#/" + string.Join("/", _currentLocation.Reverse().ToArray());
144+
return "#/" + string.Join("/", _currentLocation.Reverse().Select(s=> s.Replace("~","~0").Replace("/","~1")).ToArray());
145145
}
146146

147147
/// <summary>

test/Microsoft.OpenApi.Readers.Tests/ParseNodeTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,34 @@ public void BrokenSimpleList()
3131
})
3232
});
3333
}
34+
35+
[Fact]
36+
public void BadSchema()
37+
{
38+
var input = @"openapi: 3.0.0
39+
info:
40+
title: foo
41+
version: bar
42+
paths:
43+
'/foo':
44+
get:
45+
responses:
46+
200:
47+
description: ok
48+
content:
49+
application/json:
50+
schema: asdasd
51+
";
52+
53+
var reader = new OpenApiStringReader();
54+
reader.Read(input, out var diagnostic);
55+
56+
diagnostic.Errors.Should().BeEquivalentTo(new List<OpenApiError>() {
57+
new OpenApiError(new OpenApiReaderException("Expected a value.") {
58+
Pointer = "#/~1foo/get/responses/200/content/application~1json/schema"
59+
})
60+
});
61+
}
3462
}
3563
}
64+

0 commit comments

Comments
 (0)