Skip to content
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

Master refresh #1170

Merged
merged 10 commits into from
Feb 21, 2023
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.14.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.14.1" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.2.0" />
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
</ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csd
else
{
stream = await GetStream(openapi, logger, cancellationToken);
var result = await ParseOpenApi(openapi, inlineExternal, logger, stream);
var result = await ParseOpenApi(openapi, inlineExternal, logger, stream, cancellationToken);
document = result.OpenApiDocument;
}

Expand Down Expand Up @@ -253,7 +253,7 @@ public static async Task ValidateOpenApiDocument(
{
using var stream = await GetStream(openapi, logger, cancellationToken);

var result = await ParseOpenApi(openapi, false, logger, stream);
var result = await ParseOpenApi(openapi, false, logger, stream, cancellationToken);

using (logger.BeginScope("Calculating statistics"))
{
Expand All @@ -275,7 +275,7 @@ public static async Task ValidateOpenApiDocument(
}
}

private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream)
private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken)
{
ReadResult result;
Stopwatch stopwatch = Stopwatch.StartNew();
Expand All @@ -290,7 +290,7 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
new Uri(openApiFile) :
new Uri("file://" + new FileInfo(openApiFile).DirectoryName + Path.DirectorySeparatorChar)
}
).ReadAsync(stream);
).ReadAsync(stream, cancellationToken);

logger.LogTrace("{timestamp}ms: Completed parsing.", stopwatch.ElapsedMilliseconds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi.Readers</Title>
<PackageId>Microsoft.OpenApi.Readers</PackageId>
<Version>1.6.1</Version>
<Version>1.6.2</Version>
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
13 changes: 8 additions & 5 deletions src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -54,8 +55,9 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
/// 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="cancellationToken">Cancellation token.</param>
/// <returns>Instance result containing newly created OpenApiDocument and diagnostics object from the process</returns>
public async Task<ReadResult> ReadAsync(Stream input)
public async Task<ReadResult> ReadAsync(Stream input, CancellationToken cancellationToken = default)
{
MemoryStream bufferedStream;
if (input is MemoryStream)
Expand All @@ -67,13 +69,14 @@ public async Task<ReadResult> ReadAsync(Stream input)
// Buffer stream so that OpenApiTextReaderReader can process it synchronously
// YamlDocument doesn't support async reading.
bufferedStream = new MemoryStream();
await input.CopyToAsync(bufferedStream);
await input.CopyToAsync(bufferedStream, 81920, cancellationToken);
bufferedStream.Position = 0;
}

var reader = new StreamReader(bufferedStream);

return await new OpenApiTextReaderReader(_settings).ReadAsync(reader);
using (var reader = new StreamReader(bufferedStream))
{
return await new OpenApiTextReaderReader(_settings).ReadAsync(reader, cancellationToken);
}
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.OpenApi.Readers/OpenApiTextReaderReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -57,8 +58,9 @@ public OpenApiDocument Read(TextReader input, out OpenApiDiagnostic diagnostic)
/// Reads the content of the TextReader. If there are references to external documents then they will be read asynchronously.
/// </summary>
/// <param name="input">TextReader containing OpenAPI description to parse.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A ReadResult instance that contains the resulting OpenApiDocument and a diagnostics instance.</returns>
public async Task<ReadResult> ReadAsync(TextReader input)
public async Task<ReadResult> ReadAsync(TextReader input, CancellationToken cancellationToken = default)
{
YamlDocument yamlDocument;

Expand All @@ -78,7 +80,7 @@ public async Task<ReadResult> ReadAsync(TextReader input)
};
}

return await new OpenApiYamlDocumentReader(this._settings).ReadAsync(yamlDocument);
return await new OpenApiYamlDocumentReader(this._settings).ReadAsync(yamlDocument, cancellationToken);
}


Expand Down
9 changes: 5 additions & 4 deletions src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Extensions;
Expand Down Expand Up @@ -84,7 +85,7 @@ public OpenApiDocument Read(YamlDocument input, out OpenApiDiagnostic diagnostic
return document;
}

public async Task<ReadResult> ReadAsync(YamlDocument input)
public async Task<ReadResult> ReadAsync(YamlDocument input, CancellationToken cancellationToken = default)
{
var diagnostic = new OpenApiDiagnostic();
var context = new ParsingContext(diagnostic)
Expand All @@ -101,7 +102,7 @@ public async Task<ReadResult> ReadAsync(YamlDocument input)

if (_settings.LoadExternalRefs)
{
await LoadExternalRefs(document);
await LoadExternalRefs(document, cancellationToken);
}

ResolveReferences(diagnostic, document);
Expand Down Expand Up @@ -132,15 +133,15 @@ public async Task<ReadResult> ReadAsync(YamlDocument input)
};
}

private async Task LoadExternalRefs(OpenApiDocument document)
private async Task LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken)
{
// Create workspace for all documents to live in.
var openApiWorkSpace = new OpenApiWorkspace();

// Load this root document into the workspace
var streamLoader = new DefaultStreamLoader(_settings.BaseUrl);
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, _settings.CustomExternalLoader ?? streamLoader, _settings);
await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document);
await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, cancellationToken);
}

private void ResolveReferences(OpenApiDiagnostic diagnostic, OpenApiDocument document)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.Interface;
using Microsoft.OpenApi.Services;
using SharpYaml.Model;

namespace Microsoft.OpenApi.Readers.Services
{
Expand All @@ -24,7 +24,7 @@ public OpenApiWorkspaceLoader(OpenApiWorkspace workspace, IStreamLoader loader,
_readerSettings = readerSettings;
}

internal async Task LoadAsync(OpenApiReference reference, OpenApiDocument document)
internal async Task LoadAsync(OpenApiReference reference, OpenApiDocument document, CancellationToken cancellationToken)
{
_workspace.AddDocument(reference.ExternalResource, document);
document.Workspace = _workspace;
Expand All @@ -43,8 +43,8 @@ internal async Task LoadAsync(OpenApiReference reference, OpenApiDocument docume
if (!_workspace.Contains(item.ExternalResource))
{
var input = await _loader.LoadAsync(new Uri(item.ExternalResource, UriKind.RelativeOrAbsolute));
var result = await reader.ReadAsync(input); // TODO merge _diagnositics
await LoadAsync(item, result.OpenApiDocument);
var result = await reader.ReadAsync(input, cancellationToken); // TODO merge diagnostics
await LoadAsync(item, result.OpenApiDocument, cancellationToken);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi</Title>
<PackageId>Microsoft.OpenApi</PackageId>
<Version>1.6.1</Version>
<Version>1.6.2</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="FluentAssertions" Version="6.9.0">
<PackageReference Include="FluentAssertions" Version="6.10.0">
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2">
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="SharpYaml" Version="2.1.0" />
<PackageReference Include="Verify.Xunit" Version="19.9.2" />
<PackageReference Include="Verify.Xunit" Version="19.9.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down