forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiWorkspaceLoader.cs
More file actions
67 lines (59 loc) · 2.92 KB
/
OpenApiWorkspaceLoader.cs
File metadata and controls
67 lines (59 loc) · 2.92 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
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Services;
namespace Microsoft.OpenApi.Reader.Services
{
internal class OpenApiWorkspaceLoader
{
private readonly OpenApiWorkspace _workspace;
private readonly IStreamLoader _loader;
private readonly OpenApiReaderSettings _readerSettings;
public OpenApiWorkspaceLoader(OpenApiWorkspace workspace, IStreamLoader loader, OpenApiReaderSettings readerSettings)
{
_workspace = workspace;
_loader = loader;
_readerSettings = readerSettings;
}
internal async Task<OpenApiDiagnostic> LoadAsync(OpenApiReference reference,
OpenApiDocument document,
string format = null,
OpenApiDiagnostic diagnostic = null,
CancellationToken cancellationToken = default)
{
_workspace.AddDocumentId(reference.ExternalResource, document.BaseUri);
var version = diagnostic?.SpecificationVersion ?? OpenApiSpecVersion.OpenApi3_0;
_workspace.RegisterComponents(document);
document.Workspace = _workspace;
// Collect remote references by walking document
var referenceCollector = new OpenApiRemoteReferenceCollector();
var collectorWalker = new OpenApiWalker(referenceCollector);
collectorWalker.Walk(document);
diagnostic ??= new() { SpecificationVersion = version };
// Walk references
foreach (var item in referenceCollector.References)
{
// If not already in workspace, load it and process references
if (!_workspace.Contains(item.ExternalResource))
{
var uri = new Uri(item.ExternalResource, UriKind.RelativeOrAbsolute);
var input = await _loader.LoadAsync(item.HostDocument.BaseUri, uri, cancellationToken).ConfigureAwait(false);
var result = await OpenApiDocument.LoadAsync(input, format, _readerSettings, cancellationToken).ConfigureAwait(false);
// Merge diagnostics
if (result.Diagnostic != null)
{
diagnostic.AppendDiagnostic(result.Diagnostic, item.ExternalResource);
}
if (result.Document != null)
{
var loadDiagnostic = await LoadAsync(item, result.Document, format, diagnostic, cancellationToken).ConfigureAwait(false);
diagnostic = loadDiagnostic;
}
}
}
return diagnostic;
}
}
}