Skip to content

Commit bd9f810

Browse files
committed
Support non-standard MIME type during format inference
1 parent 878dd08 commit bd9f810

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,14 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
279279
var mediaType = response.Content.Headers.ContentType.MediaType;
280280
var contentType = mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0];
281281
format = contentType.Split('/').LastOrDefault();
282+
if (!string.IsNullOrEmpty(format) && format.Contains('-'))
283+
{
284+
format = format.Split('-').LastOrDefault(); // for non-standard MIME types e.g. text/x-yaml used in older libs or apps
285+
}
282286
#if NETSTANDARD2_0
283287
stream = await response.Content.ReadAsStreamAsync();
284288
#else
285-
stream = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false);;
289+
stream = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false);
286290
#endif
287291
return (stream, format);
288292
}

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
2626
public class OpenApiDocumentTests
2727
{
2828
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDocument/";
29+
private const string codacyApi = "https://api.codacy.com/api/api-docs/swagger.yaml";
2930

3031
public OpenApiDocumentTests()
3132
{
@@ -1362,5 +1363,13 @@ public async Task ParseDocumentWithExampleReferencesPasses()
13621363
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExampleReferences.yaml"));
13631364
Assert.Empty(result.Diagnostic.Errors);
13641365
}
1366+
1367+
[Fact]
1368+
public async Task ParseDocumentWithNonStandardMIMETypePasses()
1369+
{
1370+
// Act & Assert: Ensure NotSupportedException is not thrown for non-standard MIME type: text/x-yaml
1371+
var result = await OpenApiDocument.LoadAsync(codacyApi);
1372+
Assert.NotNull(result.Document);
1373+
}
13651374
}
13661375
}

0 commit comments

Comments
 (0)