forked from ByteBardOrg/AsyncAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncApiWorkspace.cs
More file actions
292 lines (248 loc) · 10.9 KB
/
AsyncApiWorkspace.cs
File metadata and controls
292 lines (248 loc) · 10.9 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
namespace ByteBard.AsyncAPI
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
public class AsyncApiWorkspace
{
private readonly Dictionary<Uri, Stream> artifactsRegistry = new();
private readonly Dictionary<Uri, IAsyncApiSerializable> resolvedReferenceRegistry = new();
public AsyncApiDocument RootDocument { get; private set; }
/// <summary>
/// Stack for tracking parent context during serialization.
/// Allows bindings to access their parent operation, channel, or message.
/// </summary>
public Stack<object> SerializationContext { get; } = new();
/// <summary>
/// Gets the first item of the specified type from the serialization context.
/// </summary>
/// <typeparam name="T">The type to find in the context.</typeparam>
/// <returns>The first matching item, or default if not found.</returns>
public T GetSerializationContext<T>()
where T : class
{
return this.SerializationContext.OfType<T>().FirstOrDefault();
}
public void RegisterComponents(AsyncApiDocument document)
{
if (document?.Components == null)
{
return;
}
string componentsBaseUri = "#/components/";
string location;
// Register Schema
foreach (var item in document.Components.Schemas)
{
location = componentsBaseUri + ReferenceType.Schema.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register Parameters
foreach (var item in document.Components.Parameters)
{
location = componentsBaseUri + ReferenceType.Parameter.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register Channels
foreach (var item in document.Components.Channels)
{
location = componentsBaseUri + ReferenceType.Channel.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register Operations
foreach (var item in document.Components.Operations)
{
location = componentsBaseUri + ReferenceType.Operation.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register Servers
foreach (var item in document.Components.Servers)
{
location = componentsBaseUri + ReferenceType.Server.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register ServerVariables
foreach (var item in document.Components.ServerVariables)
{
location = componentsBaseUri + ReferenceType.ServerVariable.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register Messages
foreach (var item in document.Components.Messages)
{
location = componentsBaseUri + ReferenceType.Message.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register SecuritySchemes
foreach (var item in document.Components.SecuritySchemes)
{
location = componentsBaseUri + ReferenceType.SecurityScheme.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
this.RegisterComponent(item.Key, item.Value);
}
// Register Server Variables
foreach (var item in document.Components.ServerVariables)
{
location = componentsBaseUri + ReferenceType.ServerVariable.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register CorrelationIds
foreach (var item in document.Components.CorrelationIds)
{
location = componentsBaseUri + ReferenceType.CorrelationId.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register Replies
foreach (var item in document.Components.Replies)
{
location = componentsBaseUri + ReferenceType.OperationReply.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register ReplyAddresses
foreach (var item in document.Components.ReplyAddresses)
{
location = componentsBaseUri + ReferenceType.OperationReplyAddress.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register ExternalDocs
foreach (var item in document.Components.ExternalDocs)
{
location = componentsBaseUri + ReferenceType.ExternalDocs.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register Tags
foreach (var item in document.Components.Tags)
{
location = componentsBaseUri + ReferenceType.Tag.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register OperationTraits
foreach (var item in document.Components.OperationTraits)
{
location = componentsBaseUri + ReferenceType.OperationTrait.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register MessageTraits
foreach (var item in document.Components.MessageTraits)
{
location = componentsBaseUri + ReferenceType.MessageTrait.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register ServerBindings
foreach (var item in document.Components.ServerBindings)
{
location = componentsBaseUri + ReferenceType.ServerBindings.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register ChannelBindings
foreach (var item in document.Components.ChannelBindings)
{
location = componentsBaseUri + ReferenceType.ChannelBindings.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register OperationBindings
foreach (var item in document.Components.OperationBindings)
{
location = componentsBaseUri + ReferenceType.OperationBindings.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
// Register MessageBindings
foreach (var item in document.Components.MessageBindings)
{
location = componentsBaseUri + ReferenceType.MessageBindings.GetDisplayName() + "/" + item.Key;
this.RegisterComponent(location, item.Value);
}
string channelBaseUri = "#/channels/";
foreach (var channel in document.Channels)
{
var registerableChannelValue = channel.Value;
if (channel.Value is IAsyncApiReferenceable reference)
{
if (reference.Reference.IsExternal)
{
continue;
}
registerableChannelValue = this.ResolveReference<AsyncApiChannel>(reference.Reference);
}
location = channelBaseUri + channel.Key;
this.RegisterComponent(location, registerableChannelValue);
foreach (var message in registerableChannelValue.Messages)
{
this.RegisterComponent(location + "/messages/" + message.Key, message.Value);
}
}
string serverBaseUri = "#/servers/";
foreach (var server in document.Servers)
{
var registerableServerValue = server.Value;
if (server.Value is IAsyncApiReferenceable serverReference)
{
if (serverReference.Reference.IsExternal)
{
continue;
}
registerableServerValue = this.ResolveReference<AsyncApiServer>(serverReference.Reference);
}
location = serverBaseUri + server.Key;
this.RegisterComponent(location, registerableServerValue);
}
}
public bool RegisterComponent<T>(string location, T component)
{
var uri = this.ToLocationUrl(location);
if (component is IAsyncApiSerializable referenceable)
{
if (!this.resolvedReferenceRegistry.ContainsKey(uri))
{
this.resolvedReferenceRegistry[uri] = referenceable;
return true;
}
}
if (component is Stream stream)
{
if (!this.artifactsRegistry.ContainsKey(uri))
{
this.artifactsRegistry[uri] = stream;
}
return true;
}
return false;
}
public bool Contains(string location)
{
var key = this.ToLocationUrl(location);
return this.resolvedReferenceRegistry.ContainsKey(key) || this.artifactsRegistry.ContainsKey(key);
}
public T ResolveReference<T>(AsyncApiReference reference)
where T : class
{
return this.ResolveReference<T>(reference.Reference);
}
public T ResolveReference<T>(string location)
where T : class
{
var uri = this.ToLocationUrl(location);
if (this.resolvedReferenceRegistry.TryGetValue(uri, out var referenceableValue))
{
return referenceableValue as T;
}
if (this.artifactsRegistry.TryGetValue(uri, out var stream))
{
stream.Position = 0;
return (T)(object)stream;
}
return default;
}
private Uri ToLocationUrl(string location)
{
return new(location, UriKind.RelativeOrAbsolute);
}
public void SetRootDocument(AsyncApiDocument doc)
{
this.RootDocument = doc;
}
}
}