From 19602c9aea492bf8efe9355b0287b36f39d212d9 Mon Sep 17 00:00:00 2001 From: Alex Wichmann Date: Tue, 18 Jun 2024 18:13:24 +0200 Subject: [PATCH] fix: add missing avro payload resolution (#186) --- .../AsyncApiExternalReferenceResolver.cs | 12 ++- .../Models/AsyncApiReference_Should.cs | 73 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/LEGO.AsyncAPI.Readers/AsyncApiExternalReferenceResolver.cs b/src/LEGO.AsyncAPI.Readers/AsyncApiExternalReferenceResolver.cs index 2c111773..edfb1a79 100644 --- a/src/LEGO.AsyncAPI.Readers/AsyncApiExternalReferenceResolver.cs +++ b/src/LEGO.AsyncAPI.Readers/AsyncApiExternalReferenceResolver.cs @@ -90,10 +90,18 @@ public override void Visit(AsyncApiOperation operation) public override void Visit(AsyncApiMessage message) { this.ResolveObject(message.Headers, r => message.Headers = r); - if (message.Payload is AsyncApiJsonSchemaPayload) + switch (message.Payload) { - this.ResolveObject(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = r); + case AsyncApiJsonSchemaPayload json: + this.ResolveObject(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = r); + break; + case AsyncApiAvroSchemaPayload avro: + this.ResolveObject(message.Payload as AsyncApiAvroSchemaPayload, r => message.Payload = r); + break; + default: + break; } + this.ResolveList(message.Traits); this.ResolveObject(message.CorrelationId, r => message.CorrelationId = r); this.ResolveObject(message.Bindings, r => message.Bindings = r); diff --git a/test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs b/test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs index 41972784..16d58b4c 100644 --- a/test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs +++ b/test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs @@ -4,6 +4,7 @@ namespace LEGO.AsyncAPI.Tests { using System.Linq; using FluentAssertions; + using LEGO.AsyncAPI.Extensions; using LEGO.AsyncAPI.Models; using LEGO.AsyncAPI.Readers; using NUnit.Framework; @@ -313,8 +314,80 @@ public void AsyncApiReference_WithExternalResourcesInterface_DeserializesCorrect var payload = message.Payload.As(); payload.Properties.Count.Should().Be(3); } + + [Test] + public void AvroReference_WithExternalResourcesInterface_DeserializesCorrectly() + { + var yaml = """ + asyncapi: 2.3.0 + info: + title: test + version: 1.0.0 + channels: + workspace: + publish: + message: + schemaFormat: 'application/vnd.apache.avro+yaml;version=1.9.0' + payload: + $ref: 'path/to/user-create.avsc/#UserCreate' + """; + var settings = new AsyncApiReaderSettings + { + ReferenceResolution = ReferenceResolutionSetting.ResolveAllReferences, + ExternalReferenceReader = new MockExternalAvroReferenceReader(), + }; + var reader = new AsyncApiStringReader(settings); + var doc = reader.Read(yaml, out var diagnostic); + var payload = doc.Channels["workspace"].Publish.Message.First().Payload; + payload.Should().BeAssignableTo(typeof(AsyncApiAvroSchemaPayload)); + var avro = payload as AsyncApiAvroSchemaPayload; + avro.TryGetAs(out var record); + record.Name.Should().Be("SomeEvent"); + } } + public class MockExternalAvroReferenceReader : IAsyncApiExternalReferenceReader + { + public string Load(string reference) + { + return + """ + { + "type": "record", + "name": "SomeEvent", + "namespace": "my.namspace.for.event", + "fields": [ + { + "name": "countryCode", + "type": "string", + "doc": "Country of the partner, (e.g. DE)" + }, + { + "name": "occurredOn", + "type": "string", + "doc": "Timestamp of when action occurred." + }, + { + "name": "partnerId", + "type": "string", + "doc": "Id of the partner" + }, + { + "name": "platformSource", + "type": "string", + "doc": "Platform source" + } + ], + "example": { + "occurredOn": "2023-11-03T09:56.582+00:00", + "partnerId": "1", + "platformSource": "Brecht", + "countryCode": "DE" + } + } + """; + } + } public class MockExternalReferenceReader : IAsyncApiExternalReferenceReader { public string Load(string reference)