Skip to content

Commit e9de510

Browse files
committed
More custom schema
1 parent 120dd41 commit e9de510

5 files changed

Lines changed: 148 additions & 6 deletions

File tree

src/ByteBard.AsyncAPI.Readers/Schemas/AvroSchemaParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace ByteBard.AsyncAPI.Readers;
44
using Models.Interfaces;
55
using ParseNodes;
66

7-
public class AvroSchemaParser : ISchemaParser
7+
public class AvroSchemaParser : IAsyncApiSchemaParser
88
{
99
public IAsyncApiSchema LoadSchema(ParseNode parseNode)
1010
{

src/ByteBard.AsyncAPI.Readers/Schemas/ISchemaParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace ByteBard.AsyncAPI.Readers;
44
using Models.Interfaces;
55
using ParseNodes;
66

7-
public interface ISchemaParser
7+
public interface IAsyncApiSchemaParser
88
{
99
IAsyncApiSchema LoadSchema(ParseNode node);
1010
IEnumerable<string> SupportedFormats { get; }

src/ByteBard.AsyncAPI.Readers/Schemas/JsonSchemaParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace ByteBard.AsyncAPI.Readers;
44
using Models.Interfaces;
55
using ParseNodes;
66

7-
public class JsonSchemaParser: ISchemaParser
7+
public class JsonSchemaParser: IAsyncApiSchemaParser
88
{
99
public IAsyncApiSchema LoadSchema(ParseNode node)
1010
{

src/ByteBard.AsyncAPI.Readers/Schemas/SchemaParserRegistry.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public SchemaParserRegistry()
1111
this.RegisterParser(new AvroSchemaParser());
1212
}
1313

14-
private readonly Dictionary<string, ISchemaParser> parsers = new();
14+
private readonly Dictionary<string, IAsyncApiSchemaParser> parsers = new();
1515
private readonly Dictionary<string, string> formatToPrefix = new();
1616

17-
public void RegisterParser(ISchemaParser deserializer)
17+
public void RegisterParser(IAsyncApiSchemaParser deserializer)
1818
{
1919
foreach (var format in deserializer.SupportedFormats)
2020
{
@@ -23,7 +23,7 @@ public void RegisterParser(ISchemaParser deserializer)
2323
}
2424
}
2525

26-
public ISchemaParser GetParser(string format)
26+
public IAsyncApiSchemaParser GetParser(string format)
2727
{
2828
if (string.IsNullOrEmpty(format))
2929
{
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
namespace ByteBard.AsyncAPI.Tests.Models;
2+
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using AsyncAPI.Models;
6+
using AsyncAPI.Models.Interfaces;
7+
using AsyncAPI.Writers;
8+
using FluentAssertions;
9+
using NUnit.Framework;
10+
using Readers;
11+
using Readers.ParseNodes;
12+
13+
public class CustomSchema_Should
14+
{
15+
public enum MySchemaType
16+
{
17+
One,
18+
Two,
19+
}
20+
21+
public class MySchema : IAsyncApiSchema
22+
{
23+
public MySchemaType Type { get; set; }
24+
25+
public int Length { get; set; }
26+
27+
public string Description { get; set; }
28+
29+
public IEnumerable<MySchema> Children { get; set; }
30+
31+
public void SerializeV2(IAsyncApiWriter writer)
32+
{
33+
// not being tested
34+
throw new System.NotImplementedException();
35+
}
36+
37+
public void SerializeV3(IAsyncApiWriter writer)
38+
{
39+
// not being tested
40+
throw new System.NotImplementedException();
41+
}
42+
}
43+
44+
public class MySchemaParser : IAsyncApiSchemaParser
45+
{
46+
private readonly static FixedFieldMap<MySchema> SchemaFixedFields = new()
47+
{
48+
{
49+
"type",
50+
(schema, node) => { schema.Type = node.GetScalarValue().GetEnumFromDisplayName<MySchemaType>(); }
51+
},
52+
{
53+
"length", (schema, node) => { schema.Length = node.GetIntegerValue(); }
54+
},
55+
{
56+
"description", (schema, node) => { schema.Description = node.GetScalarValueOrDefault("No description"); }
57+
},
58+
{
59+
"children", (schema, node) => { schema.Children = node.CreateList(Parse); }
60+
},
61+
};
62+
63+
private static MySchema Parse(ParseNode node)
64+
{
65+
var mapNode = node.CheckMapNode("arbitrary string");
66+
67+
var schema = new MySchema();
68+
69+
// map each propery against the fixedFieldMap
70+
foreach (var property in mapNode)
71+
{
72+
property.ParseField(schema, SchemaFixedFields, null);
73+
}
74+
75+
return schema;
76+
}
77+
78+
public IAsyncApiSchema LoadSchema(ParseNode node)
79+
{
80+
return Parse(node);
81+
}
82+
83+
public IEnumerable<string> SupportedFormats => new string[] { "application/myschema+json" };
84+
}
85+
86+
[Test]
87+
public void Document_WithCustomSchema_Deserializes()
88+
{
89+
// Arrange
90+
var settings = new AsyncApiReaderSettings();
91+
settings.SchemaParserRegistry.RegisterParser(new MySchemaParser());
92+
93+
var reader = new AsyncApiStringReader(settings);
94+
95+
var input =
96+
"""
97+
asyncapi: 3.0.0
98+
info:
99+
title: test
100+
version: 1.0.0
101+
102+
channels:
103+
workspace:
104+
messages:
105+
workspaceEvent:
106+
$ref: '#/components/messages/WorkspaceEventPayload'
107+
components:
108+
messages:
109+
WorkspaceEventPayload:
110+
contentType: text/plain
111+
payload:
112+
schemaFormat: application/myschema+json
113+
schema:
114+
type: one
115+
description: a test description
116+
length: 1
117+
children:
118+
- type: two
119+
length: 2
120+
121+
""";
122+
123+
// Act
124+
var document = reader.Read(input, out var diagnostic);
125+
126+
// Assert
127+
diagnostic.Errors.Should().HaveCount(0);
128+
129+
var schema = document.Components.Messages.Values.First().Payload;
130+
schema.SchemaFormat.Should().Be("application/myschema+json");
131+
132+
var myschema = schema.Schema.As<MySchema>();
133+
myschema.Description.Should().Be("a test description");
134+
myschema.Length.Should().Be(1);
135+
myschema.Type.Should().Be(MySchemaType.One);
136+
myschema.Children.Should().HaveCount(1);
137+
var child = myschema.Children.First();
138+
139+
child.Type.Should().Be(MySchemaType.Two);
140+
child.Length.Should().Be(2);
141+
}
142+
}

0 commit comments

Comments
 (0)