Skip to content

Commit 47dca99

Browse files
committed
Code cleanup and fix tests
1 parent 66cc6e0 commit 47dca99

File tree

6 files changed

+47
-48
lines changed

6 files changed

+47
-48
lines changed

src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public interface IOpenApiReader
4343
/// <summary>
4444
/// Reads the MemoryStream and parses the fragment of an OpenAPI description into an Open API Element.
4545
/// </summary>
46-
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
46+
/// <param name="input">Memory stream containing OpenAPI description to parse.</param>
4747
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
4848
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
4949
/// <param name="settings">The OpenApiReader settings.</param>
@@ -53,7 +53,7 @@ public interface IOpenApiReader
5353
/// <summary>
5454
/// Reads the JsonNode input and parses the fragment of an OpenAPI description into an Open API Element.
5555
/// </summary>
56-
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
56+
/// <param name="input">Memory stream containing OpenAPI description to parse.</param>
5757
/// <param name="version">Version of the OpenAPI specification that the fragment conforms to.</param>
5858
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
5959
/// <param name="settings">The OpenApiReader settings.</param>

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -54,36 +54,6 @@ public static ReadResult Load(MemoryStream stream,
5454
return result;
5555
}
5656

57-
/// <summary>
58-
/// Reads the stream input and ensures it is buffered before passing it to the Load method.
59-
/// </summary>
60-
/// <typeparam name="T"></typeparam>
61-
/// <param name="input"></param>
62-
/// <param name="version"></param>
63-
/// <param name="format"></param>
64-
/// <param name="diagnostic"></param>
65-
/// <param name="settings"></param>
66-
/// <returns></returns>
67-
public static T Load<T>(Stream input,
68-
OpenApiSpecVersion version,
69-
out OpenApiDiagnostic diagnostic,
70-
string format = null,
71-
OpenApiReaderSettings settings = null) where T : IOpenApiElement
72-
{
73-
if (input is null) throw new ArgumentNullException(nameof(input));
74-
if (input is MemoryStream memoryStream)
75-
{
76-
return Load<T>(memoryStream, version, format, out diagnostic, settings);
77-
}
78-
else
79-
{
80-
memoryStream = new MemoryStream();
81-
input.CopyTo(memoryStream);
82-
memoryStream.Position = 0;
83-
return Load<T>(memoryStream, version, format, out diagnostic, settings);
84-
}
85-
}
86-
8757
/// <summary>
8858
/// Reads the stream input and parses the fragment of an OpenAPI description into an Open API Element.
8959
/// </summary>
@@ -127,7 +97,7 @@ public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings
12797
public static async Task<T> LoadAsync<T>(string url, OpenApiSpecVersion version, OpenApiReaderSettings settings = null, CancellationToken token = default) where T : IOpenApiElement
12898
{
12999
var result = await RetrieveStreamAndFormatAsync(url, token).ConfigureAwait(false);
130-
return Load<T>(result.Item1, version, out var _, result.Item2, settings);
100+
return await LoadAsync<T>(result.Item1, version, result.Item2, settings);
131101
}
132102

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

138+
/// <summary>
139+
/// Reads the stream input and ensures it is buffered before passing it to the Load method.
140+
/// </summary>
141+
/// <typeparam name="T"></typeparam>
142+
/// <param name="input"></param>
143+
/// <param name="version"></param>
144+
/// <param name="format"></param>
145+
/// <param name="settings"></param>
146+
/// <returns></returns>
147+
public static async Task<T> LoadAsync<T>(Stream input,
148+
OpenApiSpecVersion version,
149+
string format = null,
150+
OpenApiReaderSettings settings = null) where T : IOpenApiElement
151+
{
152+
if (input is null) throw new ArgumentNullException(nameof(input));
153+
if (input is MemoryStream memoryStream)
154+
{
155+
return Load<T>(memoryStream, version, format, out var _, settings);
156+
}
157+
else
158+
{
159+
memoryStream = new MemoryStream();
160+
await input.CopyToAsync(memoryStream).ConfigureAwait(false);
161+
memoryStream.Position = 0;
162+
return Load<T>(memoryStream, version, format, out var _, settings);
163+
}
164+
}
165+
168166
/// <summary>
169167
/// Reads the input string and parses it into an Open API document.
170168
/// </summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public async Task ParseBasicEncodingShouldSucceed()
3535
}
3636

3737
[Fact]
38-
public void ParseAdvancedEncodingShouldSucceed()
38+
public async Task ParseAdvancedEncodingShouldSucceed()
3939
{
4040
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "advancedEncoding.yaml"));
4141

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

4545
// Assert
4646
encoding.Should().BeEquivalentTo(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ public async Task ParseBasicInfoShouldSucceed()
109109
}
110110

111111
[Fact]
112-
public void ParseMinimalInfoShouldSucceed()
112+
public async Task ParseMinimalInfoShouldSucceed()
113113
{
114114
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "minimalInfo.yaml"));
115115

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

119119
// Assert
120120
openApiInfo.Should().BeEquivalentTo(

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public OpenApiParameterTests()
2626
}
2727

2828
[Fact]
29-
public void ParsePathParameterShouldSucceed()
29+
public async Task ParsePathParameterShouldSucceed()
3030
{
3131
// Arrange
3232
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "pathParameter.yaml"));
3333

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

3737
// Assert
3838
parameter.Should().BeEquivalentTo(
@@ -101,13 +101,13 @@ public async Task ParseQueryParameterWithObjectTypeShouldSucceed()
101101
}
102102

103103
[Fact]
104-
public void ParseQueryParameterWithObjectTypeAndContentShouldSucceed()
104+
public async Task ParseQueryParameterWithObjectTypeAndContentShouldSucceed()
105105
{
106106
// Arrange
107107
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "queryParameterWithObjectTypeAndContent.yaml"));
108108

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

112112
// Assert
113113
parameter.Should().BeEquivalentTo(
@@ -194,13 +194,13 @@ public async Task ParseParameterWithNullLocationShouldSucceed()
194194
}
195195

196196
[Fact]
197-
public void ParseParameterWithNoLocationShouldSucceed()
197+
public async Task ParseParameterWithNoLocationShouldSucceed()
198198
{
199199
// Arrange
200200
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "parameterWithNoLocation.yaml"));
201201

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

205205
// Assert
206206
parameter.Should().BeEquivalentTo(
@@ -218,13 +218,13 @@ public void ParseParameterWithNoLocationShouldSucceed()
218218
}
219219

220220
[Fact]
221-
public void ParseParameterWithUnknownLocationShouldSucceed()
221+
public async Task ParseParameterWithUnknownLocationShouldSucceed()
222222
{
223223
// Arrange
224224
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "parameterWithUnknownLocation.yaml"));
225225

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

229229
// Assert
230230
parameter.Should().BeEquivalentTo(

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.IO;
6+
using System.Threading.Tasks;
67
using FluentAssertions;
78
using Microsoft.OpenApi.Models;
89
using Microsoft.OpenApi.Reader;
@@ -21,10 +22,10 @@ public OpenApiXmlTests()
2122
}
2223

2324
[Fact]
24-
public void ParseBasicXmlShouldSucceed()
25+
public async Task ParseBasicXmlShouldSucceed()
2526
{
2627
// Act
27-
var xml = OpenApiModelFactory.Load<OpenApiXml>(Resources.GetStream(Path.Combine(SampleFolderPath, "basicXml.yaml")), OpenApiSpecVersion.OpenApi3_0, out _);
28+
var xml = await OpenApiModelFactory.LoadAsync<OpenApiXml>(Resources.GetStream(Path.Combine(SampleFolderPath, "basicXml.yaml")), OpenApiSpecVersion.OpenApi3_0);
2829

2930
// Assert
3031
xml.Should().BeEquivalentTo(

0 commit comments

Comments
 (0)