-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathOpenApiModelFactory.cs
386 lines (343 loc) · 18.2 KB
/
OpenApiModelFactory.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader.Services;
using Microsoft.OpenApi.Services;
namespace Microsoft.OpenApi.Reader
{
/// <summary>
/// A factory class for loading OpenAPI models from various sources.
/// </summary>
public static class OpenApiModelFactory
{
private static readonly HttpClient _httpClient = new();
static OpenApiModelFactory()
{
OpenApiReaderRegistry.RegisterReader(OpenApiConstants.Json, new OpenApiJsonReader());
}
/// <summary>
/// Loads the input stream and parses it into an Open API document.
/// </summary>
/// <param name="stream"> The input stream.</param>
/// <param name="settings"> The OpenApi reader settings.</param>
/// <param name="format">The OpenAPI format.</param>
/// <returns>An OpenAPI document instance.</returns>
public static ReadResult Load(MemoryStream stream,
string format = null,
OpenApiReaderSettings settings = null)
{
if (stream is null) throw new ArgumentNullException(nameof(stream));
settings ??= new OpenApiReaderSettings();
// Get the format of the stream if not provided
format ??= InspectStreamFormat(stream);
var result = InternalLoad(stream, format, settings);
if (!settings.LeaveStreamOpen)
{
stream.Dispose();
}
return result;
}
/// <summary>
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">Stream containing OpenAPI description to parse.</param>
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
/// <param name="format"></param>
/// <param name="openApiDocument">The OpenApiDocument object to which the fragment belongs, used to lookup references.</param>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <param name="settings">The OpenApiReader settings.</param>
/// <returns>Instance of newly created IOpenApiElement.</returns>
/// <returns>The OpenAPI element.</returns>
public static T Load<T>(MemoryStream input, OpenApiSpecVersion version, string format, OpenApiDocument openApiDocument, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement
{
format ??= InspectStreamFormat(input);
return OpenApiReaderRegistry.GetReader(format).ReadFragment<T>(input, version, openApiDocument, out diagnostic, settings);
}
/// <summary>
/// Loads the input URL and parses it into an Open API document.
/// </summary>
/// <param name="url">The path to the OpenAPI file</param>
/// <param name="settings"> The OpenApi reader settings.</param>
/// <param name="token"></param>
/// <returns></returns>
public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings settings = null, CancellationToken token = default)
{
var (stream, format) = await RetrieveStreamAndFormatAsync(url, token).ConfigureAwait(false);
return await LoadAsync(stream, format, settings, token).ConfigureAwait(false);
}
/// <summary>
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url">The path to the OpenAPI file</param>
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
/// <param name="settings">The OpenApiReader settings.</param>
/// <param name="openApiDocument">The OpenApiDocument object to which the fragment belongs, used to lookup references.</param>
/// <param name="token"></param>
/// <returns>Instance of newly created IOpenApiElement.</returns>
/// <returns>The OpenAPI element.</returns>
public static async Task<T> LoadAsync<T>(string url, OpenApiSpecVersion version, OpenApiDocument openApiDocument, OpenApiReaderSettings settings = null, CancellationToken token = default) where T : IOpenApiElement
{
var (stream, format) = await RetrieveStreamAndFormatAsync(url, token).ConfigureAwait(false);
return await LoadAsync<T>(stream, version, openApiDocument, format, settings, token);
}
/// <summary>
/// Loads the input stream and parses it into an Open API document. If the stream is not buffered and it contains yaml, it will be buffered before parsing.
/// </summary>
/// <param name="input">The input stream.</param>
/// <param name="settings"> The OpenApi reader settings.</param>
/// <param name="cancellationToken">Propagates notification that operations should be cancelled.</param>
/// <param name="format">The Open API format</param>
/// <returns></returns>
public static async Task<ReadResult> LoadAsync(Stream input, string format = null, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default)
{
if (input is null) throw new ArgumentNullException(nameof(input));
settings ??= new OpenApiReaderSettings();
Stream preparedStream;
if (format is null)
{
(preparedStream, format) = await PrepareStreamForReadingAsync(input, format, cancellationToken).ConfigureAwait(false);
}
else
{
preparedStream = input;
}
// Use StreamReader to process the prepared stream (buffered for YAML, direct for JSON)
using (preparedStream)
{
var result = await InternalLoadAsync(preparedStream, format, settings, cancellationToken).ConfigureAwait(false);
if (!settings.LeaveStreamOpen)
{
input.Dispose();
}
return result;
}
}
/// <summary>
/// Reads the stream input and ensures it is buffered before passing it to the Load method.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <param name="version"></param>
/// <param name="openApiDocument">The document used to lookup tag or schema references.</param>
/// <param name="format"></param>
/// <param name="settings"></param>
/// <param name="token"></param>
/// <returns></returns>
public static async Task<T> LoadAsync<T>(Stream input,
OpenApiSpecVersion version,
OpenApiDocument openApiDocument,
string format = null,
OpenApiReaderSettings settings = null,
CancellationToken token = default) where T : IOpenApiElement
{
Utils.CheckArgumentNull(openApiDocument);
if (input is null) throw new ArgumentNullException(nameof(input));
if (input is MemoryStream memoryStream)
{
return Load<T>(memoryStream, version, format, openApiDocument, out var _, settings);
}
else
{
memoryStream = new MemoryStream();
await input.CopyToAsync(memoryStream, 81920, token).ConfigureAwait(false);
memoryStream.Position = 0;
return Load<T>(memoryStream, version, format, openApiDocument, out var _, settings);
}
}
/// <summary>
/// Reads the input string and parses it into an Open API document.
/// </summary>
/// <param name="input">The input string.</param>
/// <param name="format">The Open API format</param>
/// <param name="settings">The OpenApi reader settings.</param>
/// <returns>An OpenAPI document instance.</returns>
public static ReadResult Parse(string input,
string format = null,
OpenApiReaderSettings settings = null)
{
if (input is null) throw new ArgumentNullException(nameof(input));
format ??= InspectInputFormat(input);
settings ??= new OpenApiReaderSettings();
// Copy string into MemoryStream
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
return InternalLoad(stream, format, settings);
}
/// <summary>
/// Reads the input string and parses it into an Open API document.
/// </summary>
/// <param name="input">The input string.</param>
/// <param name="version"></param>
/// <param name="openApiDocument">The OpenApiDocument object to which the fragment belongs, used to lookup references.</param>
/// <param name="diagnostic">The diagnostic entity containing information from the reading process.</param>
/// <param name="format">The Open API format</param>
/// <param name="settings">The OpenApi reader settings.</param>
/// <returns>An OpenAPI document instance.</returns>
public static T Parse<T>(string input,
OpenApiSpecVersion version,
OpenApiDocument openApiDocument,
out OpenApiDiagnostic diagnostic,
string format = null,
OpenApiReaderSettings settings = null) where T : IOpenApiElement
{
if (input is null) throw new ArgumentNullException(nameof(input));
format ??= InspectInputFormat(input);
settings ??= new OpenApiReaderSettings();
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
return Load<T>(stream, version, format, openApiDocument, out diagnostic, settings);
}
private static readonly OpenApiReaderSettings DefaultReaderSettings = new();
private static async Task<ReadResult> InternalLoadAsync(Stream input, string format, OpenApiReaderSettings settings, CancellationToken cancellationToken = default)
{
var reader = OpenApiReaderRegistry.GetReader(format);
var readResult = await reader.ReadAsync(input, settings, cancellationToken).ConfigureAwait(false);
if (settings?.LoadExternalRefs ?? DefaultReaderSettings.LoadExternalRefs)
{
var diagnosticExternalRefs = await LoadExternalRefsAsync(readResult.Document, settings, format, cancellationToken).ConfigureAwait(false);
// Merge diagnostics of external reference
if (diagnosticExternalRefs != null)
{
readResult.Diagnostic.Errors.AddRange(diagnosticExternalRefs.Errors);
readResult.Diagnostic.Warnings.AddRange(diagnosticExternalRefs.Warnings);
}
}
return readResult;
}
private static async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument document, OpenApiReaderSettings settings, string format = null, CancellationToken token = default)
{
// Create workspace for all documents to live in.
var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri);
var openApiWorkSpace = new OpenApiWorkspace(baseUrl);
// Load this root document into the workspace
var streamLoader = new DefaultStreamLoader(settings.BaseUrl);
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, settings.CustomExternalLoader ?? streamLoader, settings);
return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, format ?? OpenApiConstants.Json, null, token).ConfigureAwait(false);
}
private static ReadResult InternalLoad(MemoryStream input, string format, OpenApiReaderSettings settings)
{
if (settings?.LoadExternalRefs ?? DefaultReaderSettings.LoadExternalRefs)
{
throw new InvalidOperationException("Loading external references are not supported when using synchronous methods.");
}
var reader = OpenApiReaderRegistry.GetReader(format);
var readResult = reader.Read(input, settings);
return readResult;
}
private static async Task<(Stream, string)> RetrieveStreamAndFormatAsync(string url, CancellationToken token = default)
{
if (!string.IsNullOrEmpty(url))
{
Stream stream;
string format;
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase)
|| url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
var response = await _httpClient.GetAsync(url, token).ConfigureAwait(false);
var mediaType = response.Content.Headers.ContentType.MediaType;
var contentType = mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0];
format = contentType.Split('/').LastOrDefault();
#if NETSTANDARD2_0
stream = await response.Content.ReadAsStreamAsync();
#else
stream = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false);;
#endif
return (stream, format);
}
else
{
format = Path.GetExtension(url).Split('.').LastOrDefault();
try
{
var fileInput = new FileInfo(url);
stream = fileInput.OpenRead();
}
catch (Exception ex) when (
ex is
FileNotFoundException or
PathTooLongException or
DirectoryNotFoundException or
IOException or
UnauthorizedAccessException or
SecurityException or
NotSupportedException)
{
throw new InvalidOperationException($"Could not open the file at {url}", ex);
}
return (stream, format);
}
}
return (null, null);
}
private static string InspectInputFormat(string input)
{
return input.StartsWith("{", StringComparison.OrdinalIgnoreCase) || input.StartsWith("[", StringComparison.OrdinalIgnoreCase) ? OpenApiConstants.Json : OpenApiConstants.Yaml;
}
private static string InspectStreamFormat(Stream stream)
{
if (stream == null) throw new ArgumentNullException(nameof(stream));
long initialPosition = stream.Position;
int firstByte = stream.ReadByte();
// Skip whitespace if present and read the next non-whitespace byte
if (char.IsWhiteSpace((char)firstByte))
{
firstByte = stream.ReadByte();
}
stream.Position = initialPosition; // Reset the stream position to the beginning
char firstChar = (char)firstByte;
return firstChar switch
{
'{' or '[' => OpenApiConstants.Json, // If the first character is '{' or '[', assume JSON
_ => OpenApiConstants.Yaml // Otherwise assume YAML
};
}
private static async Task<(Stream, string)> PrepareStreamForReadingAsync(Stream input, string format, CancellationToken token = default)
{
Stream preparedStream = input;
if (!input.CanSeek)
{
// Use a temporary buffer to read a small portion for format detection
using var bufferStream = new MemoryStream();
await input.CopyToAsync(bufferStream, 1024, token).ConfigureAwait(false);
bufferStream.Position = 0;
// Inspect the format from the buffered portion
format ??= InspectStreamFormat(bufferStream);
// If format is JSON, no need to buffer further — use the original stream.
if (format.Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase))
{
preparedStream = input;
}
else
{
// YAML or other non-JSON format; copy remaining input to a new stream.
preparedStream = new MemoryStream();
bufferStream.Position = 0;
await bufferStream.CopyToAsync(preparedStream, 81920, token).ConfigureAwait(false); // Copy buffered portion
await input.CopyToAsync(preparedStream, 81920, token).ConfigureAwait(false); // Copy remaining data
preparedStream.Position = 0;
}
}
else
{
format ??= InspectStreamFormat(input);
if (!format.Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase))
{
// Buffer stream for non-JSON formats (e.g., YAML) since they require synchronous reading
preparedStream = new MemoryStream();
await input.CopyToAsync(preparedStream, 81920, token).ConfigureAwait(false);
preparedStream.Position = 0;
}
}
return (preparedStream, format);
}
}
}