@@ -39,7 +39,11 @@ public static ReadResult Load(MemoryStream stream,
39
39
string format = null ,
40
40
OpenApiReaderSettings settings = null )
41
41
{
42
+ #if NET6_0_OR_GREATER
43
+ ArgumentNullException . ThrowIfNull ( stream ) ;
44
+ #else
42
45
if ( stream is null ) throw new ArgumentNullException ( nameof ( stream ) ) ;
46
+ #endif
43
47
settings ??= new OpenApiReaderSettings ( ) ;
44
48
45
49
// Get the format of the stream if not provided
@@ -112,7 +116,11 @@ public static async Task<T> LoadAsync<T>(string url, OpenApiSpecVersion version,
112
116
/// <returns></returns>
113
117
public static async Task < ReadResult > LoadAsync ( Stream input , string format = null , OpenApiReaderSettings settings = null , CancellationToken cancellationToken = default )
114
118
{
119
+ #if NET6_0_OR_GREATER
120
+ ArgumentNullException . ThrowIfNull ( input ) ;
121
+ #else
115
122
if ( input is null ) throw new ArgumentNullException ( nameof ( input ) ) ;
123
+ #endif
116
124
settings ??= new OpenApiReaderSettings ( ) ;
117
125
118
126
Stream preparedStream ;
@@ -160,7 +168,11 @@ public static async Task<T> LoadAsync<T>(Stream input,
160
168
CancellationToken token = default ) where T : IOpenApiElement
161
169
{
162
170
Utils . CheckArgumentNull ( openApiDocument ) ;
171
+ #if NET6_0_OR_GREATER
172
+ ArgumentNullException . ThrowIfNull ( input ) ;
173
+ #else
163
174
if ( input is null ) throw new ArgumentNullException ( nameof ( input ) ) ;
175
+ #endif
164
176
if ( input is MemoryStream memoryStream )
165
177
{
166
178
return Load < T > ( memoryStream , version , format , openApiDocument , out var _ , settings ) ;
@@ -185,7 +197,11 @@ public static ReadResult Parse(string input,
185
197
string format = null ,
186
198
OpenApiReaderSettings settings = null )
187
199
{
188
- if ( input is null ) throw new ArgumentNullException ( nameof ( input ) ) ;
200
+ #if NET6_0_OR_GREATER
201
+ ArgumentException . ThrowIfNullOrEmpty ( input ) ;
202
+ #else
203
+ if ( string . IsNullOrEmpty ( input ) ) throw new ArgumentNullException ( nameof ( input ) ) ;
204
+ #endif
189
205
format ??= InspectInputFormat ( input ) ;
190
206
settings ??= new OpenApiReaderSettings ( ) ;
191
207
@@ -212,7 +228,11 @@ public static T Parse<T>(string input,
212
228
string format = null ,
213
229
OpenApiReaderSettings settings = null ) where T : IOpenApiElement
214
230
{
215
- if ( input is null ) throw new ArgumentNullException ( nameof ( input ) ) ;
231
+ #if NET6_0_OR_GREATER
232
+ ArgumentException . ThrowIfNullOrEmpty ( input ) ;
233
+ #else
234
+ if ( string . IsNullOrEmpty ( input ) ) throw new ArgumentNullException ( nameof ( input ) ) ;
235
+ #endif
216
236
format ??= InspectInputFormat ( input ) ;
217
237
settings ??= new OpenApiReaderSettings ( ) ;
218
238
using var stream = new MemoryStream ( Encoding . UTF8 . GetBytes ( input ) ) ;
@@ -278,11 +298,12 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
278
298
var response = await _httpClient . GetAsync ( url , token ) . ConfigureAwait ( false ) ;
279
299
var mediaType = response . Content . Headers . ContentType . MediaType ;
280
300
var contentType = mediaType . Split ( ";" . ToCharArray ( ) , StringSplitOptions . RemoveEmptyEntries ) [ 0 ] ;
281
- format = contentType . Split ( '/' ) . LastOrDefault ( ) ;
301
+ format = contentType . Split ( '/' ) . Last ( ) . Split ( '+' ) . Last ( ) . Split ( '-' ) . Last ( ) ;
302
+ // for non-standard MIME types e.g. text/x-yaml used in older libs or apps
282
303
#if NETSTANDARD2_0
283
304
stream = await response . Content . ReadAsStreamAsync ( ) ;
284
305
#else
285
- stream = await response . Content . ReadAsStreamAsync ( token ) . ConfigureAwait ( false ) ; ;
306
+ stream = await response . Content . ReadAsStreamAsync ( token ) . ConfigureAwait ( false ) ;
286
307
#endif
287
308
return ( stream , format ) ;
288
309
}
@@ -321,8 +342,12 @@ private static string InspectInputFormat(string input)
321
342
322
343
private static string InspectStreamFormat ( Stream stream )
323
344
{
324
- if ( stream == null ) throw new ArgumentNullException ( nameof ( stream ) ) ;
325
-
345
+ #if NET6_0_OR_GREATER
346
+ ArgumentNullException . ThrowIfNull ( stream ) ;
347
+ #else
348
+ if ( stream is null ) throw new ArgumentNullException ( nameof ( stream ) ) ;
349
+ #endif
350
+
326
351
long initialPosition = stream . Position ;
327
352
int firstByte = stream . ReadByte ( ) ;
328
353
0 commit comments