Skip to content

Commit e958896

Browse files
committed
If the input stream is JSON, read directly from stream, otherwise buffer it into memory
1 parent 7261ad9 commit e958896

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,27 @@ public static async Task<ReadResult> LoadAsync(Stream input, string format, Open
9898
Utils.CheckArgumentNull(format, nameof(format));
9999
settings ??= new OpenApiReaderSettings();
100100

101-
MemoryStream bufferedStream;
102-
if (input is MemoryStream stream)
101+
Stream preparedStream;
102+
103+
// Avoid buffering for JSON format
104+
if (input is MemoryStream || format.Equals(OpenApiConstants.Json, StringComparison.OrdinalIgnoreCase))
103105
{
104-
bufferedStream = stream;
106+
preparedStream = input;
105107
}
106108
else
107109
{
108-
// Buffer stream so that OpenApiTextReaderReader can process it synchronously
109-
// YamlDocument doesn't support async reading.
110-
bufferedStream = new MemoryStream();
111-
await input.CopyToAsync(bufferedStream, 81920, cancellationToken);
112-
bufferedStream.Position = 0;
110+
// Buffer stream for non-JSON formats (e.g., YAML) since they require synchronous reading
111+
preparedStream = new MemoryStream();
112+
await input.CopyToAsync(preparedStream, 81920, cancellationToken);
113+
preparedStream.Position = 0;
113114
}
114115

115-
using var reader = new StreamReader(bufferedStream, default, true, -1, settings.LeaveStreamOpen);
116+
// Use StreamReader to process the prepared stream (buffered for YAML, direct for JSON)
117+
using var reader = new StreamReader(preparedStream, default, true, -1, settings.LeaveStreamOpen);
116118
return await LoadAsync(reader, format, settings, cancellationToken);
117119
}
118120

121+
119122
/// <summary>
120123
/// Loads the TextReader input and parses it into an Open API document.
121124
/// </summary>

0 commit comments

Comments
 (0)