Skip to content

Commit d93689c

Browse files
authored
Merge pull request #2487 from jkporter/main
fix: Fix URL processing in InternalLoad method would not detect the filename/scheme properly in some scenarios
1 parent 292b8ed commit d93689c

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
302302
Stream stream;
303303
string? format;
304304

305-
if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase)
306-
|| url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
305+
if (url.StartsWith("http:", StringComparison.OrdinalIgnoreCase)
306+
|| url.StartsWith("https:", StringComparison.OrdinalIgnoreCase))
307307
{
308308
var response = await settings.HttpClient.GetAsync(url, token).ConfigureAwait(false);
309309
var mediaType = response.Content.Headers.ContentType?.MediaType;
@@ -320,15 +320,27 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
320320
}
321321
else
322322
{
323-
format = Path.GetExtension(url).Split('.').LastOrDefault();
324-
325323
try
326324
{
327-
var fileInput = new FileInfo(url);
325+
string fileName;
326+
if (url.StartsWith("file:", StringComparison.OrdinalIgnoreCase))
327+
{
328+
fileName = new Uri(url).LocalPath;
329+
}
330+
else
331+
{
332+
fileName = url;
333+
}
334+
335+
format = Path.GetExtension(fileName).Split('.').LastOrDefault();
336+
337+
var fileInput = new FileInfo(fileName);
328338
stream = fileInput.OpenRead();
329339
}
330340
catch (Exception ex) when (
331341
ex is
342+
UriFormatException or
343+
FormatException or
332344
FileNotFoundException or
333345
PathTooLongException or
334346
DirectoryNotFoundException or

0 commit comments

Comments
 (0)