Skip to content

Commit 8b4fd00

Browse files
committed
fix: a bug where the input stream would be disposed not matter what
Signed-off-by: Vincent Biret <[email protected]>
1 parent 240fb54 commit 8b4fd00

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,31 @@ public static async Task<ReadResult> LoadAsync(Stream input, string? format = nu
118118
#endif
119119
settings ??= new OpenApiReaderSettings();
120120

121-
Stream preparedStream;
121+
Stream? preparedStream = null;
122122
if (format is null)
123123
{
124124
(preparedStream, format) = await PrepareStreamForReadingAsync(input, format, cancellationToken).ConfigureAwait(false);
125125
}
126-
else
127-
{
128-
preparedStream = input;
129-
}
130126

131127
// Use StreamReader to process the prepared stream (buffered for YAML, direct for JSON)
132-
using (preparedStream)
128+
var result = await InternalLoadAsync(preparedStream ?? input, format, settings, cancellationToken).ConfigureAwait(false);
129+
if (!settings.LeaveStreamOpen)
133130
{
134-
var result = await InternalLoadAsync(preparedStream, format, settings, cancellationToken).ConfigureAwait(false);
135-
if (!settings.LeaveStreamOpen)
136-
{
137131
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP || NET5_0_OR_GREATER
138-
await input.DisposeAsync().ConfigureAwait(false);
132+
await input.DisposeAsync().ConfigureAwait(false);
139133
#else
140-
input.Dispose();
134+
input.Dispose();
141135
#endif
142-
}
143-
return result;
144136
}
137+
if (preparedStream is not null && preparedStream != input)
138+
{
139+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP || NET5_0_OR_GREATER
140+
await preparedStream.DisposeAsync().ConfigureAwait(false);
141+
#else
142+
preparedStream.Dispose();
143+
#endif
144+
}
145+
return result;
145146
}
146147

147148
/// <summary>

0 commit comments

Comments
 (0)