Skip to content

Commit 272b5b4

Browse files
VisualBeanMads Glahder
andauthored
missing ref (LEGO#28)
* missing ref * Newline at end of file * Update test/LEGO.AsyncAPI.E2E.Tests/readers/samples/AsyncApi/MissingRefCompleteKafkaSpec.json Co-authored-by: Mads Glahder <32570017+boiled-water-tsar@users.noreply.github.com>
1 parent ba05af7 commit 272b5b4

8 files changed

Lines changed: 266 additions & 10 deletions

File tree

‎examples/LEGO.AsyncAPI.Readers.JsonExample/LEGO.AsyncAPI.Readers.JsonExample.csproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@
2323
</PackageReference>
2424
</ItemGroup>
2525

26+
<ItemGroup>
27+
<ProjectReference Include="..\..\src\LEGO.AsyncAPI.Readers\LEGO.AsyncAPI.Readers.csproj" />
28+
</ItemGroup>
29+
2630
</Project>

‎examples/LEGO.AsyncAPI.Readers.JsonExample/Program.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static async Task Main()
1616

1717
if (diagnostic.HasError)
1818
{
19-
Console.WriteLine($"Error during spec parsing: {diagnostic.Error}");
19+
Console.WriteLine($"Error during spec parsing: {diagnostic.Errors.First()}");
2020
}
2121
else
2222
{

‎examples/LEGO.AsyncAPI.Writers.Example/LEGO.AsyncAPI.Writers.Example.csproj‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@
2020
<PackageReference Include="LEGO.AsyncAPI.Writers" Version="*-*" />
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<ProjectReference Include="..\..\src\LEGO.AsyncAPI.Readers\LEGO.AsyncAPI.Readers.csproj" />
25+
<ProjectReference Include="..\..\src\LEGO.AsyncAPI.Writers\LEGO.AsyncAPI.Writers.csproj" />
26+
</ItemGroup>
27+
2328
</Project>

‎src/LEGO.AsyncAPI.Readers/AsyncApiDiagnostic.cs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace LEGO.AsyncAPI.Readers
44
{
55
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
68

79
/// <summary>
810
/// Object containing errors that happened during AsyncApi parsing.
@@ -15,17 +17,21 @@ public class AsyncApiDiagnostic
1517
/// <param name="e"></param>
1618
public AsyncApiDiagnostic(Exception e = null)
1719
{
18-
this.Error = e;
20+
this.Errors = new List<Exception>();
21+
if (e is not null)
22+
{
23+
this.Errors.Add(e);
24+
}
1925
}
2026

2127
/// <summary>
2228
/// Returns true if Error value is not null.
2329
/// </summary>
24-
public bool HasError => this.Error != null;
30+
public bool HasError => this.Errors.Any();
2531

2632
/// <summary>
2733
/// Gets an error.
2834
/// </summary>
29-
public Exception Error { get; }
35+
public List<Exception> Errors { get; }
3036
}
31-
}
37+
}

‎src/LEGO.AsyncAPI/NewtonUtils/JTokenExtensions.cs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LEGO.AsyncAPI.NewtonUtils
44
{
5+
using System;
56
using System.Globalization;
67
using System.Linq;
78
using Newtonsoft.Json.Linq;
@@ -32,6 +33,10 @@ public static void ResolveReferences(this JToken root)
3233
{
3334
refObj.Replace(original);
3435
}
36+
else
37+
{
38+
throw new Exception($"Reference '{path}' could not be found.");
39+
}
3540
}
3641
}
3742
while (container.Descendants().OfType<JObject>().Where(IsRefObject).Any());
@@ -74,7 +79,7 @@ private static JToken ResolveRef(JToken token, string path)
7479
if (token is JObject obj)
7580
{
7681
token = obj[component];
77-
if (token["$id"] != null && count == components.Length)
82+
if (token?["$id"] != null && count == components.Length)
7883
{
7984
token = obj[component].DeepClone();
8085
token["$id"].Parent.Remove();
@@ -84,10 +89,6 @@ private static JToken ResolveRef(JToken token, string path)
8489
{
8590
token = token[int.Parse(component, NumberFormatInfo.InvariantInfo)];
8691
}
87-
else
88-
{
89-
return null;
90-
}
9192
}
9293

9394
return token;

‎test/LEGO.AsyncAPI.E2E.Tests/LEGO.AsyncAPI.E2E.Tests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<EmbeddedResource Include="Readers\Samples\AsyncApi\CompleteUsingComponentReferences.json" />
7474
<EmbeddedResource Include="Readers\Samples\AsyncApi\CompleteKafkaSpec.yml" />
7575
<EmbeddedResource Include="Readers\Samples\AsyncApi\Minimal.json" />
76+
<EmbeddedResource Include="Readers\Samples\AsyncApi\MissingRefCompleteKafkaSpec.json" />
7677
<EmbeddedResource Include="Readers\Samples\AsyncApi\schema-v2.3.0.json" />
7778
<None Remove="Readers\Samples\AsyncApi\CompleteKafkaSpec.json" />
7879
<None Remove="Readers\Samples\AsyncApi\CompleteKafkaSpec.yml" />
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
{
2+
"asyncapi": "2.3.0",
3+
"info": {
4+
"title": "Streetlights Kafka API",
5+
"version": "1.0.0",
6+
"description": "The Smartylighting Streetlights API allows you to remotely manage the city lights.\n",
7+
"license": {
8+
"name": "Apache 2.0",
9+
"url": "https://www.apache.org/licenses/LICENSE-2.0"
10+
}
11+
},
12+
"servers": {
13+
"test": {
14+
"url": "test.mykafkacluster.org:8092",
15+
"protocol": "kafka-secure",
16+
"description": "Test broker",
17+
"security": [
18+
{
19+
"saslScram": []
20+
}
21+
]
22+
}
23+
},
24+
"defaultContentType": "application/json",
25+
"channels": {
26+
"smartylighting.streetlights.1.0.event.{streetlightId}.lighting.measured": {
27+
"description": "The topic on which measured values may be produced and consumed.",
28+
"parameters": {
29+
"streetlightId": {
30+
"$ref": "#/components/parameters/streetlightId"
31+
}
32+
},
33+
"publish": {
34+
"summary": "Inform about environmental lighting conditions of a particular streetlight.",
35+
"operationId": "receiveLightMeasurement",
36+
"traits": [
37+
{
38+
"$ref": "#/components/operationTraits/kafka"
39+
}
40+
],
41+
"message": {
42+
"$ref": "#/components/messages/lightMeasured"
43+
}
44+
}
45+
},
46+
"smartylighting.streetlights.1.0.action.{streetlightId}.turn.on": {
47+
"parameters": {
48+
"streetlightId": {
49+
"$ref": "#/components/parameters/streetlightId"
50+
}
51+
},
52+
"subscribe": {
53+
"operationId": "turnOn",
54+
"traits": [
55+
{
56+
"$ref": "#/components/operationTraits/kafka"
57+
}
58+
],
59+
"message": {
60+
"$ref": "#/components/messages/turnOnOff"
61+
}
62+
}
63+
},
64+
"smartylighting.streetlights.1.0.action.{streetlightId}.turn.off": {
65+
"parameters": {
66+
"streetlightId": {
67+
"$ref": "#/components/parameters/streetlightId"
68+
}
69+
},
70+
"subscribe": {
71+
"operationId": "turnOff",
72+
"traits": [
73+
{
74+
"$ref": "#/components/operationTraits/kafka"
75+
}
76+
],
77+
"message": {
78+
"$ref": "#/components/messages/turnOnOff"
79+
}
80+
}
81+
},
82+
"smartylighting.streetlights.1.0.action.{streetlightId}.dim": {
83+
"parameters": {
84+
"streetlightId": {
85+
"$ref": "#/components/parameters/streetlightId"
86+
}
87+
},
88+
"subscribe": {
89+
"operationId": "dimLight",
90+
"traits": [
91+
{
92+
"$ref": "#/components/operationTraits/kafka"
93+
}
94+
],
95+
"message": {
96+
"$ref": "#/components/messages/dimLight"
97+
}
98+
}
99+
}
100+
},
101+
"components": {
102+
"messages": {
103+
"lightMeasured": {
104+
"name": "lightMeasured",
105+
"title": "Light measured",
106+
"summary": "Inform about environmental lighting conditions of a particular streetlight.",
107+
"contentType": "application/json",
108+
"traits": [
109+
{
110+
"$ref": "#/components/messageTraits/commonHeaders"
111+
}
112+
],
113+
"payload": {
114+
"$ref": "#/components/schemas/lightMeasuredPayload"
115+
}
116+
},
117+
"turnOnOff": {
118+
"name": "turnOnOff",
119+
"title": "Turn on/off",
120+
"summary": "Command a particular streetlight to turn the lights on or off.",
121+
"traits": [
122+
{
123+
"$ref": "#/components/messageTraits/commonHeaders"
124+
}
125+
],
126+
"payload": {
127+
"$ref": "#/components/schemas/turnOnOffPayload"
128+
}
129+
},
130+
"dimLight": {
131+
"name": "dimLight",
132+
"title": "Dim light",
133+
"summary": "Command a particular streetlight to dim the lights.",
134+
"traits": [
135+
{
136+
"$ref": "#/components/messageTraits/commonHeaders"
137+
}
138+
],
139+
"payload": {
140+
"$ref": "#/components/schemas/dimLightPayload"
141+
}
142+
}
143+
},
144+
"schemas": {
145+
"lightMeasuredPayload": {
146+
"type": "object",
147+
"properties": {
148+
"lumens": {
149+
"type": "integer",
150+
"minimum": "0",
151+
"description": "Light intensity measured in lumens."
152+
},
153+
"sentAt": {
154+
"$ref": "#/components/schemas/sentAt"
155+
}
156+
}
157+
},
158+
"turnOnOffPayload": {
159+
"type": "object",
160+
"properties": {
161+
"command": {
162+
"type": "string",
163+
"enum": [ "on", "off" ],
164+
"description": "Whether to turn on or off the light."
165+
},
166+
"sentAt": {
167+
"$ref": "#/components/schemas/sentAt"
168+
}
169+
}
170+
},
171+
"dimLightPayload": {
172+
"type": "object",
173+
"properties": {
174+
"percentage": {
175+
"type": "integer",
176+
"description": "Percentage to which the light should be dimmed to.",
177+
"minimum": "0",
178+
"maximum": "100"
179+
},
180+
"sentAt": {
181+
"$ref": "#/components/schemas/sentAt"
182+
}
183+
}
184+
},
185+
"sentAt": {
186+
"type": "string",
187+
"format": "date-time",
188+
"description": "Date and time when the message was sent."
189+
}
190+
},
191+
"securitySchemes": {
192+
"saslScram": {
193+
"type": "scramSha256",
194+
"description": "Provide your username and password for SASL/SCRAM authentication"
195+
}
196+
},
197+
"parameters": {
198+
"StreetlightId": {
199+
"description": "The ID of the streetlight.",
200+
"schema": {
201+
"type": "string"
202+
}
203+
}
204+
},
205+
"messageTraits": {
206+
"commonHeaders": {
207+
"headers": {
208+
"type": "object",
209+
"properties": {
210+
"my-app-header": {
211+
"type": "integer",
212+
"minimum": "0",
213+
"maximum": "100"
214+
}
215+
}
216+
}
217+
}
218+
},
219+
"operationTraits": {
220+
"kafka": {
221+
"bindings": {
222+
"kafka": {
223+
"clientId": {
224+
"type": "string",
225+
"enum": [ "my-app-id" ]
226+
}
227+
}
228+
}
229+
}
230+
}
231+
}
232+
}

‎test/LEGO.AsyncAPI.E2E.Tests/readers/samples/AsyncApi/ShouldConsumeAsyncApiDocument.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace LEGO.AsyncAPI.E2E.Tests.Readers.Samples.AsyncApi
22
{
3+
using System;
34
using System.Linq;
45
using LEGO.AsyncAPI.Models;
56
using LEGO.AsyncAPI.Models.Bindings.OperationBindings;
@@ -53,6 +54,12 @@ public void CompleteJsonWithKafkaSpecExampleData()
5354
this.AssertKafkaSpecExample(output);
5455
}
5556

57+
[Fact]
58+
public void CompleteJsonWithMissingRefKafkaSpecExampleData()
59+
{
60+
Assert.Throws<Exception>(() => this.AsyncApiAsyncApiReader.Read(this.GetStream("MissingRefCompleteKafkaSpec.json")));
61+
}
62+
5663
[Fact]
5764
public void CompleteJsonWithKafkaSpecExampleDataDeserializedTwice()
5865
{

0 commit comments

Comments
 (0)