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