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: 7 additions & 2 deletions src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SharpYaml.Serialization;
using Microsoft.OpenApi.Models;
using System;
using System.Linq;
using System.Text;

namespace Microsoft.OpenApi.Readers
Expand Down Expand Up @@ -123,8 +124,12 @@ static JsonNode LoadJsonNodesFromYamlDocument(TextReader input)
{
var yamlStream = new YamlStream();
yamlStream.Load(input);
var yamlDocument = yamlStream.Documents[0];
return yamlDocument.ToJsonNode();
if (yamlStream.Documents.Any())
{
return yamlStream.Documents[0].ToJsonNode();
}

throw new InvalidOperationException("No documents found in the YAML stream.");
}
}
}
5 changes: 4 additions & 1 deletion src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,13 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
{
throw new InvalidOperationException("Loading external references are not supported when using synchronous methods.");
}
if (input.Length == 0 || input.Position == input.Length)
{
throw new ArgumentException($"Cannot parse the stream: {nameof(input)} is empty or contains no elements.");
}

var reader = OpenApiReaderRegistry.GetReader(format);
var readResult = reader.Read(input, settings);

return readResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
using Microsoft.OpenApi.Writers;
using Xunit;
using VerifyXunit;
using VerifyTests;
using Microsoft.OpenApi.Models.Interfaces;
using System;

namespace Microsoft.OpenApi.Readers.Tests.V31Tests
{
Expand Down Expand Up @@ -539,5 +539,11 @@ public async Task ParseDocumentWith31PropertiesWorks()
// Assert
await Verifier.Verify(actual);
}

[Fact]
public void ParseEmptyMemoryStreamThrowsAnArgumentException()
{
Assert.Throws<ArgumentException>(() => OpenApiDocument.Load(new MemoryStream()));
}
}
}