Skip to content

Commit 4f0a022

Browse files
Copilotbaywet
andauthored
fix(reader): return diagnostic for empty streams
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
1 parent f07d8a2 commit 4f0a022

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ public static ReadResult Parse(string input,
212212
OpenApiReaderSettings? settings = null)
213213
{
214214
#if NET6_0_OR_GREATER
215-
ArgumentNullException.ThrowIfNull(input);
215+
ArgumentException.ThrowIfNullOrEmpty(input);
216216
#else
217-
if (input is null) throw new ArgumentNullException(nameof(input));
217+
if (string.IsNullOrEmpty(input)) throw new ArgumentNullException(nameof(input));
218218
#endif
219219
format ??= InspectInputFormat(input);
220220
settings ??= new OpenApiReaderSettings();
@@ -246,9 +246,9 @@ public static ReadResult Parse(string input,
246246
OpenApiReaderSettings? settings = null) where T : IOpenApiElement
247247
{
248248
#if NET6_0_OR_GREATER
249-
ArgumentNullException.ThrowIfNull(input);
249+
ArgumentException.ThrowIfNullOrEmpty(input);
250250
#else
251-
if (input is null) throw new ArgumentNullException(nameof(input));
251+
if (string.IsNullOrEmpty(input)) throw new ArgumentNullException(nameof(input));
252252
#endif
253253
format ??= InspectInputFormat(input);
254254
settings ??= new OpenApiReaderSettings();
@@ -299,6 +299,20 @@ private static ReadResult InternalLoad(MemoryStream input, string format, OpenAp
299299
{
300300
throw new InvalidOperationException("Loading external references are not supported when using synchronous methods.");
301301
}
302+
if (input.Length == 0 || input.Position == input.Length)
303+
{
304+
var diagnostic = new OpenApiDiagnostic
305+
{
306+
Format = format,
307+
};
308+
diagnostic.Errors.Add(new OpenApiError(null, $"Cannot parse the stream: {nameof(input)} is empty or contains no elements."));
309+
310+
return new()
311+
{
312+
Document = null,
313+
Diagnostic = diagnostic,
314+
};
315+
}
302316
var location = new Uri(OpenApiConstants.BaseRegistryUri);
303317
var reader = settings.GetReader(format);
304318
var readResult = reader.Read(input, location, settings);

0 commit comments

Comments
 (0)