-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathOpenApiFilterService.cs
411 lines (360 loc) · 16.3 KB
/
OpenApiFilterService.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Properties;
namespace Microsoft.OpenApi.Services
{
/// <summary>
/// A service that slices an OpenApiDocument into a subset document
/// </summary>
public static class OpenApiFilterService
{
/// <summary>
/// Create predicate function based on passed query parameters
/// </summary>
/// <param name="operationIds">Comma delimited list of operationIds or * for all operations.</param>
/// <param name="tags">Comma delimited list of tags or a single regex.</param>
/// <param name="requestUrls">A dictionary of requests from a postman collection.</param>
/// <param name="source">The input OpenAPI document.</param>
/// <returns>A predicate.</returns>
public static Func<string, OperationType?, OpenApiOperation, bool> CreatePredicate(
string operationIds = null,
string tags = null,
Dictionary<string, List<string>> requestUrls = null,
OpenApiDocument source = null)
{
Func<string, OperationType?, OpenApiOperation, bool> predicate;
ValidateFilters(requestUrls, operationIds, tags);
if (operationIds != null)
{
predicate = GetOperationIdsPredicate(operationIds);
}
else if (tags != null)
{
predicate = GetTagsPredicate(tags);
}
else if (requestUrls != null)
{
predicate = GetRequestUrlsPredicate(requestUrls, source);
}
else
{
throw new InvalidOperationException("Either operationId(s),tag(s) or Postman collection need to be specified.");
}
return predicate;
}
/// <summary>
/// Create partial OpenAPI document based on the provided predicate.
/// </summary>
/// <param name="source">The target <see cref="OpenApiDocument"/>.</param>
/// <param name="predicate">A predicate function.</param>
/// <returns>A partial OpenAPI document.</returns>
public static OpenApiDocument CreateFilteredDocument(OpenApiDocument source, Func<string, OperationType?, OpenApiOperation, bool> predicate)
{
// Fetch and copy title, graphVersion and server info from OpenApiDoc
var components = source.Components is null
? null
: new OpenApiComponents() { SecuritySchemes = source.Components.SecuritySchemes };
var subset = new OpenApiDocument
{
Info = new()
{
Title = source.Info.Title + " - Subset",
Description = source.Info.Description,
TermsOfService = source.Info.TermsOfService,
Contact = source.Info.Contact,
License = source.Info.License,
Version = source.Info.Version,
Extensions = source.Info.Extensions
},
Components = components,
SecurityRequirements = source.SecurityRequirements,
Servers = source.Servers
};
var results = FindOperations(source, predicate);
foreach (var result in results)
{
OpenApiPathItem pathItem;
var pathKey = result.CurrentKeys.Path;
if (subset.Paths == null)
{
subset.Paths = new();
pathItem = new();
subset.Paths.Add(pathKey, pathItem);
}
else
{
if (!subset.Paths.TryGetValue(pathKey, out pathItem))
{
pathItem = new();
subset.Paths.Add(pathKey, pathItem);
}
}
if (result.CurrentKeys.Operation != null)
{
pathItem.Operations.Add((OperationType)result.CurrentKeys.Operation, result.Operation);
if (result.Parameters?.Any() ?? false)
{
foreach (var parameter in result.Parameters)
{
pathItem.Parameters.Add(parameter);
}
}
}
}
if (subset.Paths == null)
{
throw new ArgumentException("No paths found for the supplied parameters.");
}
CopyReferences(subset);
return subset;
}
/// <summary>
/// Creates an <see cref="OpenApiUrlTreeNode"/> from a collection of <see cref="OpenApiDocument"/>.
/// </summary>
/// <param name="sources">Dictionary of labels and their corresponding <see cref="OpenApiDocument"/> objects.</param>
/// <returns>The created <see cref="OpenApiUrlTreeNode"/>.</returns>
public static OpenApiUrlTreeNode CreateOpenApiUrlTreeNode(Dictionary<string, OpenApiDocument> sources)
{
var rootNode = OpenApiUrlTreeNode.Create();
foreach (var source in sources)
{
rootNode.Attach(source.Value, source.Key);
}
return rootNode;
}
private static IDictionary<OperationType, OpenApiOperation> GetOpenApiOperations(OpenApiUrlTreeNode rootNode, string relativeUrl, string label)
{
if (relativeUrl.Equals("/", StringComparison.Ordinal) && rootNode.HasOperations(label))
{
return rootNode.PathItems[label].Operations;
}
var urlSegments = relativeUrl.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
IDictionary<OperationType, OpenApiOperation> operations = null;
var targetChild = rootNode;
/* This will help keep track of whether we've skipped a segment
* in the target url due to a possible parameter naming mismatch
* with the corresponding OpenApiUrlTreeNode target child segment.
*/
var parameterNameOffset = 0;
for (var i = 0; i < urlSegments?.Length; i++)
{
var tempTargetChild = targetChild?.Children?
.FirstOrDefault(x => x.Key.Equals(urlSegments[i],
StringComparison.OrdinalIgnoreCase)).Value;
// Segment name mismatch
if (tempTargetChild == null)
{
if (i == 0)
{
/* If no match and we are at the 1st segment of the relative url,
* exit; no need to continue matching subsequent segments.
*/
break;
}
/* Attempt to get the parameter segment from the children of the current node:
* We are assuming a failed match because of different parameter namings
* between the relative url segment and the corresponding OpenApiUrlTreeNode segment name
* ex.: matching '/users/12345/messages' with '/users/{user-id}/messages'
*/
tempTargetChild = targetChild?.Children?
.FirstOrDefault(x => x.Value.IsParameter).Value;
/* If no parameter segment exists in the children of the
* current node or we've already skipped a parameter
* segment in the relative url from the last pass,
* then exit; there's no match.
*/
if (tempTargetChild == null || parameterNameOffset > 0)
{
break;
}
/* To help us know we've skipped a
* corresponding segment in the relative url.
*/
parameterNameOffset++;
}
else
{
parameterNameOffset = 0;
}
// Move to the next segment
targetChild = tempTargetChild;
// We want the operations of the last segment of the path.
if (i == urlSegments.Length - 1 && targetChild.HasOperations(label))
{
operations = targetChild.PathItems[label].Operations;
}
}
return operations;
}
private static IList<SearchResult> FindOperations(OpenApiDocument sourceDocument, Func<string, OperationType?, OpenApiOperation, bool> predicate)
{
var search = new OperationSearch(predicate);
var walker = new OpenApiWalker(search);
walker.Walk(sourceDocument);
return search.SearchResults;
}
private static void CopyReferences(OpenApiDocument target)
{
bool morestuff;
do
{
var copy = new CopyReferences(target);
var walker = new OpenApiWalker(copy);
walker.Walk(target);
morestuff = AddReferences(copy.Components, target.Components);
} while (morestuff);
}
private static bool AddReferences(OpenApiComponents newComponents, OpenApiComponents target)
{
var moreStuff = false;
foreach (var item in newComponents.Schemas)
{
if (!target.Schemas.ContainsKey(item.Key))
{
moreStuff = true;
target.Schemas.Add(item);
}
}
foreach (var item in newComponents.Parameters)
{
if (!target.Parameters.ContainsKey(item.Key))
{
moreStuff = true;
target.Parameters.Add(item);
}
}
foreach (var item in newComponents.Responses)
{
if (!target.Responses.ContainsKey(item.Key))
{
moreStuff = true;
target.Responses.Add(item);
}
}
foreach (var item in newComponents.RequestBodies
.Where(item => !target.RequestBodies.ContainsKey(item.Key)))
{
moreStuff = true;
target.RequestBodies.Add(item);
}
foreach (var item in newComponents.Headers
.Where(item => !target.Headers.ContainsKey(item.Key)))
{
moreStuff = true;
target.Headers.Add(item);
}
foreach (var item in newComponents.Links
.Where(item => !target.Links.ContainsKey(item.Key)))
{
moreStuff = true;
target.Links.Add(item);
}
foreach (var item in newComponents.Callbacks
.Where(item => !target.Callbacks.ContainsKey(item.Key)))
{
moreStuff = true;
target.Callbacks.Add(item);
}
foreach (var item in newComponents.Examples
.Where(item => !target.Examples.ContainsKey(item.Key)))
{
moreStuff = true;
target.Examples.Add(item);
}
foreach (var item in newComponents.SecuritySchemes
.Where(item => !target.SecuritySchemes.ContainsKey(item.Key)))
{
moreStuff = true;
target.SecuritySchemes.Add(item);
}
return moreStuff;
}
private static string ExtractPath(string url, IList<OpenApiServer> serverList)
{
// if OpenAPI has servers, then see if the url matches one of them
var baseUrl = serverList.Select(s => s.Url.TrimEnd('/'))
.FirstOrDefault(c => url.Contains(c));
return baseUrl == null ?
new Uri(new(SRResource.DefaultBaseUri), url).GetComponents(UriComponents.Path | UriComponents.KeepDelimiter, UriFormat.Unescaped)
: url.Split(new[] { baseUrl }, StringSplitOptions.None)[1];
}
private static void ValidateFilters(IDictionary<string, List<string>> requestUrls, string operationIds, string tags)
{
if (requestUrls != null && (operationIds != null || tags != null))
{
throw new InvalidOperationException("Cannot filter by Postman collection and either operationIds and tags at the same time.");
}
if (!string.IsNullOrEmpty(operationIds) && !string.IsNullOrEmpty(tags))
{
throw new InvalidOperationException("Cannot specify both operationIds and tags at the same time.");
}
}
private static Func<string, OperationType?, OpenApiOperation, bool> GetOperationIdsPredicate(string operationIds)
{
if (operationIds == "*")
{
return (_, _, _) => true; // All operations
}
else
{
var operationIdsArray = operationIds.Split(',');
return (_, _, operation) => operationIdsArray.Contains(operation.OperationId);
}
}
private static Func<string, OperationType?, OpenApiOperation, bool> GetTagsPredicate(string tags)
{
var tagsArray = tags.Split(',');
if (tagsArray.Length == 1)
{
var regex = new Regex(tagsArray[0]);
return (_, _, operation) => operation.Tags.Any(tag => regex.IsMatch(tag.Name));
}
else
{
return (_, _, operation) => operation.Tags.Any(tag => tagsArray.Contains(tag.Name));
}
}
private static Func<string, OperationType?, OpenApiOperation, bool> GetRequestUrlsPredicate(Dictionary<string, List<string>> requestUrls, OpenApiDocument source)
{
var operationTypes = new List<string>();
if (source != null)
{
var apiVersion = source.Info.Version;
var sources = new Dictionary<string, OpenApiDocument> { { apiVersion, source } };
var rootNode = CreateOpenApiUrlTreeNode(sources);
// Iterate through urls dictionary and fetch operations for each url
foreach (var url in requestUrls)
{
var serverList = source.Servers;
var path = ExtractPath(url.Key, serverList);
var openApiOperations = GetOpenApiOperations(rootNode, path, apiVersion);
if (openApiOperations == null)
{
Debug.WriteLine($"The url {url.Key} could not be found in the OpenApi description");
continue;
}
operationTypes.AddRange(GetOperationTypes(openApiOperations, url.Value, path));
}
}
if (!operationTypes.Any())
{
throw new ArgumentException("The urls in the Postman collection supplied could not be found.");
}
// predicate for matching url and operationTypes
return (path, operationType, _) => operationTypes.Contains(operationType + path);
}
private static List<string> GetOperationTypes(IDictionary<OperationType, OpenApiOperation> openApiOperations, List<string> url, string path)
{
// Add the available ops if they are in the postman collection. See path.Value
return openApiOperations.Where(ops => url.Contains(ops.Key.ToString().ToUpper()))
.Select(ops => ops.Key + path)
.ToList();
}
}
}