From c27d51877de8446c3847f181d14a9b14916bb76b Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 2 Oct 2023 20:25:11 +1100 Subject: [PATCH 1/5] Update OpenApiYamlDocumentReader.cs --- src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs b/src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs index 5537366d0..87838ea98 100644 --- a/src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs +++ b/src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs @@ -140,7 +140,7 @@ public async Task ReadAsync(YamlDocument input, CancellationToken ca }; } - private async Task LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken) + private async Task LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken = default) { // Create workspace for all documents to live in. var openApiWorkSpace = new OpenApiWorkspace(); From 74ce7db1b33bb6330df3142d4dc76223b817486f Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 2 Oct 2023 20:26:49 +1100 Subject: [PATCH 2/5] Update OpenApiService.cs --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index d698bd5b2..73e21389e 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -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) { @@ -212,7 +212,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma } // Get OpenAPI document either from OpenAPI or CSDL - private static async Task GetOpenApi(HidiOptions options, ILogger logger, CancellationToken cancellationToken, string? metadataVersion = null) + private static async Task GetOpenApi(HidiOptions options, ILogger logger, string? metadataVersion = null, CancellationToken cancellationToken = default) { OpenApiDocument document; @@ -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")) { @@ -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(); From 52be452e3638e2429a6cd20ae96cffd6037a45a5 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 2 Oct 2023 20:28:04 +1100 Subject: [PATCH 3/5] . --- src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs | 2 +- .../Services/OpenApiWorkspaceLoader.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs b/src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs index 87838ea98..6eac81a93 100644 --- a/src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs +++ b/src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs @@ -148,7 +148,7 @@ private async Task LoadExternalRefs(OpenApiDocument document, // 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) diff --git a/src/Microsoft.OpenApi.Readers/Services/OpenApiWorkspaceLoader.cs b/src/Microsoft.OpenApi.Readers/Services/OpenApiWorkspaceLoader.cs index 79f6206d0..468b88e28 100644 --- a/src/Microsoft.OpenApi.Readers/Services/OpenApiWorkspaceLoader.cs +++ b/src/Microsoft.OpenApi.Readers/Services/OpenApiWorkspaceLoader.cs @@ -24,7 +24,7 @@ public OpenApiWorkspaceLoader(OpenApiWorkspace workspace, IStreamLoader loader, _readerSettings = readerSettings; } - internal async Task LoadAsync(OpenApiReference reference, OpenApiDocument document, CancellationToken cancellationToken, OpenApiDiagnostic diagnostic = null) + internal async Task LoadAsync(OpenApiReference reference, OpenApiDocument document, OpenApiDiagnostic diagnostic = null, CancellationToken cancellationToken = default) { _workspace.AddDocument(reference.ExternalResource, document); document.Workspace = _workspace; @@ -56,7 +56,7 @@ internal async Task 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; } } From 508dc0a58ce3c2de90933f0cdf4b9af446c2f23f Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 2 Oct 2023 20:30:47 +1100 Subject: [PATCH 4/5] Update OpenApiService.cs --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 73e21389e..eed17ceeb 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -41,7 +41,7 @@ internal static class OpenApiService /// /// Implementation of the transform command /// - 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)) { @@ -116,7 +116,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l } } - private static async Task FindApiDependency(string? apiManifestPath, ILogger logger, CancellationToken cancellationToken) + private static async Task FindApiDependency(string? apiManifestPath, ILogger logger, CancellationToken cancellationToken = default) { ApiDependency? apiDependency = null; // If API Manifest is provided, load it, use it get the OpenAPI path @@ -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)) { @@ -361,7 +361,7 @@ public static async Task ValidateOpenApiDocument( } } - private static async Task ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken) + private static async Task ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default) { ReadResult result; Stopwatch stopwatch = Stopwatch.StartNew(); @@ -480,7 +480,7 @@ private static Dictionary> EnumerateJsonDocument(JsonElemen /// /// Reads stream from file system or makes HTTP request depending on the input string /// - private static async Task GetStream(string input, ILogger logger, CancellationToken cancellationToken) + private static async Task GetStream(string input, ILogger logger, CancellationToken cancellationToken = default) { Stream stream; using (logger.BeginScope("Reading input stream")) @@ -556,7 +556,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl return extension; } - internal static async Task ShowOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken) + internal static async Task ShowOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default) { try { @@ -709,7 +709,7 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d writer.WriteLine(" Date: Mon, 2 Oct 2023 20:31:44 +1100 Subject: [PATCH 5/5] Update OpenApiServiceTests.cs --- .../Services/OpenApiServiceTests.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs index fb2349e52..55be138b2 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs +++ b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs @@ -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); @@ -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)); } @@ -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); @@ -180,7 +180,7 @@ public async Task ShowCommandGeneratesMermaidMarkdownFileFromCsdlWithMermaidDiag public async Task ThrowIfOpenApiUrlIsNotProvidedWhenValidating() { await Assert.ThrowsAsync(async () => - await OpenApiService.ValidateOpenApiDocument("", _logger, new CancellationToken())); + await OpenApiService.ValidateOpenApiDocument("", _logger)); } @@ -188,21 +188,21 @@ await Assert.ThrowsAsync(async () => public async Task ThrowIfURLIsNotResolvableWhenValidating() { await Assert.ThrowsAsync(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(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); } @@ -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); @@ -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); @@ -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); @@ -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); @@ -295,7 +295,7 @@ public async Task ThrowTransformCommandIfOpenApiAndCsdlAreEmpty() InlineExternal = false, }; await Assert.ThrowsAsync(async () => - await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken())); + await OpenApiService.TransformOpenApiDocument(options, _logger)); } @@ -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);