-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAsyncApiExternalReferenceResolver.cs
More file actions
256 lines (227 loc) · 9.09 KB
/
AsyncApiExternalReferenceResolver.cs
File metadata and controls
256 lines (227 loc) · 9.09 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
using LEGO.AsyncAPI.Readers.Exceptions;
using LEGO.AsyncAPI.Services;
namespace LEGO.AsyncAPI.Readers
{
using System;
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
/// <summary>
/// This class is used to walk an AsyncApiDocument and convert unresolved references to references to populated objects.
/// </summary>
internal class AsyncApiExternalReferenceResolver : AsyncApiVisitorBase
{
private AsyncApiDocument currentDocument;
private List<AsyncApiError> errors = new List<AsyncApiError>();
private AsyncApiReaderSettings readerSettings;
public AsyncApiExternalReferenceResolver(
AsyncApiDocument currentDocument,
AsyncApiReaderSettings readerSettings)
{
this.currentDocument = currentDocument;
this.readerSettings = readerSettings;
}
public IEnumerable<AsyncApiError> Errors
{
get
{
return this.errors;
}
}
public override void Visit(IAsyncApiReferenceable referenceable)
{
if (referenceable.Reference != null)
{
referenceable.Reference.HostDocument = this.currentDocument;
}
}
public override void Visit(AsyncApiComponents components)
{
this.ResolveMap(components.Parameters);
this.ResolveMap(components.Channels);
this.ResolveMap(components.Schemas);
this.ResolveMap(components.Servers);
this.ResolveMap(components.CorrelationIds);
this.ResolveMap(components.MessageTraits);
this.ResolveMap(components.OperationTraits);
this.ResolveMap(components.SecuritySchemes);
this.ResolveMap(components.ChannelBindings);
this.ResolveMap(components.MessageBindings);
this.ResolveMap(components.OperationBindings);
this.ResolveMap(components.ServerBindings);
this.ResolveMap(components.Messages);
}
public override void Visit(AsyncApiDocument doc)
{
this.ResolveMap(doc.Servers);
this.ResolveMap(doc.Channels);
}
public override void Visit(AsyncApiChannel channel)
{
this.ResolveMap(channel.Parameters);
this.ResolveObject(channel.Bindings, r => channel.Bindings = r);
}
public override void Visit(AsyncApiMessageTrait trait)
{
this.ResolveObject(trait.CorrelationId, r => trait.CorrelationId = r);
this.ResolveObject(trait.Headers, r => trait.Headers = r);
}
/// <summary>
/// Resolve all references used in an operation.
/// </summary>
public override void Visit(AsyncApiOperation operation)
{
this.ResolveList(operation.Message);
this.ResolveList(operation.Traits);
this.ResolveObject(operation.Bindings, r => operation.Bindings = r);
}
public override void Visit(AsyncApiMessage message)
{
this.ResolveObject(message.Headers, r => message.Headers = r);
switch (message.Payload)
{
case AsyncApiJsonSchemaPayload json:
this.ResolveObject<AsyncApiJsonSchema>(message.Payload as AsyncApiJsonSchemaPayload, r => message.Payload = new AsyncApiJsonSchemaPayload(r));
break;
case AsyncApiAvroSchemaPayload avro:
// ToFix: this might not resolve correctly.
this.ResolveObject(message.Payload as AsyncApiAvroSchemaPayload, r => message.Payload = r);
break;
default:
break;
}
this.ResolveList(message.Traits);
this.ResolveObject(message.CorrelationId, r => message.CorrelationId = r);
this.ResolveObject(message.Bindings, r => message.Bindings = r);
}
public override void Visit(AsyncApiServer server)
{
this.ResolveObject(server.Bindings, r => server.Bindings = r);
}
/// <summary>
/// Resolve all references to SecuritySchemes.
/// </summary>
public override void Visit(AsyncApiSecurityRequirement securityRequirement)
{
foreach (var scheme in securityRequirement.Keys.ToList())
{
this.ResolveObject(scheme, (resolvedScheme) =>
{
if (resolvedScheme != null)
{
// If scheme was unresolved
// copy Scopes and remove old unresolved scheme
var scopes = securityRequirement[scheme];
securityRequirement.Remove(scheme);
securityRequirement.Add(resolvedScheme, scopes);
}
});
}
}
/// <summary>
/// Resolve all references to parameters.
/// </summary>
public override void Visit(IList<AsyncApiParameter> parameters)
{
this.ResolveList(parameters);
}
/// <summary>
/// Resolve all references used in a parameter.
/// </summary>
public override void Visit(AsyncApiParameter parameter)
{
this.ResolveObject(parameter.Schema, r => parameter.Schema = r);
}
/// <summary>
/// Resolve all references used in a schema.
/// </summary>
public override void Visit(AsyncApiJsonSchema schema)
{
this.ResolveObject(schema.Items, r => schema.Items = r);
this.ResolveList(schema.OneOf);
this.ResolveList(schema.AllOf);
this.ResolveList(schema.AnyOf);
this.ResolveObject(schema.Contains, r => schema.Contains = r);
this.ResolveObject(schema.Else, r => schema.Else = r);
this.ResolveObject(schema.If, r => schema.If = r);
this.ResolveObject(schema.Items, r => schema.Items = r);
this.ResolveObject(schema.Not, r => schema.Not = r);
this.ResolveObject(schema.Then, r => schema.Then = r);
this.ResolveObject(schema.PropertyNames, r => schema.PropertyNames = r);
this.ResolveObject(schema.AdditionalProperties, r => schema.AdditionalProperties = r);
this.ResolveMap(schema.Properties);
}
private void ResolveObject<T>(T entity, Action<T> assign)
where T : class, IAsyncApiReferenceable, new()
{
if (entity == null)
{
return;
}
if (this.IsUnresolvedReference(entity))
{
assign(this.ResolveReference<T>(entity.Reference));
}
}
private void ResolveList<T>(IList<T> list)
where T : class, IAsyncApiReferenceable, new()
{
if (list == null)
{
return;
}
for (int i = 0; i < list.Count; i++)
{
var entity = list[i];
if (this.IsUnresolvedReference(entity))
{
list[i] = this.ResolveReference<T>(entity.Reference);
}
}
}
private void ResolveMap<T>(IDictionary<string, T> map)
where T : class, IAsyncApiReferenceable, new()
{
if (map == null)
{
return;
}
foreach (var key in map.Keys.ToList())
{
var entity = map[key];
if (this.IsUnresolvedReference(entity))
{
map[key] = this.ResolveReference<T>(entity.Reference);
}
}
}
private T ResolveReference<T>(AsyncApiReference reference)
where T : class, IAsyncApiReferenceable, new()
{
if (reference.IsExternal)
{
if (this.readerSettings.ExternalReferenceReader is null)
{
throw new AsyncApiReaderException(
"External reference configured in AsyncApi document but no implementation provided for ExternalReferenceReader.");
}
// read external content
var externalContent = this.readerSettings.ExternalReferenceReader.Load(reference.Reference);
// read external object content
var reader = new AsyncApiStringReader(this.readerSettings);
var externalAsyncApiContent = reader.ReadFragment<T>(externalContent, AsyncApiVersion.AsyncApi2_0, out var diagnostic);
foreach (var error in diagnostic.Errors)
{
this.errors.Add(error);
}
return externalAsyncApiContent;
}
return null;
}
private bool IsUnresolvedReference(IAsyncApiReferenceable possibleReference)
{
return (possibleReference != null && possibleReference.UnresolvedReference);
}
}
}