Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private static async Task<OpenApiDocument> GetOpenApiAsync(HidiOptions options,
else if (!string.IsNullOrEmpty(options.OpenApi))
{
stream = await GetStreamAsync(options.OpenApi, logger, cancellationToken).ConfigureAwait(false);
var result = await ParseOpenApiAsync(options.OpenApi, format, options.InlineExternal, logger, stream, cancellationToken).ConfigureAwait(false);
var result = await ParseOpenApiAsync(options.OpenApi, options.InlineExternal, logger, stream, cancellationToken).ConfigureAwait(false);
document = result.Document;
}
else throw new InvalidOperationException("No input file path or URL provided");
Expand Down Expand Up @@ -351,8 +351,7 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe
try
{
using var stream = await GetStreamAsync(openApi, logger, cancellationToken).ConfigureAwait(false);
var openApiFormat = !string.IsNullOrEmpty(openApi) ? GetOpenApiFormat(openApi, logger) : OpenApiFormat.Yaml;
result = await ParseOpenApiAsync(openApi, openApiFormat.GetDisplayName(),false, logger, stream, cancellationToken).ConfigureAwait(false);
result = await ParseOpenApiAsync(openApi, false, logger, stream, cancellationToken).ConfigureAwait(false);

using (logger.BeginScope("Calculating statistics"))
{
Expand Down Expand Up @@ -380,7 +379,7 @@ private static MemoryStream ApplyFilterToCsdl(Stream csdlStream, string entitySe
return result.Diagnostic.Errors.Count == 0;
}

private static async Task<ReadResult> ParseOpenApiAsync(string openApiFile, string format, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default)
private static async Task<ReadResult> ParseOpenApiAsync(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default)
{
ReadResult result;
var stopwatch = Stopwatch.StartNew();
Expand All @@ -396,7 +395,7 @@ private static async Task<ReadResult> ParseOpenApiAsync(string openApiFile, stri
new Uri("file://" + new FileInfo(openApiFile).DirectoryName + Path.DirectorySeparatorChar)
};

result = await OpenApiDocument.LoadAsync(stream, format, settings, cancellationToken).ConfigureAwait(false);
result = await OpenApiDocument.LoadAsync(stream, settings: settings, cancellationToken: cancellationToken).ConfigureAwait(false);

logger.LogTrace("{Timestamp}ms: Completed parsing.", stopwatch.ElapsedMilliseconds);

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
/// </summary>
private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version, Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
Utils.CheckArgumentNull(writer);;
Utils.CheckArgumentNull(writer);

writer.WriteStartObject();

Expand Down
37 changes: 31 additions & 6 deletions src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public static ReadResult Load(MemoryStream stream,
string format = null,
OpenApiReaderSettings settings = null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null) throw new ArgumentNullException(nameof(stream));
#endif
settings ??= new OpenApiReaderSettings();

// Get the format of the stream if not provided
Expand Down Expand Up @@ -112,7 +116,11 @@ public static async Task<T> LoadAsync<T>(string url, OpenApiSpecVersion version,
/// <returns></returns>
public static async Task<ReadResult> LoadAsync(Stream input, string format = null, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(input);
#else
if (input is null) throw new ArgumentNullException(nameof(input));
#endif
settings ??= new OpenApiReaderSettings();

Stream preparedStream;
Expand Down Expand Up @@ -160,7 +168,11 @@ public static async Task<T> LoadAsync<T>(Stream input,
CancellationToken token = default) where T : IOpenApiElement
{
Utils.CheckArgumentNull(openApiDocument);
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(input);
#else
if (input is null) throw new ArgumentNullException(nameof(input));
#endif
if (input is MemoryStream memoryStream)
{
return Load<T>(memoryStream, version, format, openApiDocument, out var _, settings);
Expand All @@ -185,7 +197,11 @@ public static ReadResult Parse(string input,
string format = null,
OpenApiReaderSettings settings = null)
{
if (input is null) throw new ArgumentNullException(nameof(input));
#if NET6_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(input);
#else
if (string.IsNullOrEmpty(input)) throw new ArgumentNullException(nameof(input));
#endif
format ??= InspectInputFormat(input);
settings ??= new OpenApiReaderSettings();

Expand All @@ -212,7 +228,11 @@ public static T Parse<T>(string input,
string format = null,
OpenApiReaderSettings settings = null) where T : IOpenApiElement
{
if (input is null) throw new ArgumentNullException(nameof(input));
#if NET6_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(input);
#else
if (string.IsNullOrEmpty(input)) throw new ArgumentNullException(nameof(input));
#endif
format ??= InspectInputFormat(input);
settings ??= new OpenApiReaderSettings();
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
Expand Down Expand Up @@ -278,11 +298,12 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
var response = await _httpClient.GetAsync(url, token).ConfigureAwait(false);
var mediaType = response.Content.Headers.ContentType.MediaType;
var contentType = mediaType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0];
format = contentType.Split('/').LastOrDefault();
format = contentType.Split('/').Last().Split('+').Last().Split('-').Last();
// 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 Expand Up @@ -321,8 +342,12 @@ private static string InspectInputFormat(string input)

private static string InspectStreamFormat(Stream stream)
{
if (stream == null) throw new ArgumentNullException(nameof(stream));

#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null) throw new ArgumentNullException(nameof(stream));
#endif

long initialPosition = stream.Position;
int firstByte = stream.ReadByte();

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);
}
}
}
Loading