Skip to content

Commit c9b01cb

Browse files
committed
Moved load external references
1 parent a648591 commit c9b01cb

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,6 @@ public T ReadFragment<T>(JsonNode input,
214214
return (T)element;
215215
}
216216

217-
private async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null)
218-
{
219-
// Create workspace for all documents to live in.
220-
var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri);
221-
var openApiWorkSpace = new OpenApiWorkspace(baseUrl);
222-
223-
// Load this root document into the workspace
224-
var streamLoader = new DefaultStreamLoader(settings.BaseUrl);
225-
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, settings.CustomExternalLoader ?? streamLoader, settings);
226-
return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, format ?? OpenApiConstants.Json, null, cancellationToken);
227-
}
217+
228218
}
229219
}

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using System.Threading.Tasks;
1313
using Microsoft.OpenApi.Interfaces;
1414
using Microsoft.OpenApi.Models;
15+
using Microsoft.OpenApi.Reader.Services;
16+
using Microsoft.OpenApi.Services;
1517

1618
namespace Microsoft.OpenApi.Reader
1719
{
@@ -71,7 +73,16 @@ public static ReadResult Load(MemoryStream stream,
7173
/// <param name="settings"> The OpenApi reader settings.</param>
7274
/// <returns></returns>
7375
public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings settings = null)
74-
{
76+
{
77+
// If url is HTTP
78+
// Get the response object.
79+
// Select format based on MediaType
80+
// Get the stream from the response object.
81+
// Load the stream.
82+
// Else
83+
// Determine the format from the file extension.
84+
// Load the file from the local file system.
85+
7586
var format = GetFormat(url);
7687
var stream = await GetStreamAsync(url); // Get response back and then get Content
7788
return await LoadAsync(stream, format, settings);
@@ -134,14 +145,45 @@ private static async Task<ReadResult> InternalLoadAsync(Stream input, string for
134145
Utils.CheckArgumentNull(format, nameof(format));
135146
var reader = OpenApiReaderRegistry.GetReader(format);
136147
var readResult = await reader.ReadAsync(input, settings, cancellationToken);
148+
149+
if (settings.LoadExternalRefs)
150+
{
151+
var diagnosticExternalRefs = await LoadExternalRefsAsync(readResult.OpenApiDocument, cancellationToken, settings, format);
152+
// Merge diagnostics of external reference
153+
if (diagnosticExternalRefs != null)
154+
{
155+
readResult.OpenApiDiagnostic.Errors.AddRange(diagnosticExternalRefs.Errors);
156+
readResult.OpenApiDiagnostic.Warnings.AddRange(diagnosticExternalRefs.Warnings);
157+
}
158+
}
159+
160+
137161
return readResult;
138162
}
139163

164+
private static async Task<OpenApiDiagnostic> LoadExternalRefsAsync(OpenApiDocument document, CancellationToken cancellationToken, OpenApiReaderSettings settings, string format = null)
165+
{
166+
// Create workspace for all documents to live in.
167+
var baseUrl = settings.BaseUrl ?? new Uri(OpenApiConstants.BaseRegistryUri);
168+
var openApiWorkSpace = new OpenApiWorkspace(baseUrl);
169+
170+
// Load this root document into the workspace
171+
var streamLoader = new DefaultStreamLoader(settings.BaseUrl);
172+
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, settings.CustomExternalLoader ?? streamLoader, settings);
173+
return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, format ?? OpenApiConstants.Json, null, cancellationToken);
174+
}
175+
140176
private static ReadResult InternalLoad(MemoryStream input, string format, OpenApiReaderSettings settings = null)
141177
{
142178
Utils.CheckArgumentNull(format, nameof(format));
179+
if (settings.LoadExternalRefs)
180+
{
181+
throw new InvalidOperationException("Loading external references are not supported when using synchronous methods.");
182+
}
183+
143184
var reader = OpenApiReaderRegistry.GetReader(format);
144185
var readResult = reader.Read(input, settings);
186+
145187
return readResult;
146188
}
147189

0 commit comments

Comments
 (0)