Skip to content

Commit

Permalink
Merge pull request #1581 from microsoft/mk/fix-stream-leave-open
Browse files Browse the repository at this point in the history
Fix OpenApiStreamReader.ReadAsync doesn't respect "leave open" setting
  • Loading branch information
MaggieKimani1 authored Mar 6, 2024
2 parents e65b484 + 9036a21 commit 2f64478
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task<ReadResult> ReadAsync(Stream input, CancellationToken cancella
bufferedStream.Position = 0;
}

using var reader = new StreamReader(bufferedStream);
using var reader = new StreamReader(bufferedStream, default, true, -1, _settings.LeaveStreamOpen);
return await new OpenApiTextReaderReader(_settings).ReadAsync(reader, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,21 @@ public void StreamShouldNotCloseIfLeaveStreamOpenSettingEqualsTrue()
reader.Read(stream, out _);
Assert.True(stream.CanRead);
}

[Fact]
public async void StreamShouldNotBeDisposedIfLeaveStreamOpenSettingIsTrue()
{
var memoryStream = new MemoryStream();
using var fileStream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml"));

await fileStream.CopyToAsync(memoryStream);
memoryStream.Position = 0;
var stream = memoryStream;

var reader = new OpenApiStreamReader(new() { LeaveStreamOpen = true });
_ = await reader.ReadAsync(stream);
stream.Seek(0, SeekOrigin.Begin); // does not throw an object disposed exception
Assert.True(stream.CanRead);
}
}
}

0 comments on commit 2f64478

Please sign in to comment.