Skip to content

Commit

Permalink
fix: a bug where external reference loading for local files would not…
Browse files Browse the repository at this point in the history
… work on linux

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
  • Loading branch information
baywet committed Feb 14, 2025
1 parent a5ffab1 commit df99a00
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/Microsoft.OpenApi/Reader/Services/DefaultStreamLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,26 @@ public DefaultStreamLoader(Uri baseUrl)
/// <inheritdoc/>
public async Task<Stream> LoadAsync(Uri uri, CancellationToken cancellationToken = default)
{
Uri absoluteUri;
absoluteUri = baseUrl.AbsoluteUri.Equals(OpenApiConstants.BaseRegistryUri) ? new Uri(Directory.GetCurrentDirectory() + uri)
: new Uri(baseUrl, uri);
var absoluteUri = (baseUrl.AbsoluteUri.Equals(OpenApiConstants.BaseRegistryUri), baseUrl.IsAbsoluteUri, uri.IsAbsoluteUri) switch
{
(true, _, _) => new Uri(Path.Combine(Directory.GetCurrentDirectory(), uri.ToString())),
// this overcomes a URI concatenation issue for local paths on linux OSes
(_, true, false) when baseUrl.Scheme.Equals("file", StringComparison.OrdinalIgnoreCase) =>
new Uri(Path.Combine(baseUrl.AbsoluteUri, uri.ToString())),
(_, _, _) => new Uri(baseUrl, uri),
};

switch (absoluteUri.Scheme)
return absoluteUri.Scheme switch
{
case "file":
return File.OpenRead(absoluteUri.AbsolutePath);
case "http":
case "https":
"file" => File.OpenRead(absoluteUri.AbsolutePath),
"http" or "https" =>
#if NET5_0_OR_GREATER
return await _httpClient.GetStreamAsync(absoluteUri, cancellationToken).ConfigureAwait(false);
await _httpClient.GetStreamAsync(absoluteUri, cancellationToken).ConfigureAwait(false),
#else
return await _httpClient.GetStreamAsync(absoluteUri).ConfigureAwait(false);
await _httpClient.GetStreamAsync(absoluteUri).ConfigureAwait(false),
#endif
default:
throw new ArgumentException("Unsupported scheme");
}
_ => throw new ArgumentException("Unsupported scheme"),
};
}
}
}

0 comments on commit df99a00

Please sign in to comment.