Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing avro payload resolution #186

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading