Skip to content

Commit

Permalink
Code cleanup and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Dec 20, 2024
1 parent 66cc6e0 commit 47dca99
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface IOpenApiReader
/// <summary>
/// Reads the MemoryStream and parses the fragment of an OpenAPI description into an Open API Element.
/// </summary>
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
/// <param name="input">Memory stream containing OpenAPI description to parse.</param>
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <param name="settings">The OpenApiReader settings.</param>
Expand All @@ -53,7 +53,7 @@ public interface IOpenApiReader
/// <summary>
/// Reads the JsonNode input and parses the fragment of an OpenAPI description into an Open API Element.
/// </summary>
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
/// <param name="input">Memory stream containing OpenAPI description to parse.</param>
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <param name="settings">The OpenApiReader settings.</param>
Expand Down
62 changes: 30 additions & 32 deletions src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
Expand Down Expand Up @@ -54,36 +54,6 @@ public static ReadResult Load(MemoryStream stream,
return result;
}

/// <summary>
/// Reads the stream input and ensures it is buffered before passing it to the Load method.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <param name="version"></param>
/// <param name="format"></param>
/// <param name="diagnostic"></param>
/// <param name="settings"></param>
/// <returns></returns>
public static T Load<T>(Stream input,
OpenApiSpecVersion version,
out OpenApiDiagnostic diagnostic,
string format = null,
OpenApiReaderSettings settings = null) where T : IOpenApiElement
{
if (input is null) throw new ArgumentNullException(nameof(input));
if (input is MemoryStream memoryStream)
{
return Load<T>(memoryStream, version, format, out diagnostic, settings);
}
else
{
memoryStream = new MemoryStream();
input.CopyTo(memoryStream);
memoryStream.Position = 0;
return Load<T>(memoryStream, version, format, out diagnostic, settings);
}
}

/// <summary>
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
/// </summary>
Expand Down Expand Up @@ -127,7 +97,7 @@ public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings
public static async Task<T> LoadAsync<T>(string url, OpenApiSpecVersion version, OpenApiReaderSettings settings = null, CancellationToken token = default) where T : IOpenApiElement
{
var result = await RetrieveStreamAndFormatAsync(url, token).ConfigureAwait(false);
return Load<T>(result.Item1, version, out var _, result.Item2, settings);
return await LoadAsync<T>(result.Item1, version, result.Item2, settings);
}

/// <summary>
Expand Down Expand Up @@ -165,6 +135,34 @@ public static async Task<ReadResult> LoadAsync(Stream input, string format = nul
return result;
}

/// <summary>
/// Reads the stream input and ensures it is buffered before passing it to the Load method.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <param name="version"></param>
/// <param name="format"></param>
/// <param name="settings"></param>
/// <returns></returns>
public static async Task<T> LoadAsync<T>(Stream input,
OpenApiSpecVersion version,
string format = null,
OpenApiReaderSettings settings = null) where T : IOpenApiElement
{
if (input is null) throw new ArgumentNullException(nameof(input));
if (input is MemoryStream memoryStream)
{
return Load<T>(memoryStream, version, format, out var _, settings);
}
else
{
memoryStream = new MemoryStream();
await input.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
return Load<T>(memoryStream, version, format, out var _, settings);
}
}

/// <summary>
/// Reads the input string and parses it into an Open API document.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public async Task ParseBasicEncodingShouldSucceed()
}

[Fact]
public void ParseAdvancedEncodingShouldSucceed()
public async Task ParseAdvancedEncodingShouldSucceed()
{
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedEncoding.yaml"));

// Act
var encoding = OpenApiModelFactory.Load<OpenApiEncoding>(stream, OpenApiSpecVersion.OpenApi3_0, out _);
var encoding = await OpenApiModelFactory.LoadAsync<OpenApiEncoding>(stream, OpenApiSpecVersion.OpenApi3_0);

// Assert
encoding.Should().BeEquivalentTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ public async Task ParseBasicInfoShouldSucceed()
}

[Fact]
public void ParseMinimalInfoShouldSucceed()
public async Task ParseMinimalInfoShouldSucceed()
{
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalInfo.yaml"));

// Act
var openApiInfo = OpenApiModelFactory.Load<OpenApiInfo>(stream, OpenApiSpecVersion.OpenApi3_0, out _, "yaml");
var openApiInfo = await OpenApiModelFactory.LoadAsync<OpenApiInfo>(stream, OpenApiSpecVersion.OpenApi3_0);

// Assert
openApiInfo.Should().BeEquivalentTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public OpenApiParameterTests()
}

[Fact]
public void ParsePathParameterShouldSucceed()
public async Task ParsePathParameterShouldSucceed()
{
// Arrange
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "pathParameter.yaml"));

// Act
var parameter = OpenApiModelFactory.Load<OpenApiParameter>(stream, OpenApiSpecVersion.OpenApi3_0, out _, "yaml");
var parameter = await OpenApiModelFactory.LoadAsync<OpenApiParameter>(stream, OpenApiSpecVersion.OpenApi3_0);

// Assert
parameter.Should().BeEquivalentTo(
Expand Down Expand Up @@ -101,13 +101,13 @@ public async Task ParseQueryParameterWithObjectTypeShouldSucceed()
}

[Fact]
public void ParseQueryParameterWithObjectTypeAndContentShouldSucceed()
public async Task ParseQueryParameterWithObjectTypeAndContentShouldSucceed()
{
// Arrange
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "queryParameterWithObjectTypeAndContent.yaml"));

// Act
var parameter = OpenApiModelFactory.Load<OpenApiParameter>(stream, OpenApiSpecVersion.OpenApi3_0, out _, "yaml");
var parameter = await OpenApiModelFactory.LoadAsync<OpenApiParameter>(stream, OpenApiSpecVersion.OpenApi3_0);

// Assert
parameter.Should().BeEquivalentTo(
Expand Down Expand Up @@ -194,13 +194,13 @@ public async Task ParseParameterWithNullLocationShouldSucceed()
}

[Fact]
public void ParseParameterWithNoLocationShouldSucceed()
public async Task ParseParameterWithNoLocationShouldSucceed()
{
// Arrange
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "parameterWithNoLocation.yaml"));

// Act
var parameter = OpenApiModelFactory.Load<OpenApiParameter>(stream, OpenApiSpecVersion.OpenApi3_0, out _);
var parameter = await OpenApiModelFactory.LoadAsync<OpenApiParameter>(stream, OpenApiSpecVersion.OpenApi3_0);

// Assert
parameter.Should().BeEquivalentTo(
Expand All @@ -218,13 +218,13 @@ public void ParseParameterWithNoLocationShouldSucceed()
}

[Fact]
public void ParseParameterWithUnknownLocationShouldSucceed()
public async Task ParseParameterWithUnknownLocationShouldSucceed()
{
// Arrange
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "parameterWithUnknownLocation.yaml"));

// Act
var parameter = OpenApiModelFactory.Load<OpenApiParameter>(stream, OpenApiSpecVersion.OpenApi3_0, out _);
var parameter = await OpenApiModelFactory.LoadAsync<OpenApiParameter>(stream, OpenApiSpecVersion.OpenApi3_0);

// Assert
parameter.Should().BeEquivalentTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader;
Expand All @@ -21,10 +22,10 @@ public OpenApiXmlTests()
}

[Fact]
public void ParseBasicXmlShouldSucceed()
public async Task ParseBasicXmlShouldSucceed()
{
// Act
var xml = OpenApiModelFactory.Load<OpenApiXml>(Resources.GetStream(Path.Combine(SampleFolderPath, "basicXml.yaml")), OpenApiSpecVersion.OpenApi3_0, out _);
var xml = await OpenApiModelFactory.LoadAsync<OpenApiXml>(Resources.GetStream(Path.Combine(SampleFolderPath, "basicXml.yaml")), OpenApiSpecVersion.OpenApi3_0);

// Assert
xml.Should().BeEquivalentTo(
Expand Down

0 comments on commit 47dca99

Please sign in to comment.