diff --git a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs index 33bea8fb5..bcf1df8ea 100644 --- a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs +++ b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs @@ -279,10 +279,14 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp var mediaType = response.Content.Headers.ContentType.MediaType; var contentType = mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0]; format = contentType.Split('/').LastOrDefault(); + if (!string.IsNullOrEmpty(format) && format.Contains('-')) + { + format = format.Split('-').LastOrDefault(); // for non-standard MIME types e.g. text/x-yaml used in older libs or apps + } #if NETSTANDARD2_0 stream = await response.Content.ReadAsStreamAsync(); #else - stream = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false);; + stream = await response.Content.ReadAsStreamAsync(token).ConfigureAwait(false); #endif return (stream, format); } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs index e61ee8fbc..ea287db5e 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs @@ -26,6 +26,7 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests public class OpenApiDocumentTests { private const string SampleFolderPath = "V3Tests/Samples/OpenApiDocument/"; + private const string codacyApi = "https://api.codacy.com/api/api-docs/swagger.yaml"; public OpenApiDocumentTests() { @@ -1362,5 +1363,13 @@ public async Task ParseDocumentWithExampleReferencesPasses() var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExampleReferences.yaml")); Assert.Empty(result.Diagnostic.Errors); } + + [Fact] + public async Task ParseDocumentWithNonStandardMIMETypePasses() + { + // Act & Assert: Ensure NotSupportedException is not thrown for non-standard MIME type: text/x-yaml + var result = await OpenApiDocument.LoadAsync(codacyApi); + Assert.NotNull(result.Document); + } } }