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

cleanup CancellationToken #1379

Merged
merged 5 commits into from
Oct 2, 2023
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
22 changes: 11 additions & 11 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal static class OpenApiService
/// <summary>
/// Implementation of the transform command
/// </summary>
public static async Task TransformOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken)
public static async Task TransformOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(options.OpenApi) && string.IsNullOrEmpty(options.Csdl) && string.IsNullOrEmpty(options.FilterOptions?.FilterByApiManifest))
{
Expand Down Expand Up @@ -85,7 +85,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
}

// Load OpenAPI document
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion).ConfigureAwait(false);
OpenApiDocument document = await GetOpenApi(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);

if (options.FilterOptions != null)
{
Expand Down Expand Up @@ -116,7 +116,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
}
}

private static async Task<ApiDependency?> FindApiDependency(string? apiManifestPath, ILogger logger, CancellationToken cancellationToken)
private static async Task<ApiDependency?> FindApiDependency(string? apiManifestPath, ILogger logger, CancellationToken cancellationToken = default)
{
ApiDependency? apiDependency = null;
// If API Manifest is provided, load it, use it get the OpenAPI path
Expand Down Expand Up @@ -212,7 +212,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
}

// Get OpenAPI document either from OpenAPI or CSDL
private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogger logger, CancellationToken cancellationToken, string? metadataVersion = null)
private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogger logger, string? metadataVersion = null, CancellationToken cancellationToken = default)
{

OpenApiDocument document;
Expand Down Expand Up @@ -326,7 +326,7 @@ private static Stream ApplyFilterToCsdl(Stream csdlStream, string entitySetOrSin
public static async Task ValidateOpenApiDocument(
string openApi,
ILogger logger,
CancellationToken cancellationToken)
CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(openApi))
{
Expand Down Expand Up @@ -361,7 +361,7 @@ public static async Task ValidateOpenApiDocument(
}
}

private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken)
private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default)
{
ReadResult result;
Stopwatch stopwatch = Stopwatch.StartNew();
Expand Down Expand Up @@ -480,7 +480,7 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
/// <summary>
/// Reads stream from file system or makes HTTP request depending on the input string
/// </summary>
private static async Task<Stream> GetStream(string input, ILogger logger, CancellationToken cancellationToken)
private static async Task<Stream> GetStream(string input, ILogger logger, CancellationToken cancellationToken = default)
{
Stream stream;
using (logger.BeginScope("Reading input stream"))
Expand Down Expand Up @@ -556,7 +556,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
return extension;
}

internal static async Task<string?> ShowOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken)
internal static async Task<string?> ShowOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
{
try
{
Expand All @@ -565,7 +565,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
throw new ArgumentException("Please input a file path or URL");
}

var document = await GetOpenApi(options, logger, cancellationToken).ConfigureAwait(false);
var document = await GetOpenApi(options, logger, null, cancellationToken).ConfigureAwait(false);

using (logger.BeginScope("Creating diagram"))
{
Expand Down Expand Up @@ -709,7 +709,7 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
writer.WriteLine("</html");
}

internal static async Task PluginManifest(HidiOptions options, ILogger logger, CancellationToken cancellationToken)
internal static async Task PluginManifest(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
{
// If ApiManifest is provided, set the referenced OpenAPI document
var apiDependency = await FindApiDependency(options.FilterOptions?.FilterByApiManifest, logger, cancellationToken).ConfigureAwait(false);
Expand All @@ -719,7 +719,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
}

// Load OpenAPI document
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion).ConfigureAwait(false);
OpenApiDocument document = await GetOpenApi(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);

cancellationToken.ThrowIfCancellationRequested();

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ public async Task<ReadResult> ReadAsync(YamlDocument input, CancellationToken ca
};
}

private async Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken)
private async Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken = default)
{
// 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);
return await workspaceLoader.LoadAsync(new OpenApiReference { ExternalResource = "/" }, document, cancellationToken);
return await workspaceLoader.LoadAsync(new OpenApiReference { ExternalResource = "/" }, document, null, cancellationToken);
}

private void ResolveReferences(OpenApiDiagnostic diagnostic, OpenApiDocument document)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public OpenApiWorkspaceLoader(OpenApiWorkspace workspace, IStreamLoader loader,
_readerSettings = readerSettings;
}

internal async Task<OpenApiDiagnostic> LoadAsync(OpenApiReference reference, OpenApiDocument document, CancellationToken cancellationToken, OpenApiDiagnostic diagnostic = null)
internal async Task<OpenApiDiagnostic> LoadAsync(OpenApiReference reference, OpenApiDocument document, OpenApiDiagnostic diagnostic = null, CancellationToken cancellationToken = default)
{
_workspace.AddDocument(reference.ExternalResource, document);
document.Workspace = _workspace;
Expand Down Expand Up @@ -56,7 +56,7 @@ internal async Task<OpenApiDiagnostic> LoadAsync(OpenApiReference reference, Ope
}
if (result.OpenApiDocument != null)
{
var loadDiagnostic = await LoadAsync(item, result.OpenApiDocument, cancellationToken, diagnostic);
var loadDiagnostic = await LoadAsync(item, result.OpenApiDocument, diagnostic, cancellationToken);
diagnostic = loadDiagnostic;
}
}
Expand Down
26 changes: 13 additions & 13 deletions test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public async Task ShowCommandGeneratesMermaidMarkdownFileWithMermaidDiagram()
Output = new FileInfo("sample.md")
};

await OpenApiService.ShowOpenApiDocument(options, _logger, new CancellationToken());
await OpenApiService.ShowOpenApiDocument(options, _logger);

var output = await File.ReadAllTextAsync(options.Output.FullName);
Assert.Contains("graph LR", output, StringComparison.Ordinal);
Expand All @@ -155,7 +155,7 @@ public async Task ShowCommandGeneratesMermaidHtmlFileWithMermaidDiagram()
{
OpenApi = Path.Combine("UtilityFiles", "SampleOpenApi.yml")
};
var filePath = await OpenApiService.ShowOpenApiDocument(options, _logger, new CancellationToken());
var filePath = await OpenApiService.ShowOpenApiDocument(options, _logger);
Assert.True(File.Exists(filePath));
}

Expand All @@ -170,7 +170,7 @@ public async Task ShowCommandGeneratesMermaidMarkdownFileFromCsdlWithMermaidDiag
};

// create a dummy ILogger instance for testing
await OpenApiService.ShowOpenApiDocument(options, _logger, new CancellationToken());
await OpenApiService.ShowOpenApiDocument(options, _logger);

var output = await File.ReadAllTextAsync(options.Output.FullName);
Assert.Contains("graph LR", output, StringComparison.Ordinal);
Expand All @@ -180,29 +180,29 @@ public async Task ShowCommandGeneratesMermaidMarkdownFileFromCsdlWithMermaidDiag
public async Task ThrowIfOpenApiUrlIsNotProvidedWhenValidating()
{
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
await OpenApiService.ValidateOpenApiDocument("", _logger, new CancellationToken()));
await OpenApiService.ValidateOpenApiDocument("", _logger));
}


[Fact]
public async Task ThrowIfURLIsNotResolvableWhenValidating()
{
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await OpenApiService.ValidateOpenApiDocument("https://example.org/itdoesnmatter", _logger, new CancellationToken()));
await OpenApiService.ValidateOpenApiDocument("https://example.org/itdoesnmatter", _logger));
}

[Fact]
public async Task ThrowIfFileDoesNotExistWhenValidating()
{
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await OpenApiService.ValidateOpenApiDocument("aFileThatBetterNotExist.fake", _logger, new CancellationToken()));
await OpenApiService.ValidateOpenApiDocument("aFileThatBetterNotExist.fake", _logger));
}

[Fact]
public async Task ValidateCommandProcessesOpenApi()
{
// create a dummy ILogger instance for testing
await OpenApiService.ValidateOpenApiDocument(Path.Combine("UtilityFiles", "SampleOpenApi.yml"), _logger, new CancellationToken());
await OpenApiService.ValidateOpenApiDocument(Path.Combine("UtilityFiles", "SampleOpenApi.yml"), _logger);

Assert.True(true);
}
Expand All @@ -221,7 +221,7 @@ public async Task TransformCommandConvertsOpenApi()
InlineExternal = false,
};
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken());
await OpenApiService.TransformOpenApiDocument(options, _logger);

var output = await File.ReadAllTextAsync("sample.json");
Assert.NotEmpty(output);
Expand All @@ -240,7 +240,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputName()
InlineExternal = false,
};
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken());
await OpenApiService.TransformOpenApiDocument(options, _logger);

var output = await File.ReadAllTextAsync("output.yml");
Assert.NotEmpty(output);
Expand All @@ -258,7 +258,7 @@ public async Task TransformCommandConvertsCsdlWithDefaultOutputName()
InlineExternal = false,
};
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken());
await OpenApiService.TransformOpenApiDocument(options, _logger);

var output = await File.ReadAllTextAsync("output.yml");
Assert.NotEmpty(output);
Expand All @@ -278,7 +278,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAndSwitchF
InlineExternal = false,
};
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken());
await OpenApiService.TransformOpenApiDocument(options, _logger);

var output = await File.ReadAllTextAsync("output.yml");
Assert.NotEmpty(output);
Expand All @@ -295,7 +295,7 @@ public async Task ThrowTransformCommandIfOpenApiAndCsdlAreEmpty()
InlineExternal = false,
};
await Assert.ThrowsAsync<ArgumentException>(async () =>
await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken()));
await OpenApiService.TransformOpenApiDocument(options, _logger));

}

Expand All @@ -315,7 +315,7 @@ public async Task TransformToPowerShellCompliantOpenApi()
SettingsConfig = SettingsUtilities.GetConfiguration(settingsPath)
};
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken());
await OpenApiService.TransformOpenApiDocument(options, _logger);

var output = await File.ReadAllTextAsync("output.yml");
Assert.NotEmpty(output);
Expand Down