Skip to content

Commit d95c74e

Browse files
authored
Fix: Try read relative uri's as files.
Signed-off-by: Alex Wichmann <VisualBean@users.noreply.github.com>
1 parent e9666d0 commit d95c74e

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/ByteBard.AsyncAPI.Readers/Services/DefaultStreamLoader.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,50 @@ public Stream Load(Uri uri)
1515
{
1616
try
1717
{
18-
switch (uri.Scheme)
18+
if (uri.IsAbsoluteUri && !string.IsNullOrEmpty(uri.Scheme))
1919
{
20-
case "file":
21-
return File.OpenRead(uri.AbsolutePath);
22-
case "http":
23-
case "https":
24-
return HttpClient.GetStreamAsync(uri).GetAwaiter().GetResult();
25-
default:
26-
throw new ArgumentException("Unsupported scheme");
20+
switch (uri.Scheme.ToLowerInvariant())
21+
{
22+
case "file":
23+
return File.OpenRead(uri.AbsolutePath);
24+
case "http":
25+
case "https":
26+
return HttpClient.GetStreamAsync(uri).GetAwaiter().GetResult();
27+
default:
28+
throw new ArgumentException("Unsupported scheme");
29+
}
30+
}
31+
else
32+
{
33+
return File.OpenRead(uri.OriginalString);
2734
}
2835
}
2936
catch (Exception ex)
3037
{
31-
throw new AsyncApiReaderException($"Something went wrong trying to fetch '{uri.OriginalString}. {ex.Message}'", ex);
38+
throw new AsyncApiReaderException($"Something went wrong trying to fetch '{uri.OriginalString}'. {ex.Message}", ex);
3239
}
3340
}
3441

3542
public async Task<Stream> LoadAsync(Uri uri)
3643
{
3744
try
3845
{
39-
switch (uri.Scheme)
46+
if (uri.IsAbsoluteUri && !string.IsNullOrEmpty(uri.Scheme))
47+
{
48+
switch (uri.Scheme.ToLowerInvariant())
49+
{
50+
case "file":
51+
return File.OpenRead(uri.AbsolutePath);
52+
case "http":
53+
case "https":
54+
return HttpClient.GetStreamAsync(uri).GetAwaiter().GetResult();
55+
default:
56+
throw new ArgumentException("Unsupported scheme");
57+
}
58+
}
59+
else
4060
{
41-
case "file":
42-
return File.OpenRead(uri.AbsolutePath);
43-
case "http":
44-
case "https":
45-
return await HttpClient.GetStreamAsync(uri);
46-
default:
47-
throw new ArgumentException("Unsupported scheme");
61+
return File.OpenRead(uri.OriginalString);
4862
}
4963
}
5064
catch (Exception ex)
@@ -53,4 +67,4 @@ public async Task<Stream> LoadAsync(Uri uri)
5367
}
5468
}
5569
}
56-
}
70+
}

0 commit comments

Comments
 (0)