Skip to content

Commit

Permalink
- adds missing cancellation token parameter and passes it along
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Feb 20, 2023
1 parent 116aca8 commit 622a6e2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
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
8 changes: 5 additions & 3 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,13 @@ 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);
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

0 comments on commit 622a6e2

Please sign in to comment.