From 9a47cb2198e2c433f7c4842fa1421c3cb90950c3 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 6 Mar 2024 12:53:50 +0300 Subject: [PATCH 1/3] Pass the leave open setting to the StreamReader constructor to prevent the stream from getting disposed if asked to leave open --- src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs b/src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs index e96aa4b12..d1515bc56 100644 --- a/src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs +++ b/src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs @@ -73,7 +73,7 @@ public async Task ReadAsync(Stream input, CancellationToken cancella bufferedStream.Position = 0; } - using var reader = new StreamReader(bufferedStream); + using var reader = new StreamReader(bufferedStream, default, true, -1, leaveOpen: _settings.LeaveStreamOpen); return await new OpenApiTextReaderReader(_settings).ReadAsync(reader, cancellationToken); } From 9de2aecdd7f7315c29dc2c9b54fba5211bdfc326 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 6 Mar 2024 12:54:05 +0300 Subject: [PATCH 2/3] Add test to validate --- .../OpenApiStreamReaderTests.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs index 91e271549..d3b19eff1 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/OpenApiReaderTests/OpenApiStreamReaderTests.cs @@ -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); + } } } From 9036a21eb1a079b583f0aeeacc906db1c58b5330 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Wed, 6 Mar 2024 13:00:48 +0300 Subject: [PATCH 3/3] Remove unnecessary param name --- src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs b/src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs index d1515bc56..ae01833fb 100644 --- a/src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs +++ b/src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs @@ -73,7 +73,7 @@ public async Task ReadAsync(Stream input, CancellationToken cancella bufferedStream.Position = 0; } - using var reader = new StreamReader(bufferedStream, default, true, -1, leaveOpen: _settings.LeaveStreamOpen); + using var reader = new StreamReader(bufferedStream, default, true, -1, _settings.LeaveStreamOpen); return await new OpenApiTextReaderReader(_settings).ReadAsync(reader, cancellationToken); }