Skip to content

Commit 46718f0

Browse files
committed
clean up and dispose stream if specified in the settings
1 parent 627d75b commit 46718f0

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
using Microsoft.OpenApi.Extensions;
1212
using Microsoft.OpenApi.Validations;
1313
using System.Linq;
14-
using Microsoft.OpenApi.Services;
1514
using Microsoft.OpenApi.Interfaces;
16-
using Microsoft.OpenApi.Reader.Services;
17-
using System.Collections.Generic;
18-
using System;
1915

2016
namespace Microsoft.OpenApi.Reader
2117
{

src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static async Task<ReadResult> LoadAsync(string url, OpenApiReaderSettings
118118
public static async Task<T> LoadAsync<T>(string url, OpenApiSpecVersion version, OpenApiReaderSettings settings = null) where T : IOpenApiElement
119119
{
120120
var result = await RetrieveStreamAndFormatAsync(url);
121-
return Load<T>(result.Item1 as MemoryStream, version, result.Item2, out var diagnostic, settings);
121+
return Load<T>(result.Item1, version, result.Item2, out var diagnostic, settings);
122122
}
123123

124124
/// <summary>
@@ -149,8 +149,18 @@ public static async Task<ReadResult> LoadAsync(Stream input, string format = nul
149149
preparedStream.Position = 0;
150150
}
151151

152-
// Use StreamReader to process the prepared stream (buffered for YAML, direct for JSON)
153-
return await InternalLoadAsync(preparedStream, format, settings, cancellationToken);
152+
try
153+
{
154+
// Use StreamReader to process the prepared stream (buffered for YAML, direct for JSON)
155+
return await InternalLoadAsync(preparedStream, format, settings, cancellationToken);
156+
}
157+
finally
158+
{
159+
if (!settings.LeaveStreamOpen)
160+
{
161+
input.Dispose();
162+
}
163+
}
154164
}
155165

156166
/// <summary>

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDiscriminatorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public async Task ParseBasicDiscriminatorShouldSucceed()
2828
// Copy stream to MemoryStream
2929
using var memoryStream = new MemoryStream();
3030
await stream.CopyToAsync(memoryStream);
31+
memoryStream.Position = 0;
3132

3233
// Act
3334
var discriminator = OpenApiModelFactory.Load<OpenApiDiscriminator>(memoryStream, OpenApiSpecVersion.OpenApi3_0, OpenApiConstants.Yaml, out var diagnostic);

0 commit comments

Comments
 (0)