Skip to content

Commit eeb9d8f

Browse files
authored
fix: small adjustments in deserialization validation (#29)
* Small adjustments in deserialization validation * fix: correct validation rules for v2 * fix: remove redudant rule * fix: add v3 host property to map There is bug that validation parse v2 twice, it causes url to be already mapped to host which cause issues. As dirty fix i added 2 properties related to v3... * Revert "fix: add v3 host property to map" This reverts commit df52c06. * fix: add server to components * fix: solve variable conflict * fix: make sure nesting is correct * fix: correcred reference name
1 parent 7cea38b commit eeb9d8f

4 files changed

Lines changed: 45 additions & 17 deletions

File tree

src/ByteBard.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public T ReadFragment<T>(JsonNode input, AsyncApiVersion version, out AsyncApiDi
139139
where T : IAsyncApiElement
140140
{
141141
diagnostic = new AsyncApiDiagnostic();
142+
diagnostic.SpecificationVersion = version;
142143
this.context ??= new ParsingContext(diagnostic, this.settings)
143144
{
144145
ExtensionParsers = this.settings.ExtensionParsers,
@@ -363,58 +364,61 @@ private IAsyncApiSerializable ResolveStreamReference(Stream stream, IAsyncApiRef
363364
}
364365
}
365366

366-
AsyncApiDiagnostic fragmentDiagnostic = new AsyncApiDiagnostic();
367+
AsyncApiDiagnostic fragmentDiagnostic = new AsyncApiDiagnostic
368+
{
369+
SpecificationVersion = diagnostic.SpecificationVersion,
370+
};
367371
IAsyncApiSerializable result = null;
368372
switch (reference.Reference.Type)
369373
{
370374
case ReferenceType.Schema:
371375
if (reference is AsyncApiJsonSchemaReference)
372376
{
373-
result = this.ReadFragment<AsyncApiJsonSchema>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
377+
result = this.ReadFragment<AsyncApiJsonSchema>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
374378
}
375379

376380
if (reference is AsyncApiAvroSchemaReference)
377381
{
378-
result = this.ReadFragment<AsyncApiAvroSchema>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
382+
result = this.ReadFragment<AsyncApiAvroSchema>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
379383
}
380384

381385
break;
382386

383387
case ReferenceType.Server:
384-
result = this.ReadFragment<AsyncApiServer>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
388+
result = this.ReadFragment<AsyncApiServer>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
385389
break;
386390
case ReferenceType.Channel:
387-
result = this.ReadFragment<AsyncApiChannel>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
391+
result = this.ReadFragment<AsyncApiChannel>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
388392
break;
389393
case ReferenceType.Message:
390-
result = this.ReadFragment<AsyncApiMessage>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
394+
result = this.ReadFragment<AsyncApiMessage>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
391395
break;
392396
case ReferenceType.SecurityScheme:
393-
result = this.ReadFragment<AsyncApiSecurityScheme>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
397+
result = this.ReadFragment<AsyncApiSecurityScheme>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
394398
break;
395399
case ReferenceType.Parameter:
396-
result = this.ReadFragment<AsyncApiParameter>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
400+
result = this.ReadFragment<AsyncApiParameter>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
397401
break;
398402
case ReferenceType.CorrelationId:
399-
result = this.ReadFragment<AsyncApiCorrelationId>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
403+
result = this.ReadFragment<AsyncApiCorrelationId>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
400404
break;
401405
case ReferenceType.OperationTrait:
402-
result = this.ReadFragment<AsyncApiOperationTrait>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
406+
result = this.ReadFragment<AsyncApiOperationTrait>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
403407
break;
404408
case ReferenceType.MessageTrait:
405-
result = this.ReadFragment<AsyncApiMessageTrait>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
409+
result = this.ReadFragment<AsyncApiMessageTrait>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
406410
break;
407411
case ReferenceType.ServerBindings:
408-
result = this.ReadFragment<AsyncApiBindings<IServerBinding>>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
412+
result = this.ReadFragment<AsyncApiBindings<IServerBinding>>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
409413
break;
410414
case ReferenceType.ChannelBindings:
411-
result = this.ReadFragment<AsyncApiBindings<IChannelBinding>>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
415+
result = this.ReadFragment<AsyncApiBindings<IChannelBinding>>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
412416
break;
413417
case ReferenceType.OperationBindings:
414-
result = this.ReadFragment<AsyncApiBindings<IOperationBinding>>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
418+
result = this.ReadFragment<AsyncApiBindings<IOperationBinding>>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
415419
break;
416420
case ReferenceType.MessageBindings:
417-
result = this.ReadFragment<AsyncApiBindings<IMessageBinding>>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
421+
result = this.ReadFragment<AsyncApiBindings<IMessageBinding>>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
418422
break;
419423
default:
420424
diagnostic.Errors.Add(new AsyncApiError(reference.Reference.Reference, "Could not resolve reference."));

src/ByteBard.AsyncAPI.Readers/V2/AsyncApiChannelDeserializer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ internal static partial class AsyncApiV2Deserializer
1111
private static readonly FixedFieldMap<AsyncApiChannel> ChannelFixedFields = new()
1212
{
1313
{ "description", (a, n) => { a.Description = n.GetScalarValue(); } },
14-
{ "servers", (a, n) => { a.Servers = n.CreateSimpleList(s => new AsyncApiServerReference("#/servers/" + s.GetScalarValue())); } },
14+
{ "servers", (a, n) => { a.Servers = n.CreateSimpleList(s => new AsyncApiServerReference(GetServerReferenceKey(s))); } },
1515
{ "subscribe", (a, n) => { /* happens after initial reading */ } },
1616
{ "publish", (a, n) => { /* happens after initial reading */ } },
1717
{ "parameters", (a, n) => { a.Parameters = n.CreateMap(LoadParameter); } },
1818
{ "bindings", (a, n) => { a.Bindings = LoadChannelBindings(n); } },
1919
};
2020

21+
private static string GetServerReferenceKey(ValueNode valueNode)
22+
{
23+
var stringValue = valueNode.GetScalarValue();
24+
return stringValue.StartsWith("#/servers/") ? stringValue : "#/servers/" + stringValue;
25+
}
26+
2127
private static readonly PatternFieldMap<AsyncApiChannel> ChannelPatternFields =
2228
new()
2329
{

src/ByteBard.AsyncAPI.Readers/V3/AsyncApiDocumentDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static partial class AsyncApiV3Deserializer
88
{
99
private static FixedFieldMap<AsyncApiDocument> asyncApiFixedFields = new()
1010
{
11-
{ "asyncapi", (a, n) => { a.Asyncapi = "3.0.0"; } },
11+
{ "asyncapi", (a, n) => { a.Asyncapi = "3.1.0"; } },
1212
{ "id", (a, n) => a.Id = n.GetScalarValue() },
1313
{ "info", (a, n) => a.Info = LoadInfo(n) },
1414
{ "servers", (a, n) => a.Servers = n.CreateMap(LoadServer) },

src/ByteBard.AsyncAPI/AsyncApiWorkspace.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,24 @@ public void RegisterComponents(AsyncApiDocument document)
204204
this.RegisterComponent(location + "/messages/" + message.Key, message.Value);
205205
}
206206
}
207+
208+
string serverBaseUri = "#/servers/";
209+
foreach (var server in document.Servers)
210+
{
211+
var registerableServerValue = server.Value;
212+
if (server.Value is IAsyncApiReferenceable serverReference)
213+
{
214+
if (serverReference.Reference.IsExternal)
215+
{
216+
continue;
217+
}
218+
219+
registerableServerValue = this.ResolveReference<AsyncApiServer>(serverReference.Reference);
220+
}
221+
222+
location = serverBaseUri + server.Key;
223+
this.RegisterComponent(location, registerableServerValue);
224+
}
207225
}
208226

209227
public bool RegisterComponent<T>(string location, T component)

0 commit comments

Comments
 (0)