-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathOpenApiWorkspace.cs
281 lines (247 loc) · 11.3 KB
/
OpenApiWorkspace.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
namespace Microsoft.OpenApi.Services
{
/// <summary>
/// Contains a set of OpenApi documents and document fragments that reference each other
/// </summary>
public class OpenApiWorkspace
{
private readonly Dictionary<string, Uri> _documentsIdRegistry = new();
private readonly Dictionary<Uri, Stream> _artifactsRegistry = new();
private readonly Dictionary<Uri, IOpenApiReferenceable> _IOpenApiReferenceableRegistry = new();
/// <summary>
/// The base location from where all relative references are resolved
/// </summary>
public Uri BaseUrl { get; }
/// <summary>
/// Initialize workspace pointing to a base URL to allow resolving relative document locations. Use a file:// url to point to a folder
/// </summary>
/// <param name="baseUrl"></param>
public OpenApiWorkspace(Uri baseUrl)
{
BaseUrl = baseUrl;
}
/// <summary>
/// Initialize workspace using current directory as the default location.
/// </summary>
public OpenApiWorkspace()
{
BaseUrl = new Uri(OpenApiConstants.BaseRegistryUri);
}
/// <summary>
/// Initializes a copy of an <see cref="OpenApiWorkspace"/> object
/// </summary>
public OpenApiWorkspace(OpenApiWorkspace workspace) { }
/// <summary>
/// Returns the total count of all the components in the workspace registry
/// </summary>
/// <returns></returns>
public int ComponentsCount()
{
return _IOpenApiReferenceableRegistry.Count + _artifactsRegistry.Count;
}
private const string ComponentSegmentSeparator = "/";
/// <summary>
/// Registers a document's components into the workspace
/// </summary>
/// <param name="document"></param>
public void RegisterComponents(OpenApiDocument document)
{
if (document?.Components == null) return;
string baseUri = getBaseUri(document);
string location;
// Register Schema
foreach (var item in document.Components.Schemas)
{
location = item.Value.Id ?? baseUri + ReferenceType.Schema.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register Parameters
foreach (var item in document.Components.Parameters)
{
location = baseUri + ReferenceType.Parameter.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register Responses
foreach (var item in document.Components.Responses)
{
location = baseUri + ReferenceType.Response.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register RequestBodies
foreach (var item in document.Components.RequestBodies)
{
location = baseUri + ReferenceType.RequestBody.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register Links
foreach (var item in document.Components.Links)
{
location = baseUri + ReferenceType.Link.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register Callbacks
foreach (var item in document.Components.Callbacks)
{
location = baseUri + ReferenceType.Callback.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register PathItems
foreach (var item in document.Components.PathItems)
{
location = baseUri + ReferenceType.PathItem.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register Examples
foreach (var item in document.Components.Examples)
{
location = baseUri + ReferenceType.Example.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register Headers
foreach (var item in document.Components.Headers)
{
location = baseUri + ReferenceType.Header.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
// Register SecuritySchemes
foreach (var item in document.Components.SecuritySchemes)
{
location = baseUri + ReferenceType.SecurityScheme.GetDisplayName() + ComponentSegmentSeparator + item.Key;
RegisterComponent(location, item.Value);
}
}
private string getBaseUri(OpenApiDocument openApiDocument)
{
return openApiDocument.BaseUri + OpenApiConstants.ComponentsSegment;
}
/// <summary>
/// Registers a component for a document in the workspace
/// </summary>
/// <param name="openApiDocument">The document to register the component for.</param>
/// <param name="componentToRegister">The component to register.</param>
/// <param name="id">The id of the component.</param>
/// <typeparam name="T">The type of the component to register.</typeparam>
/// <returns>true if the component is successfully registered; otherwise false.</returns>
/// <exception cref="ArgumentNullException">openApiDocument is null</exception>
/// <exception cref="ArgumentNullException">componentToRegister is null</exception>
/// <exception cref="ArgumentNullException">id is null or empty</exception>
public bool RegisterComponentForDocument<T>(OpenApiDocument openApiDocument, T componentToRegister, string id)
{
Utils.CheckArgumentNull(openApiDocument);
Utils.CheckArgumentNull(componentToRegister);
Utils.CheckArgumentNullOrEmpty(id);
var baseUri = getBaseUri(openApiDocument);
var location = componentToRegister switch
{
OpenApiSchema => baseUri + ReferenceType.Schema.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiParameter => baseUri + ReferenceType.Parameter.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiResponse => baseUri + ReferenceType.Response.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiRequestBody => baseUri + ReferenceType.RequestBody.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiLink => baseUri + ReferenceType.Link.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiCallback => baseUri + ReferenceType.Callback.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiPathItem => baseUri + ReferenceType.PathItem.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiExample => baseUri + ReferenceType.Example.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiHeader => baseUri + ReferenceType.Header.GetDisplayName() + ComponentSegmentSeparator + id,
OpenApiSecurityScheme => baseUri + ReferenceType.SecurityScheme.GetDisplayName() + ComponentSegmentSeparator + id,
_ => throw new ArgumentException($"Invalid component type {componentToRegister.GetType().Name}"),
};
return RegisterComponent(location, componentToRegister);
}
/// <summary>
/// Registers a component in the component registry.
/// </summary>
/// <param name="location"></param>
/// <param name="component"></param>
/// <returns>true if the component is successfully registered; otherwise false.</returns>
internal bool RegisterComponent<T>(string location, T component)
{
var uri = ToLocationUrl(location);
if (component is IOpenApiReferenceable referenceable)
{
if (!_IOpenApiReferenceableRegistry.ContainsKey(uri))
{
_IOpenApiReferenceableRegistry[uri] = referenceable;
return true;
}
}
else if (component is Stream stream)
{
if (!_artifactsRegistry.ContainsKey(uri))
{
_artifactsRegistry[uri] = stream;
return true;
}
}
return false;
}
/// <summary>
/// Adds a document id to the dictionaries of document locations and their ids.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddDocumentId(string key, Uri value)
{
if (!_documentsIdRegistry.ContainsKey(key))
{
_documentsIdRegistry[key] = value;
}
}
/// <summary>
/// Retrieves the document id given a key.
/// </summary>
/// <param name="key"></param>
/// <returns>The document id of the given key.</returns>
public Uri GetDocumentId(string key)
{
if (_documentsIdRegistry.TryGetValue(key, out var id))
{
return id;
}
return null;
}
/// <summary>
/// Verify if workspace contains a component based on its URL.
/// </summary>
/// <param name="location">A relative or absolute URL of the file. Use file:// for folder locations.</param>
/// <returns>Returns true if a matching document is found.</returns>
public bool Contains(string location)
{
var key = ToLocationUrl(location);
return _IOpenApiReferenceableRegistry.ContainsKey(key) || _artifactsRegistry.ContainsKey(key);
}
#nullable enable
/// <summary>
/// Resolves a reference given a key.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="location"></param>
/// <returns>The resolved reference.</returns>
public T? ResolveReference<T>(string location)
{
if (string.IsNullOrEmpty(location)) return default;
var uri = ToLocationUrl(location);
if (_IOpenApiReferenceableRegistry.TryGetValue(uri, out var referenceableValue))
{
return (T)referenceableValue;
}
else if (_artifactsRegistry.TryGetValue(uri, out var artifact))
{
return (T)(object)artifact;
}
return default;
}
#nullable restore
private Uri ToLocationUrl(string location)
{
return new(BaseUrl, location);
}
}
}