Skip to content

Adds setting to configure leaving stream open #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// Licensed under the MIT license.

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
Expand Down Expand Up @@ -61,11 +61,17 @@ public class OpenApiReaderSettings
public Uri BaseUrl { get; set; }

/// <summary>
/// Function used to provide an alternative loader for accessing external references.
/// Function used to provide an alternative loader for accessing external references.
/// </summary>
/// <remarks>
/// Default loader will attempt to dereference http(s) urls and file urls.
/// </remarks>
public IStreamLoader CustomExternalLoader { get; set; }

/// <summary>
/// Whether to leave the <see cref="Stream"/> object open after reading
/// from an <see cref="OpenApiStreamReader"/> object.
/// </summary>
public bool LeaveStreamOpen { get; set; }
}
}
18 changes: 11 additions & 7 deletions src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// Licensed under the MIT license.

using System.IO;
using System.Threading.Tasks;
Expand Down Expand Up @@ -29,14 +29,18 @@ public OpenApiStreamReader(OpenApiReaderSettings settings = null)
/// Reads the stream input and parses it into an Open API document.
/// </summary>
/// <param name="input">Stream containing OpenAPI description to parse.</param>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing</param>
/// <returns>Instance of newly created OpenApiDocument</returns>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <returns>Instance of newly created OpenApiDocument.</returns>
public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
{
using (var reader = new StreamReader(input))
var reader = new StreamReader(input);
var result = new OpenApiTextReaderReader(_settings).Read(reader, out diagnostic);
if (!_settings.LeaveStreamOpen)
{
return new OpenApiTextReaderReader(_settings).Read(reader, out diagnostic);
reader.Dispose();
}

return result;
}

/// <summary>
Expand All @@ -50,8 +54,8 @@ public async Task<ReadResult> ReadAsync(Stream input)
if (input is MemoryStream)
{
bufferedStream = (MemoryStream)input;
}
else
}
else
{
// Buffer stream so that OpenApiTextReaderReader can process it synchronously
// YamlDocument doesn't support async reading.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.IO;
using Xunit;

namespace Microsoft.OpenApi.Readers.Tests.OpenApiReaderTests
{
public class OpenApiStreamReaderTests
{
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDocument/";

[Fact]
public void StreamShouldCloseIfLeaveStreamOpenSettingEqualsFalse()
{
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml")))
{
var reader = new OpenApiStreamReader(new OpenApiReaderSettings { LeaveStreamOpen = false });
reader.Read(stream, out _);
Assert.False(stream.CanRead);
}
}

[Fact]
public void StreamShouldNotCloseIfLeaveStreamOpenSettingEqualsTrue()
{
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "petStore.yaml")))
{
var reader = new OpenApiStreamReader(new OpenApiReaderSettings { LeaveStreamOpen = true});
reader.Read(stream, out _);
Assert.True(stream.CanRead);
}
}
}
}