Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ internal static partial class AsyncApiV2Deserializer
{
private static FixedFieldMap<AsyncApiMessageTrait> messageTraitFixedFields = new()
{
{ "headers", (a, n) => { a.Headers = new AsyncApiMultiFormatSchema { Schema = AsyncApiJsonSchemaDeserializer.LoadSchema(n) }; } },
{ "headers", (a, n) => { (a.Headers ??= new AsyncApiMultiFormatSchema()).Schema = AsyncApiJsonSchemaDeserializer.LoadSchema(n); } },
{ "correlationId", (a, n) => { a.CorrelationId = LoadCorrelationId(n); } },
{ "schemaFormat", (a, n) => { a.Headers.SchemaFormat = n.GetScalarValue(); } },
{ "schemaFormat", (a, n) => { (a.Headers ??= new AsyncApiMultiFormatSchema()).SchemaFormat = n.GetScalarValue(); } },
{ "contentType", (a, n) => { a.ContentType = n.GetScalarValue(); } },
{ "name", (a, n) => { a.Name = n.GetScalarValue(); } },
{ "title", (a, n) => { a.Title = n.GetScalarValue(); } },
Expand Down
6 changes: 3 additions & 3 deletions src/ByteBard.AsyncAPI/Models/AsyncApiMessageTrait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public virtual void SerializeV2(IAsyncApiWriter writer)

writer.WriteStartObject();
//writer.WriteOptionalProperty(AsyncApiConstants.MessageId, this.MessageId);
writer.WriteOptionalObject(AsyncApiConstants.Headers, this.Headers, (w, h) => h.Schema.SerializeV2(w));
writer.WriteOptionalObject(AsyncApiConstants.Headers, this.Headers?.Schema, (w, h) => h.SerializeV2(w));
writer.WriteOptionalObject(AsyncApiConstants.CorrelationId, this.CorrelationId, (w, c) => c.SerializeV2(w));
writer.WriteOptionalProperty(AsyncApiConstants.SchemaFormat, this.Headers.SchemaFormat);
writer.WriteOptionalProperty(AsyncApiConstants.SchemaFormat, this.Headers?.SchemaFormat);
writer.WriteOptionalProperty(AsyncApiConstants.ContentType, this.ContentType);
writer.WriteOptionalProperty(AsyncApiConstants.Name, this.Name);
writer.WriteOptionalProperty(AsyncApiConstants.Title, this.Title);
Expand All @@ -100,7 +100,7 @@ public virtual void SerializeV3(IAsyncApiWriter writer)
}

writer.WriteStartObject();
writer.WriteOptionalObject(AsyncApiConstants.Headers, this.Headers, (w, h) => h.SerializeV3(w));
writer.WriteOptionalObject(AsyncApiConstants.Headers, this.Headers?.Schema == null ? null : this.Headers, (w, h) => h.SerializeV3(w));
writer.WriteOptionalObject(AsyncApiConstants.CorrelationId, this.CorrelationId, (w, c) => c.SerializeV3(w));
writer.WriteOptionalProperty(AsyncApiConstants.ContentType, this.ContentType);
writer.WriteOptionalProperty(AsyncApiConstants.Name, this.Name);
Expand Down
103 changes: 103 additions & 0 deletions test/ByteBard.AsyncAPI.Tests/Models/AsyncApiMessage_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,109 @@ public void V2_AsyncApiMessage_WithUnsupportedSchemaFormat_DeserializesWithError
diagnostic.Errors.First().Message.Should().StartWith("Could not deserialize Payload. Supported formats are");
}

[Test]
public void V2_AsyncApiMessageTrait_WithoutHeaders_Serializes()
{
var expected =
"""
name: TraitName
""";

var trait = new AsyncApiMessageTrait { Name = "TraitName" };

var actual = trait.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0);

actual.Should().BePlatformAgnosticEquivalentTo(expected);
}

[Test]
public void V2_AsyncApiMessageTrait_WithSchemaFormatBeforeHeaders_Deserializes()
{
var input =
"""
schemaFormat: application/vnd.aai.asyncapi+json;version=2.6.0
headers:
type: object
""";

var trait = new AsyncApiStringReader().ReadFragment<AsyncApiMessageTrait>(input, AsyncApiVersion.AsyncApi2_0, out var diagnostic);

diagnostic.Errors.Should().BeEmpty();
trait.Headers.SchemaFormat.Should().Be("application/vnd.aai.asyncapi+json;version=2.6.0");
trait.Headers.Schema.Should().BeOfType<AsyncApiJsonSchema>();
}

[Test]
public void V2_AsyncApiMessageTrait_WithSchemaFormatAndNoHeaders_Deserializes()
{
var input =
"""
schemaFormat: application/vnd.aai.asyncapi+json;version=2.6.0
""";

var trait = new AsyncApiStringReader().ReadFragment<AsyncApiMessageTrait>(input, AsyncApiVersion.AsyncApi2_0, out var diagnostic);

diagnostic.Errors.Should().BeEmpty();
trait.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0).Should().BePlatformAgnosticEquivalentTo(input);
}

[Test]
public void V3_AsyncApiMessageTrait_WithoutHeaders_Serializes()
{
var expected =
"""
name: TraitName
""";

var trait = new AsyncApiMessageTrait { Name = "TraitName" };

var actual = trait.SerializeAsYaml(AsyncApiVersion.AsyncApi3_0);

actual.Should().BePlatformAgnosticEquivalentTo(expected);
}

[Test]
public void V3_AsyncApiMessageTrait_WithHeaders_Reserializes()
{
var input =
"""
headers:
type: object
name: TraitName
""";

// By design, 3.x schemas are always read as multi-format schemas with the default schema format filled in, so the headers come back in the explicit multi-format form.
var expected =
"""
headers:
schemaFormat: application/vnd.aai.asyncapi+json;version=3.0.0
Comment thread
VisualBean marked this conversation as resolved.
schema:
type: object
name: TraitName
""";

var trait = new AsyncApiStringReader().ReadFragment<AsyncApiMessageTrait>(input, AsyncApiVersion.AsyncApi3_0, out var diagnostic);

diagnostic.Errors.Should().BeEmpty();
trait.Headers.Schema.Should().BeOfType<AsyncApiJsonSchema>();
trait.SerializeAsYaml(AsyncApiVersion.AsyncApi3_0).Should().BePlatformAgnosticEquivalentTo(expected);
}

[Test]
public void V2_AsyncApiMessageTrait_WithSchemaFormatAndNoHeaders_UpgradesToV3WithoutHeaders()
{
var input =
"""
schemaFormat: application/vnd.aai.asyncapi+json;version=2.6.0
name: TraitName
""";

var trait = new AsyncApiStringReader().ReadFragment<AsyncApiMessageTrait>(input, AsyncApiVersion.AsyncApi2_0, out var diagnostic);

diagnostic.Errors.Should().BeEmpty();
trait.SerializeAsYaml(AsyncApiVersion.AsyncApi3_0).Should().BePlatformAgnosticEquivalentTo("name: TraitName");
}

[Test]
public void V2_AsyncApiMessage_WithNoSchemaFormat_DoesNotSerializeSchemaFormat()
{
Expand Down
Loading