From 7fe922db323c32a16e1a97d861b6f4c7bfa6c0fd Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 30 Sep 2023 22:29:02 +1000 Subject: [PATCH] fix some warnings --- src/Microsoft.OpenApi.Hidi/OpenApiService.cs | 13 +++++++------ .../Services/OpenApiServiceTests.cs | 14 +++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs index 809e3ecf9..b3e66cfe2 100644 --- a/src/Microsoft.OpenApi.Hidi/OpenApiService.cs +++ b/src/Microsoft.OpenApi.Hidi/OpenApiService.cs @@ -81,7 +81,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l if (!string.IsNullOrEmpty(options.FilterOptions?.FilterByCollection)) { using var collectionStream = await GetStream(options.FilterOptions.FilterByCollection, logger, cancellationToken).ConfigureAwait(false); - postmanCollection = JsonDocument.Parse(collectionStream); + postmanCollection = await JsonDocument.ParseAsync(collectionStream, cancellationToken: cancellationToken).ConfigureAwait(false); } // Load OpenAPI document @@ -132,7 +132,8 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l } using (var fileStream = await GetStream(apiManifestRef[0], logger, cancellationToken).ConfigureAwait(false)) { - apiManifest = ApiManifestDocument.Load(JsonDocument.Parse(fileStream).RootElement); + var document = await JsonDocument.ParseAsync(fileStream, cancellationToken: cancellationToken).ConfigureAwait(false); + apiManifest = ApiManifestDocument.Load(document.RootElement); } apiDependency = !string.IsNullOrEmpty(apiDependencyName) && apiManifest.ApiDependencies.TryGetValue(apiDependencyName, out var dependency) ? dependency : apiManifest.ApiDependencies.First().Value; @@ -453,13 +454,13 @@ private static Dictionary> EnumerateJsonDocument(JsonElemen // Fetch list of methods and urls from collection, store them in a dictionary var path = request.GetProperty("url").GetProperty("raw").ToString(); var method = request.GetProperty("method").ToString(); - if (!paths.ContainsKey(path)) + if (paths.TryGetValue(path, out var value)) { - paths.Add(path, new List { method }); + value.Add(method); } else { - paths[path].Add(method); + paths.Add(path, new List {method}); } } else @@ -755,7 +756,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C using var file = new FileStream(manifestFile.FullName, FileMode.Create); using var jsonWriter = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true }); manifest.Write(jsonWriter); - jsonWriter.Flush(); + await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false); } } } diff --git a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs index b1fcd24d3..9d73c8db6 100644 --- a/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs +++ b/test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs @@ -144,7 +144,7 @@ public async Task ShowCommandGeneratesMermaidMarkdownFileWithMermaidDiagram() await OpenApiService.ShowOpenApiDocument(options, _logger, new CancellationToken()); - var output = File.ReadAllText(options.Output.FullName); + var output = await File.ReadAllTextAsync(options.Output.FullName); Assert.Contains("graph LR", output, StringComparison.Ordinal); } @@ -172,7 +172,7 @@ public async Task ShowCommandGeneratesMermaidMarkdownFileFromCsdlWithMermaidDiag // create a dummy ILogger instance for testing await OpenApiService.ShowOpenApiDocument(options, _logger, new CancellationToken()); - var output = File.ReadAllText(options.Output.FullName); + var output = await File.ReadAllTextAsync(options.Output.FullName); Assert.Contains("graph LR", output, StringComparison.Ordinal); } @@ -223,7 +223,7 @@ public async Task TransformCommandConvertsOpenApi() // create a dummy ILogger instance for testing await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken()); - var output = File.ReadAllText("sample.json"); + var output = await File.ReadAllTextAsync("sample.json"); Assert.NotEmpty(output); } @@ -242,7 +242,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputName() // create a dummy ILogger instance for testing await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken()); - var output = File.ReadAllText("output.yml"); + var output = await File.ReadAllTextAsync("output.yml"); Assert.NotEmpty(output); } @@ -260,7 +260,7 @@ public async Task TransformCommandConvertsCsdlWithDefaultOutputName() // create a dummy ILogger instance for testing await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken()); - var output = File.ReadAllText("output.yml"); + var output = await File.ReadAllTextAsync("output.yml"); Assert.NotEmpty(output); } @@ -280,7 +280,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputNameAndSwitchF // create a dummy ILogger instance for testing await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken()); - var output = File.ReadAllText("output.yml"); + var output = await File.ReadAllTextAsync("output.yml"); Assert.NotEmpty(output); } @@ -317,7 +317,7 @@ public async Task TransformToPowerShellCompliantOpenApi() // create a dummy ILogger instance for testing await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken()); - var output = File.ReadAllText("output.yml"); + var output = await File.ReadAllTextAsync("output.yml"); Assert.NotEmpty(output); }