Repository navigation
Expand file tree
/
Copy pathAsyncApiWorkspace.cs
More file actions
334 lines (285 loc) · 13.8 KB
/
Copy pathAsyncApiWorkspace.cs
File metadata and controls
334 lines (285 loc) · 13.8 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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<string, Stream> artifactsRegistry = new(StringComparer.Ordinal);
private readonly Dictionary<string, IAsyncApiSerializable> resolvedReferenceRegistry = new(StringComparer.Ordinal);
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() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register Parameters
foreach (var item in document.Components.Parameters)
{
location = componentsBaseUri + ReferenceType.Parameter.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register Channels
foreach (var item in document.Components.Channels)
{
location = componentsBaseUri + ReferenceType.Channel.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register Operations
foreach (var item in document.Components.Operations)
{
location = componentsBaseUri + ReferenceType.Operation.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register Servers
foreach (var item in document.Components.Servers)
{
location = componentsBaseUri + ReferenceType.Server.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register ServerVariables
foreach (var item in document.Components.ServerVariables)
{
location = componentsBaseUri + ReferenceType.ServerVariable.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register Messages
foreach (var item in document.Components.Messages)
{
location = componentsBaseUri + ReferenceType.Message.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register SecuritySchemes
foreach (var item in document.Components.SecuritySchemes)
{
location = componentsBaseUri + ReferenceType.SecurityScheme.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(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() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register CorrelationIds
foreach (var item in document.Components.CorrelationIds)
{
location = componentsBaseUri + ReferenceType.CorrelationId.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register Replies
foreach (var item in document.Components.Replies)
{
location = componentsBaseUri + ReferenceType.OperationReply.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register ReplyAddresses
foreach (var item in document.Components.ReplyAddresses)
{
location = componentsBaseUri + ReferenceType.OperationReplyAddress.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register ExternalDocs
foreach (var item in document.Components.ExternalDocs)
{
location = componentsBaseUri + ReferenceType.ExternalDocs.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register Tags
foreach (var item in document.Components.Tags)
{
location = componentsBaseUri + ReferenceType.Tag.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register OperationTraits
foreach (var item in document.Components.OperationTraits)
{
location = componentsBaseUri + ReferenceType.OperationTrait.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register MessageTraits
foreach (var item in document.Components.MessageTraits)
{
location = componentsBaseUri + ReferenceType.MessageTrait.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register ServerBindings
foreach (var item in document.Components.ServerBindings)
{
location = componentsBaseUri + ReferenceType.ServerBindings.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register ChannelBindings
foreach (var item in document.Components.ChannelBindings)
{
location = componentsBaseUri + ReferenceType.ChannelBindings.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register OperationBindings
foreach (var item in document.Components.OperationBindings)
{
location = componentsBaseUri + ReferenceType.OperationBindings.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register MessageBindings
foreach (var item in document.Components.MessageBindings)
{
location = componentsBaseUri + ReferenceType.MessageBindings.GetDisplayName() + "/" + JsonPointer.EscapeUriFragmentToken(item.Key);
this.RegisterComponent(location, item.Value);
}
// Register references last, so one pointing at another root channel or server finds it registered.
string channelBaseUri = "#/channels/";
foreach (var channel in document.Channels.OrderBy(channel => channel.Value is IAsyncApiReferenceable))
{
var registerableChannelValue = channel.Value;
if (channel.Value is IAsyncApiReferenceable reference)
{
if (reference.Reference.IsExternal)
{
continue;
}
registerableChannelValue = this.ResolveReference<AsyncApiChannel>(reference.Reference);
}
// A reference to a channel that is not registered (yet) is resolved by the reader later.
if (registerableChannelValue == null)
{
continue;
}
location = channelBaseUri + JsonPointer.EscapeUriFragmentToken(channel.Key);
this.RegisterComponent(location, registerableChannelValue);
foreach (var message in registerableChannelValue.Messages)
{
this.RegisterComponent(location + "/messages/" + JsonPointer.EscapeUriFragmentToken(message.Key), message.Value);
}
}
string serverBaseUri = "#/servers/";
foreach (var server in document.Servers.OrderBy(server => server.Value is IAsyncApiReferenceable))
{
var registerableServerValue = server.Value;
if (server.Value is IAsyncApiReferenceable serverReference)
{
if (serverReference.Reference.IsExternal)
{
continue;
}
registerableServerValue = this.ResolveReference<AsyncApiServer>(serverReference.Reference);
}
location = serverBaseUri + JsonPointer.EscapeUriFragmentToken(server.Key);
this.RegisterComponent(location, registerableServerValue);
}
}
public bool RegisterComponent<T>(string location, T component)
{
var key = NormalizeLocation(location);
if (component is IAsyncApiSerializable referenceable)
{
if (!this.resolvedReferenceRegistry.ContainsKey(key))
{
this.resolvedReferenceRegistry[key] = referenceable;
return true;
}
}
if (component is Stream stream)
{
if (!this.artifactsRegistry.ContainsKey(key))
{
this.artifactsRegistry[key] = stream;
}
return true;
}
return false;
}
public bool Contains(string location)
{
var key = NormalizeLocation(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 key = NormalizeLocation(location);
if (this.resolvedReferenceRegistry.TryGetValue(key, out var referenceableValue))
{
return referenceableValue as T;
}
if (this.artifactsRegistry.TryGetValue(key, out var stream))
{
stream.Position = 0;
return (T)(object)stream;
}
return default;
}
/// <summary>
/// Rewrites the JSON pointer fragment of a location into one percent-encoded form (RFC 6901 section 6), so encoded
/// and unencoded references to the same key share a registry entry, e.g. <c>#/servers/a b</c> and <c>#/servers/a%20b</c>.
/// Locations are compared as strings, as <see cref="Uri"/> equality ignores the fragment of absolute URIs.
/// </summary>
private static string NormalizeLocation(string location)
{
var fragmentStart = location.IndexOf('#');
if (fragmentStart < 0 || !JsonPointer.TryFromUriFragment(location.Substring(fragmentStart + 1), out var pointer))
{
return location;
}
return location.Substring(0, fragmentStart + 1) + pointer.ToUriFragment();
}
public void SetRootDocument(AsyncApiDocument doc)
{
this.RootDocument = doc;
doc.SourceWorkspace = this;
}
/// <summary>
/// Prepares this workspace for writing the document: registers its components, then adds what the workspace
/// the document was read into resolved outside them, so references to those can still be written inline.
/// </summary>
internal void RegisterForWriting(AsyncApiDocument doc)
{
this.RootDocument = doc;
this.RegisterComponents(doc);
if (doc.SourceWorkspace == null || doc.SourceWorkspace == this)
{
return;
}
foreach (var resolved in doc.SourceWorkspace.resolvedReferenceRegistry)
{
if (!this.resolvedReferenceRegistry.ContainsKey(resolved.Key))
{
this.resolvedReferenceRegistry[resolved.Key] = resolved.Value;
}
}
}
}
}