Skip to content

Commit 0f46359

Browse files
authored
fix(readers): set fixed payload schemaformats to json schema types (LEGO#84)
1 parent ef428b1 commit 0f46359

2 files changed

Lines changed: 149 additions & 4 deletions

File tree

src/LEGO.AsyncAPI.Readers/V2/AsyncApiMessageDeserializer.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace LEGO.AsyncAPI.Readers
44
{
5+
using LEGO.AsyncAPI.Exceptions;
56
using LEGO.AsyncAPI.Extensions;
67
using LEGO.AsyncAPI.Models;
78
using LEGO.AsyncAPI.Readers.ParseNodes;
9+
using System;
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
814
/// <summary>
915
/// Class containing logic to deserialize AsyncApi document into
1016
/// runtime AsyncApi object model.
@@ -26,7 +32,7 @@ internal static partial class AsyncApiV2Deserializer
2632
"correlationId", (a, n) => { a.CorrelationId = LoadCorrelationId(n); }
2733
},
2834
{
29-
"schemaFormat", (a, n) => { a.SchemaFormat = n.GetScalarValue(); }
35+
"schemaFormat", (a, n) => { a.SchemaFormat = LoadSchemaFormat(n.GetScalarValue()); }
3036
},
3137
{
3238
"contentType", (a, n) => { a.ContentType = n.GetScalarValue(); }
@@ -60,6 +66,25 @@ internal static partial class AsyncApiV2Deserializer
6066
},
6167
};
6268

69+
static readonly IEnumerable<string> SupportedSchemaFormats = new List<string>
70+
{
71+
"application/vnd.aai.asyncapi+json",
72+
"application/vnd.aai.asyncapi+yaml",
73+
"application/vnd.aai.asyncapi",
74+
"application/schema+json;version=draft-07",
75+
"application/schema+yaml;version=draft-07",
76+
};
77+
78+
private static string LoadSchemaFormat(string schemaFormat)
79+
{
80+
if (!SupportedSchemaFormats.Where(s => schemaFormat.StartsWith(s)).Any())
81+
{
82+
throw new AsyncApiException($"'{schemaFormat}' is not a supported format. Supported formats are {string.Join(", ", SupportedSchemaFormats)}");
83+
}
84+
85+
return schemaFormat;
86+
}
87+
6388
private static readonly PatternFieldMap<AsyncApiMessage> messagePatternFields = new()
6489
{
6590
{ s => s.StartsWith("x-"), (a, p, n) => a.AddExtension(p, LoadExtension(p, n)) },

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

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using FluentAssertions;
67
using LEGO.AsyncAPI.Models;
78
using LEGO.AsyncAPI.Models.Any;
@@ -13,7 +14,128 @@
1314

1415
internal class AsyncApiMessage_Should
1516
{
16-
[Test]
17+
[Test]
18+
public void AsyncApiMessage_WithNoSchemaFormat_DeserializesToDefault()
19+
{
20+
// Arrange
21+
var expected =
22+
@"payload:
23+
properties:
24+
propertyA:
25+
type:
26+
- string
27+
- 'null'";
28+
29+
// Act
30+
var message = new AsyncApiStringReader().ReadFragment<AsyncApiMessage>(expected, AsyncApiVersion.AsyncApi2_0, out var diagnostic);
31+
32+
// Assert
33+
diagnostic.Errors.Should().BeEmpty();
34+
message.SchemaFormat.Should().BeNull();
35+
}
36+
37+
[Test]
38+
public void AsyncApiMessage_WithUnsupportedSchemaFormat_DeserializesWithError()
39+
{
40+
// Arrange
41+
var expected =
42+
@"payload:
43+
properties:
44+
propertyA:
45+
type:
46+
- string
47+
- 'null'
48+
schemaFormat: application/vnd.apache.avro;version=1.9.0";
49+
50+
// Act
51+
new AsyncApiStringReader().ReadFragment<AsyncApiMessage>(expected, AsyncApiVersion.AsyncApi2_0, out var diagnostic);
52+
53+
// Assert
54+
diagnostic.Errors.Should().HaveCount(1);
55+
diagnostic.Errors.First().Message.Should().StartWith("'application/vnd.apache.avro;version=1.9.0' is not a supported format");
56+
}
57+
58+
[Test]
59+
public void AsyncApiMessage_WithNoSchemaFormat_DoesNotSerializeSchemaFormat()
60+
{
61+
// Arrange
62+
var expected =
63+
@"payload:
64+
properties:
65+
propertyA:
66+
type:
67+
- string
68+
- 'null'";
69+
70+
var message = new AsyncApiMessage();
71+
message.Payload = new AsyncApiSchema()
72+
{
73+
Properties = new Dictionary<string, AsyncApiSchema>()
74+
{
75+
{
76+
"propertyA", new AsyncApiSchema()
77+
{
78+
Type = new List<SchemaType> { SchemaType.String, SchemaType.Null },
79+
}
80+
},
81+
},
82+
};
83+
84+
// Act
85+
var actual = message.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0);
86+
87+
actual = actual.MakeLineBreaksEnvironmentNeutral();
88+
expected = expected.MakeLineBreaksEnvironmentNeutral();
89+
90+
var deserializedMessage = new AsyncApiStringReader().ReadFragment<AsyncApiMessage>(expected, AsyncApiVersion.AsyncApi2_0, out _);
91+
92+
// Assert
93+
Assert.AreEqual(actual, expected);
94+
message.Should().BeEquivalentTo(deserializedMessage);
95+
}
96+
97+
[Test]
98+
public void AsyncApiMessage_WithSchemaFormat_Serializes()
99+
{
100+
// Arrange
101+
var expected =
102+
@"payload:
103+
properties:
104+
propertyA:
105+
type:
106+
- string
107+
- 'null'
108+
schemaFormat: application/vnd.aai.asyncapi+json;version=2.5.0";
109+
110+
var message = new AsyncApiMessage();
111+
message.SchemaFormat = "application/vnd.aai.asyncapi+json;version=2.5.0";
112+
message.Payload = new AsyncApiSchema()
113+
{
114+
Properties = new Dictionary<string, AsyncApiSchema>()
115+
{
116+
{
117+
"propertyA", new AsyncApiSchema()
118+
{
119+
Type = new List<SchemaType> { SchemaType.String, SchemaType.Null },
120+
}
121+
},
122+
},
123+
};
124+
125+
// Act
126+
var actual = message.SerializeAsYaml(AsyncApiVersion.AsyncApi2_0);
127+
128+
actual = actual.MakeLineBreaksEnvironmentNeutral();
129+
expected = expected.MakeLineBreaksEnvironmentNeutral();
130+
131+
var deserializedMessage = new AsyncApiStringReader().ReadFragment<AsyncApiMessage>(expected, AsyncApiVersion.AsyncApi2_0, out _);
132+
133+
// Assert
134+
Assert.AreEqual(actual, expected);
135+
message.Should().BeEquivalentTo(deserializedMessage);
136+
}
137+
138+
[Test]
17139
public void AsyncApiMessage_WithFilledObject_Serializes()
18140
{
19141
var expected =
@@ -33,7 +155,6 @@ public void AsyncApiMessage_WithFilledObject_Serializes()
33155
description: CorrelationDescription
34156
location: Header
35157
x-extension-a: a
36-
schemaFormat: MessageSchemaFormat
37158
contentType: MessageContentType
38159
name: MessageName
39160
title: MessageTitle
@@ -133,7 +254,6 @@ public void AsyncApiMessage_WithFilledObject_Serializes()
133254
{ "x-extension-a", new AsyncApiString("a") },
134255
},
135256
},
136-
SchemaFormat = "MessageSchemaFormat",
137257
ContentType = "MessageContentType",
138258
Name = "MessageName",
139259
Title = "MessageTitle",

0 commit comments

Comments
 (0)