Skip to content

Commit

Permalink
fix: add missing avro payload resolution (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean authored Jun 18, 2024
1 parent f870610 commit 19602c9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/LEGO.AsyncAPI.Readers/AsyncApiExternalReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
73 changes: 73 additions & 0 deletions test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -313,8 +314,80 @@ public void AsyncApiReference_WithExternalResourcesInterface_DeserializesCorrect
var payload = message.Payload.As<AsyncApiJsonSchemaPayload>();
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<AvroRecord>(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)
Expand Down

0 comments on commit 19602c9

Please sign in to comment.