Skip to content

Commit

Permalink
Support non-standard MIME type during format inference
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Feb 5, 2025
1 parent 878dd08 commit bd9f810
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit bd9f810

Please sign in to comment.