Skip to content

Commit 74d20ed

Browse files
committed
fix: add meaningful exception message during validation
1 parent 9856058 commit 74d20ed

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/Microsoft.OpenApi.Readers/OpenApiYamlReader.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using SharpYaml.Serialization;
1212
using Microsoft.OpenApi.Models;
1313
using System;
14+
using System.Linq;
1415
using System.Text;
1516

1617
namespace Microsoft.OpenApi.Readers
@@ -123,8 +124,13 @@ static JsonNode LoadJsonNodesFromYamlDocument(TextReader input)
123124
{
124125
var yamlStream = new YamlStream();
125126
yamlStream.Load(input);
126-
var yamlDocument = yamlStream.Documents[0];
127-
return yamlDocument.ToJsonNode();
127+
if (yamlStream.Documents.Any())
128+
{
129+
var yamlDocument = yamlStream.Documents[0];
130+
return yamlDocument.ToJsonNode();
131+
}
132+
133+
throw new InvalidOperationException("No documents found in the YAML stream.");
128134
}
129135
}
130136
}

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,13 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
278278
{
279279
throw new InvalidOperationException("Loading external references are not supported when using synchronous methods.");
280280
}
281+
if (input.Length == 0 || input.Position == input.Length)
282+
{
283+
throw new ArgumentException($"Cannot parse the stream: {nameof(input)} is empty or contains no elements.");
284+
}
281285

282286
var reader = OpenApiReaderRegistry.GetReader(format);
283287
var readResult = reader.Read(input, settings);
284-
285288
return readResult;
286289
}
287290

0 commit comments

Comments
 (0)