-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsyncApiMessageDeserializer.cs
More file actions
166 lines (150 loc) · 6.04 KB
/
AsyncApiMessageDeserializer.cs
File metadata and controls
166 lines (150 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
namespace ByteBard.AsyncAPI.Readers
{
using System.Collections.Generic;
using System.Linq;
using ByteBard.AsyncAPI.Exceptions;
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.ParseNodes;
/// <summary>
/// Class containing logic to deserialize AsyncApi document into
/// runtime AsyncApi object model.
/// </summary>
internal static partial class AsyncApiV2Deserializer
{
private static readonly FixedFieldMap<AsyncApiMessage> messageFixedFields = new()
{
{
"messageId", (a, n) => { }
},
{
"headers", (a, n) => { /* Loaded later */ }
},
{
"payload", (a, n) => { /* a.Payload = new AsyncApiMultiFormatSchema(); */ }
},
{
"correlationId", (a, n) => { a.CorrelationId = LoadCorrelationId(n); }
},
{
"schemaFormat", (a, n) => { /* a.Payload.SchemaFormat = n.GetScalarValue(); */ }
},
{
"contentType", (a, n) => { a.ContentType = n.GetScalarValue(); }
},
{
"name", (a, n) => { a.Name = n.GetScalarValue(); }
},
{
"title", (a, n) => { a.Title = n.GetScalarValue(); }
},
{
"summary", (a, n) => { a.Summary = n.GetScalarValue(); }
},
{
"description", (a, n) => { a.Description = n.GetScalarValue(); }
},
{
"tags", (a, n) => a.Tags = n.CreateList(LoadTag)
},
{
"externalDocs", (a, n) => { a.ExternalDocs = LoadExternalDocs(n); }
},
{
"bindings", (a, n) => { a.Bindings = LoadMessageBindings(n); }
},
{
"examples", (a, n) => a.Examples = n.CreateList(LoadExample)
},
{
"traits", (a, n) => a.Traits = n.CreateList(LoadMessageTrait)
},
};
public static IAsyncApiSchema LoadJsonSchemaPayload(ParseNode n)
{
return LoadPayload(n, null);
}
public static IAsyncApiSchema LoadAvroPayload(ParseNode n)
{
return LoadPayload(n, "application/vnd.apache.avro");
}
private static IAsyncApiSchema LoadPayload(ParseNode n, string format)
{
if (n == null)
{
return null;
}
switch (format)
{
case null:
case "":
case var _ when SupportedJsonSchemaFormats.Where(s => format.StartsWith(s)).Any():
return AsyncApiJsonSchemaDeserializer.LoadSchema(n);
case var _ when SupportedAvroSchemaFormats.Where(s => format.StartsWith(s)).Any():
return AsyncApiAvroSchemaDeserializer.LoadSchema(n);
default:
var supportedFormats = SupportedJsonSchemaFormats.Concat(SupportedAvroSchemaFormats);
throw new AsyncApiException($"Could not deserialize Payload. Supported formats are {string.Join(", ", supportedFormats)}");
}
}
static readonly IEnumerable<string> SupportedJsonSchemaFormats = new List<string>
{
"application/vnd.aai.asyncapi+json",
"application/vnd.aai.asyncapi+yaml",
"application/vnd.aai.asyncapi",
"application/schema+json;version=draft-07",
"application/schema+yaml;version=draft-07",
};
static readonly IEnumerable<string> SupportedAvroSchemaFormats = new List<string>
{
"application/vnd.apache.avro",
"application/vnd.apache.avro+json",
"application/vnd.apache.avro+yaml",
"application/vnd.apache.avro+json;version=1.9.0",
"application/vnd.apache.avro+yaml;version=1.9.0",
};
private static string LoadSchemaFormat(string schemaFormat)
{
var supportedFormats = SupportedJsonSchemaFormats.Concat(SupportedAvroSchemaFormats);
if (!supportedFormats.Where(s => schemaFormat.StartsWith(s)).Any())
{
throw new AsyncApiException($"'{schemaFormat}' is not a supported format. Supported formats are {string.Join(", ", supportedFormats)}");
}
return schemaFormat;
}
private static readonly PatternFieldMap<AsyncApiMessage> messagePatternFields = new()
{
{ s => s.StartsWith("x-"), (a, p, n) => a.AddExtension(p, LoadExtension(p, n)) },
};
public static AsyncApiMessage LoadMessage(ParseNode node)
{
var mapNode = node.CheckMapNode("message");
var pointer = mapNode.GetReferencePointer();
if (pointer != null)
{
return new AsyncApiMessageReference(pointer);
}
var message = new AsyncApiMessage();
ParseMap(mapNode, message, messageFixedFields, messagePatternFields);
if (mapNode["headers"] != null)
{
message.Headers = new AsyncApiMultiFormatSchema { Schema = AsyncApiJsonSchemaDeserializer.LoadSchema(mapNode["headers"].Value) };
}
if (mapNode["payload"] != null)
{
var schema = mapNode["schemaFormat"]?.Value.GetScalarValue();
var payload = LoadPayload(mapNode["payload"].Value, schema);
if (payload is IAsyncApiReferenceable reference && !reference.Reference.IsExternal)
{
message.Payload = new AsyncApiMultiFormatSchemaReference(reference.Reference.Reference);
}
else
{
message.Payload = new AsyncApiMultiFormatSchema { Schema = payload, SchemaFormat = schema };
}
}
return message;
}
}
}