Skip to content

Commit 62c0de4

Browse files
committed
chore: addresses review feedback
1 parent a46ac25 commit 62c0de4

3 files changed

Lines changed: 39 additions & 13 deletions

File tree

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,9 @@ public static async Task<ReadResult> LoadAsync(Stream stream, string? format = n
782782
/// <param name="settings"></param>
783783
/// <returns></returns>
784784
/// <remarks>
785-
/// OpenAPI semantic and parser errors are returned in the <see cref="ReadResult.Diagnostic"/>; a null or empty input string throws <see cref="ArgumentException"/>.
785+
/// OpenAPI semantic errors and parser errors that can be represented in the <see cref="ReadResult.Diagnostic"/> are returned.
786+
/// Parser failures that occur before a result can be created may throw.
787+
/// <see cref="ArgumentException"/> is thrown when <paramref name="input"/> is null or empty before parsing starts.
786788
/// </remarks>
787789
public static ReadResult Parse(string input,
788790
string? format = null,

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ public static ReadResult Parse(string input,
238238
/// <param name="settings">The OpenApi reader settings.</param>
239239
/// <returns>An OpenAPI document instance.</returns>
240240
/// <remarks>
241-
/// OpenAPI semantic and parser errors are returned in the <paramref name="diagnostic"/>.
241+
/// OpenAPI semantic errors and parser errors that can be represented in the <paramref name="diagnostic"/> are returned.
242+
/// Parser failures that occur before a diagnostic can be created may throw.
242243
/// <see cref="ArgumentException"/> is thrown when <paramref name="input"/> is null or empty before parsing starts.
243244
/// </remarks>
244245
public static T? Parse<T>(string input,
@@ -264,6 +265,11 @@ public static ReadResult Parse(string input,
264265
private static async Task<ReadResult> InternalLoadAsync(Stream input, string format, OpenApiReaderSettings settings, CancellationToken cancellationToken = default)
265266
{
266267
settings ??= DefaultReaderSettings.Value;
268+
if (input.CanSeek && (input.Length == 0 || input.Position == input.Length))
269+
{
270+
return CreateEmptyStreamReadResult(format);
271+
}
272+
267273
var reader = settings.GetReader(format);
268274
var location =
269275
(input is FileStream fileStream ? new Uri(fileStream.Name) : null) ??
@@ -304,24 +310,29 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
304310
}
305311
if (input.Length == 0 || input.Position == input.Length)
306312
{
307-
var diagnostic = new OpenApiDiagnostic
308-
{
309-
Format = format,
310-
};
311-
diagnostic.Errors.Add(new OpenApiError(null, $"Cannot parse the stream: {nameof(input)} is empty or contains no elements."));
312-
313-
return new()
314-
{
315-
Document = null,
316-
Diagnostic = diagnostic,
317-
};
313+
return CreateEmptyStreamReadResult(format);
318314
}
319315
var location = new Uri(OpenApiConstants.BaseRegistryUri);
320316
var reader = settings.GetReader(format);
321317
var readResult = reader.Read(input, location, settings);
322318
return readResult;
323319
}
324320

321+
private static ReadResult CreateEmptyStreamReadResult(string format)
322+
{
323+
var diagnostic = new OpenApiDiagnostic
324+
{
325+
Format = format,
326+
};
327+
diagnostic.Errors.Add(new OpenApiError(null, "Cannot parse the stream: input is empty or contains no elements."));
328+
329+
return new()
330+
{
331+
Document = null,
332+
Diagnostic = diagnostic,
333+
};
334+
}
335+
325336
private static async Task<(Stream, string?)> RetrieveStreamAndFormatAsync(string url, OpenApiReaderSettings settings, CancellationToken token = default)
326337
{
327338
if (string.IsNullOrEmpty(url))

test/Microsoft.OpenApi.Tests/Reader/OpenApiModelFactoryTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public void LoadReturnsDiagnosticWhenStreamIsEmpty()
2525
Assert.Equal(OpenApiConstants.Yaml, result.Diagnostic.Format);
2626
}
2727

28+
[Fact]
29+
public async Task LoadAsyncReturnsDiagnosticWhenStreamIsEmpty()
30+
{
31+
await using var stream = new MemoryStream();
32+
33+
var result = await OpenApiDocument.LoadAsync(stream, cancellationToken: TestContext.Current.CancellationToken);
34+
35+
Assert.Null(result.Document);
36+
var error = Assert.Single(result.Diagnostic.Errors);
37+
Assert.Equal("Cannot parse the stream: input is empty or contains no elements.", error.Message);
38+
Assert.Equal(OpenApiConstants.Yaml, result.Diagnostic.Format);
39+
}
40+
2841
[Fact]
2942
public void ParseThrowsWhenInputIsEmpty()
3043
{

0 commit comments

Comments
 (0)