|
12 | 12 | using System.Threading.Tasks; |
13 | 13 | using Microsoft.OpenApi.Interfaces; |
14 | 14 | using Microsoft.OpenApi.Models; |
| 15 | +using Microsoft.OpenApi.Reader.Services; |
| 16 | +using Microsoft.OpenApi.Services; |
15 | 17 |
|
16 | 18 | namespace Microsoft.OpenApi.Reader |
17 | 19 | { |
@@ -71,7 +73,16 @@ public static ReadResult Load(MemoryStream stream, |
71 | 73 | /// <param name="settings"> The OpenApi reader settings.</param> |
72 | 74 | /// <returns></returns> |
73 | 75 | 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 | + |
75 | 86 | var format = GetFormat(url); |
76 | 87 | var stream = await GetStreamAsync(url); // Get response back and then get Content |
77 | 88 | return await LoadAsync(stream, format, settings); |
@@ -134,14 +145,45 @@ private static async Task<ReadResult> InternalLoadAsync(Stream input, string for |
134 | 145 | Utils.CheckArgumentNull(format, nameof(format)); |
135 | 146 | var reader = OpenApiReaderRegistry.GetReader(format); |
136 | 147 | 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 | + |
137 | 161 | return readResult; |
138 | 162 | } |
139 | 163 |
|
| 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 | + |
140 | 176 | private static ReadResult InternalLoad(MemoryStream input, string format, OpenApiReaderSettings settings = null) |
141 | 177 | { |
142 | 178 | 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 | + |
143 | 184 | var reader = OpenApiReaderRegistry.GetReader(format); |
144 | 185 | var readResult = reader.Read(input, settings); |
| 186 | + |
145 | 187 | return readResult; |
146 | 188 | } |
147 | 189 |
|
|
0 commit comments