Skip to content

Commit 33d3110

Browse files
committed
Enables version-aware fragment parsing
Ensures AsyncAPI document fragments are parsed using the document's specified AsyncAPI version. Previously, fragment parsing was hardcoded to AsyncAPI 2.0. Also, registers AsyncAPI servers within the workspace, correctly resolving internal references, to improve component management.
1 parent 7cea38b commit 33d3110

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

src/ByteBard.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs

Lines changed: 15 additions & 14 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,
@@ -370,51 +371,51 @@ private IAsyncApiSerializable ResolveStreamReference(Stream stream, IAsyncApiRef
370371
case ReferenceType.Schema:
371372
if (reference is AsyncApiJsonSchemaReference)
372373
{
373-
result = this.ReadFragment<AsyncApiJsonSchema>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
374+
result = this.ReadFragment<AsyncApiJsonSchema>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
374375
}
375376

376377
if (reference is AsyncApiAvroSchemaReference)
377378
{
378-
result = this.ReadFragment<AsyncApiAvroSchema>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
379+
result = this.ReadFragment<AsyncApiAvroSchema>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
379380
}
380381

381382
break;
382383

383384
case ReferenceType.Server:
384-
result = this.ReadFragment<AsyncApiServer>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
385+
result = this.ReadFragment<AsyncApiServer>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
385386
break;
386387
case ReferenceType.Channel:
387-
result = this.ReadFragment<AsyncApiChannel>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
388+
result = this.ReadFragment<AsyncApiChannel>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
388389
break;
389390
case ReferenceType.Message:
390-
result = this.ReadFragment<AsyncApiMessage>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
391+
result = this.ReadFragment<AsyncApiMessage>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
391392
break;
392393
case ReferenceType.SecurityScheme:
393-
result = this.ReadFragment<AsyncApiSecurityScheme>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
394+
result = this.ReadFragment<AsyncApiSecurityScheme>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
394395
break;
395396
case ReferenceType.Parameter:
396-
result = this.ReadFragment<AsyncApiParameter>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
397+
result = this.ReadFragment<AsyncApiParameter>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
397398
break;
398399
case ReferenceType.CorrelationId:
399-
result = this.ReadFragment<AsyncApiCorrelationId>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
400+
result = this.ReadFragment<AsyncApiCorrelationId>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
400401
break;
401402
case ReferenceType.OperationTrait:
402-
result = this.ReadFragment<AsyncApiOperationTrait>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
403+
result = this.ReadFragment<AsyncApiOperationTrait>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
403404
break;
404405
case ReferenceType.MessageTrait:
405-
result = this.ReadFragment<AsyncApiMessageTrait>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
406+
result = this.ReadFragment<AsyncApiMessageTrait>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
406407
break;
407408
case ReferenceType.ServerBindings:
408-
result = this.ReadFragment<AsyncApiBindings<IServerBinding>>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
409+
result = this.ReadFragment<AsyncApiBindings<IServerBinding>>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
409410
break;
410411
case ReferenceType.ChannelBindings:
411-
result = this.ReadFragment<AsyncApiBindings<IChannelBinding>>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
412+
result = this.ReadFragment<AsyncApiBindings<IChannelBinding>>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
412413
break;
413414
case ReferenceType.OperationBindings:
414-
result = this.ReadFragment<AsyncApiBindings<IOperationBinding>>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
415+
result = this.ReadFragment<AsyncApiBindings<IOperationBinding>>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
415416
break;
416417
case ReferenceType.MessageBindings:
417-
result = this.ReadFragment<AsyncApiBindings<IMessageBinding>>(json, AsyncApiVersion.AsyncApi2_0, out fragmentDiagnostic);
418+
result = this.ReadFragment<AsyncApiBindings<IMessageBinding>>(json, diagnostic.SpecificationVersion, out fragmentDiagnostic);
418419
break;
419420
default:
420421
diagnostic.Errors.Add(new AsyncApiError(reference.Reference.Reference, "Could not resolve reference."));

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 reference)
213+
{
214+
if (reference.Reference.IsExternal)
215+
{
216+
continue;
217+
}
218+
219+
registerableServerValue = this.ResolveReference<AsyncApiServer>(reference.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)