Skip to content

Commit

Permalink
fix: JsonSchemaPayload reference resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed Aug 12, 2024
1 parent 7083b0c commit a9d501d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ public override void Visit(AsyncApiMessage message)
switch (message.Payload)
{
case AsyncApiJsonSchemaPayload json:
this.ResolveObject(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = r);
this.ResolveObject<AsyncApiSchema>(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = new AsyncApiJsonSchemaPayload(r));
break;
case AsyncApiAvroSchemaPayload avro:
// ToFix: this might not resolve correctly.
this.ResolveObject(message.Payload as AsyncApiAvroSchemaPayload, r => message.Payload = r);
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI/Services/AsyncApiReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public override void Visit(AsyncApiMessage message)
// #ToFix Resolve references correctly
if (message.Payload is AsyncApiJsonSchemaPayload)
{
this.ResolveObject(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = r);
this.ResolveObject<AsyncApiSchema>(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = new AsyncApiJsonSchemaPayload(r));
}

this.ResolveList(message.Traits);
Expand Down
102 changes: 102 additions & 0 deletions test/LEGO.AsyncAPI.Tests/AsyncApiDocumentV2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,108 @@ public void SerializeV2_WithFullSpec_Serializes()
.BePlatformAgnosticEquivalentTo(expected);
}

[Test]
public void Read_WithAvroSchemaPayload_NoErrors()
{
// Arrange
var yaml =
"""
asyncapi: '2.6.0'
info:
title: schema-validation-test
version: '1.0.0'
description: Async API for schema validation tests
contact:
name: Test
url: https://test.test/
channels:
schema-validation-topic:
description: A topic to publish messages for testing Pulsar schema validation
publish:
message:
$ref: '#/components/messages/schema-validation-message'
components:
messages:
schema-validation-message:
name: schema-validation-message
title: Message for schema validation testing that is a json object
summary: A test message is used for testing Pulsar schema validation
payload:
type: record
name: UserSignedUp
namespace: esp
doc: ESP Schema validation test
fields:
- name: userId
type: int
- name: userEmail
type: string
schemaFormat: 'application/vnd.apache.avro;version=1.9.0'
""";

// Act
var result = new AsyncApiStringReader().Read(yaml, out var diagnostics);

// Assert
diagnostics.Errors.Should().HaveCount(0);
result.Channels.First().Value.Publish.Message.First().Payload.As<AsyncApiAvroSchemaPayload>().TryGetAs<AvroRecord>(out var record).Should().BeTrue();
record.Name.Should().Be("UserSignedUp");
}

[Test]
public void Read_WithJsonSchemaReference_NoErrors()
{
// Arrange
var yaml =
"""
asyncapi: '2.6.0'
info:
title: schema-validation-test
version: '1.0.0'
description: Async API for schema validation tests
contact:
name: Test
url: https://test.test/
channels:
schema-validation-topic:
description: A topic to publish messages for testing Pulsar schema validation
publish:
message:
$ref: '#/components/messages/schema-validation-message'
subscribe:
message:
$ref: '#/components/messages/schema-validation-message'
components:
schemas:
schema-validation-message-payload:
type: object
properties:
content:
type: string
description: Content of the message
messages:
schema-validation-message:
name: schema-validation-message
title: Message for schema validation testing that is a json object
summary: A test message is used for testing Pulsar schema validation
payload:
$ref: '#/components/schemas/schema-validation-message-payload'
contentType: application/json
""";

// Act
var result = new AsyncApiStringReader().Read(yaml, out var diagnostics);

// Assert
diagnostics.Errors.Should().HaveCount(0);
result.Channels.First().Value.Publish.Message.First().Title.Should().Be("Message for schema validation testing that is a json object");
result.Channels.First().Value.Publish.Message.First().Payload.As<AsyncApiJsonSchemaPayload>().Properties.Should().HaveCount(1);
}

[Test]
public void Serialize_WithBindingReferences_SerializesDeserializes()
{
Expand Down

0 comments on commit a9d501d

Please sign in to comment.