44using System ;
55using System . IO ;
66using System . Net . Http ;
7+ using System . Runtime . InteropServices ;
8+ using System . Threading ;
79using System . Threading . Tasks ;
810using Microsoft . OpenApi . Interfaces ;
911using Microsoft . OpenApi . Models ;
@@ -27,28 +29,29 @@ public DefaultStreamLoader(Uri baseUrl)
2729 this . baseUrl = baseUrl ;
2830 }
2931
30- /// <summary>
31- /// Use Uri to locate data and convert into an input object.
32- /// </summary>
33- /// <param name="uri">Identifier of some source of an OpenAPI Description</param>
34- /// <returns>A data object that can be processed by a reader to generate an <see cref="OpenApiDocument"/></returns>
35- /// <exception cref="ArgumentException"></exception>
36- public async Task < Stream > LoadAsync ( Uri uri )
32+ /// <inheritdoc/>
33+ public async Task < Stream > LoadAsync ( Uri uri , CancellationToken cancellationToken = default )
3734 {
38- Uri absoluteUri ;
39- absoluteUri = baseUrl . AbsoluteUri . Equals ( OpenApiConstants . BaseRegistryUri ) ? new Uri ( Directory . GetCurrentDirectory ( ) + uri )
40- : new Uri ( baseUrl , uri ) ;
35+ var absoluteUri = ( baseUrl . AbsoluteUri . Equals ( OpenApiConstants . BaseRegistryUri ) , baseUrl . IsAbsoluteUri , uri . IsAbsoluteUri ) switch
36+ {
37+ ( true , _ , _ ) => new Uri ( Path . Combine ( Directory . GetCurrentDirectory ( ) , uri . ToString ( ) ) ) ,
38+ // this overcomes a URI concatenation issue for local paths on linux OSes
39+ ( _, true , false ) when baseUrl . Scheme . Equals ( "file" , StringComparison . OrdinalIgnoreCase ) && ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) =>
40+ new Uri ( Path . Combine ( baseUrl . AbsoluteUri , uri . ToString ( ) ) ) ,
41+ ( _, _, _) => new Uri ( baseUrl , uri ) ,
42+ } ;
4143
42- switch ( absoluteUri . Scheme )
44+ return absoluteUri . Scheme switch
4345 {
44- case "file" :
45- return File . OpenRead ( absoluteUri . AbsolutePath ) ;
46- case "http" :
47- case "https" :
48- return await _httpClient . GetStreamAsync ( absoluteUri ) ;
49- default :
50- throw new ArgumentException ( "Unsupported scheme" ) ;
51- }
46+ "file" => File . OpenRead ( absoluteUri . AbsolutePath ) ,
47+ "http" or "https" =>
48+ #if NET5_0_OR_GREATER
49+ await _httpClient . GetStreamAsync ( absoluteUri , cancellationToken ) . ConfigureAwait ( false ) ,
50+ #else
51+ await _httpClient . GetStreamAsync ( absoluteUri ) . ConfigureAwait ( false ) ,
52+ #endif
53+ _ => throw new ArgumentException( "Unsupported scheme" ) ,
54+ } ;
5255 }
5356 }
5457}
0 commit comments