Skip to content

Commit 19602c9

Browse files
authored
fix: add missing avro payload resolution (#186)
1 parent f870610 commit 19602c9

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/LEGO.AsyncAPI.Readers/AsyncApiExternalReferenceResolver.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,18 @@ public override void Visit(AsyncApiOperation operation)
9090
public override void Visit(AsyncApiMessage message)
9191
{
9292
this.ResolveObject(message.Headers, r => message.Headers = r);
93-
if (message.Payload is AsyncApiJsonSchemaPayload)
93+
switch (message.Payload)
9494
{
95-
this.ResolveObject(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = r);
95+
case AsyncApiJsonSchemaPayload json:
96+
this.ResolveObject(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = r);
97+
break;
98+
case AsyncApiAvroSchemaPayload avro:
99+
this.ResolveObject(message.Payload as AsyncApiAvroSchemaPayload, r => message.Payload = r);
100+
break;
101+
default:
102+
break;
96103
}
104+
97105
this.ResolveList(message.Traits);
98106
this.ResolveObject(message.CorrelationId, r => message.CorrelationId = r);
99107
this.ResolveObject(message.Bindings, r => message.Bindings = r);

test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace LEGO.AsyncAPI.Tests
44
{
55
using System.Linq;
66
using FluentAssertions;
7+
using LEGO.AsyncAPI.Extensions;
78
using LEGO.AsyncAPI.Models;
89
using LEGO.AsyncAPI.Readers;
910
using NUnit.Framework;
@@ -313,8 +314,80 @@ public void AsyncApiReference_WithExternalResourcesInterface_DeserializesCorrect
313314
var payload = message.Payload.As<AsyncApiJsonSchemaPayload>();
314315
payload.Properties.Count.Should().Be(3);
315316
}
317+
318+
[Test]
319+
public void AvroReference_WithExternalResourcesInterface_DeserializesCorrectly()
320+
{
321+
var yaml = """
322+
asyncapi: 2.3.0
323+
info:
324+
title: test
325+
version: 1.0.0
326+
channels:
327+
workspace:
328+
publish:
329+
message:
330+
schemaFormat: 'application/vnd.apache.avro+yaml;version=1.9.0'
331+
payload:
332+
$ref: 'path/to/user-create.avsc/#UserCreate'
333+
""";
334+
var settings = new AsyncApiReaderSettings
335+
{
336+
ReferenceResolution = ReferenceResolutionSetting.ResolveAllReferences,
337+
ExternalReferenceReader = new MockExternalAvroReferenceReader(),
338+
};
339+
var reader = new AsyncApiStringReader(settings);
340+
var doc = reader.Read(yaml, out var diagnostic);
341+
var payload = doc.Channels["workspace"].Publish.Message.First().Payload;
342+
payload.Should().BeAssignableTo(typeof(AsyncApiAvroSchemaPayload));
343+
var avro = payload as AsyncApiAvroSchemaPayload;
344+
avro.TryGetAs<AvroRecord>(out var record);
345+
record.Name.Should().Be("SomeEvent");
346+
}
316347
}
317348

349+
public class MockExternalAvroReferenceReader : IAsyncApiExternalReferenceReader
350+
{
351+
public string Load(string reference)
352+
{
353+
return
354+
"""
355+
{
356+
"type": "record",
357+
"name": "SomeEvent",
358+
"namespace": "my.namspace.for.event",
359+
"fields": [
360+
{
361+
"name": "countryCode",
362+
"type": "string",
363+
"doc": "Country of the partner, (e.g. DE)"
364+
},
365+
{
366+
"name": "occurredOn",
367+
"type": "string",
368+
"doc": "Timestamp of when action occurred."
369+
},
370+
{
371+
"name": "partnerId",
372+
"type": "string",
373+
"doc": "Id of the partner"
374+
},
375+
{
376+
"name": "platformSource",
377+
"type": "string",
378+
"doc": "Platform source"
379+
}
380+
],
381+
"example": {
382+
"occurredOn": "2023-11-03T09:56.582+00:00",
383+
"partnerId": "1",
384+
"platformSource": "Brecht",
385+
"countryCode": "DE"
386+
}
387+
}
388+
""";
389+
}
390+
}
318391
public class MockExternalReferenceReader : IAsyncApiExternalReferenceReader
319392
{
320393
public string Load(string reference)

0 commit comments

Comments
 (0)