Skip to content

Commit 03a2810

Browse files
committed
Merge remote-tracking branch 'upstream/vnext' into cleanup-csproj
2 parents 32b2525 + 306dda2 commit 03a2810

File tree

159 files changed

+2790
-2652
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+2790
-2652
lines changed

src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private void AddAdditionalPropertiesToSchema(OpenApiSchema schema)
179179
{
180180
if (schema != null && !_schemaLoop.Contains(schema) && "object".Equals(schema.Type, StringComparison.OrdinalIgnoreCase))
181181
{
182-
schema.AdditionalProperties = new OpenApiSchema() { Type = "object" };
182+
schema.AdditionalProperties = new OpenApiSchema { Type = "object" };
183183

184184
/* Because 'additionalProperties' are now being walked,
185185
* we need a way to keep track of visited schemas to avoid

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static class OpenApiService
4141
/// <summary>
4242
/// Implementation of the transform command
4343
/// </summary>
44-
public static async Task TransformOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken)
44+
public static async Task TransformOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken = default)
4545
{
4646
if (string.IsNullOrEmpty(options.OpenApi) && string.IsNullOrEmpty(options.Csdl) && string.IsNullOrEmpty(options.FilterOptions?.FilterByApiManifest))
4747
{
@@ -81,11 +81,11 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
8181
if (!string.IsNullOrEmpty(options.FilterOptions?.FilterByCollection))
8282
{
8383
using var collectionStream = await GetStream(options.FilterOptions.FilterByCollection, logger, cancellationToken).ConfigureAwait(false);
84-
postmanCollection = JsonDocument.Parse(collectionStream);
84+
postmanCollection = await JsonDocument.ParseAsync(collectionStream, cancellationToken: cancellationToken).ConfigureAwait(false);
8585
}
8686

8787
// Load OpenAPI document
88-
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion).ConfigureAwait(false);
88+
OpenApiDocument document = await GetOpenApi(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
8989

9090
if (options.FilterOptions != null)
9191
{
@@ -116,7 +116,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
116116
}
117117
}
118118

119-
private static async Task<ApiDependency?> FindApiDependency(string? apiManifestPath, ILogger logger, CancellationToken cancellationToken)
119+
private static async Task<ApiDependency?> FindApiDependency(string? apiManifestPath, ILogger logger, CancellationToken cancellationToken = default)
120120
{
121121
ApiDependency? apiDependency = null;
122122
// If API Manifest is provided, load it, use it get the OpenAPI path
@@ -132,7 +132,8 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
132132
}
133133
using (var fileStream = await GetStream(apiManifestRef[0], logger, cancellationToken).ConfigureAwait(false))
134134
{
135-
apiManifest = ApiManifestDocument.Load(JsonDocument.Parse(fileStream).RootElement);
135+
var document = await JsonDocument.ParseAsync(fileStream, cancellationToken: cancellationToken).ConfigureAwait(false);
136+
apiManifest = ApiManifestDocument.Load(document.RootElement);
136137
}
137138

138139
apiDependency = !string.IsNullOrEmpty(apiDependencyName) && apiManifest.ApiDependencies.TryGetValue(apiDependencyName, out var dependency) ? dependency : apiManifest.ApiDependencies.First().Value;
@@ -185,7 +186,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
185186
using var outputStream = options.Output.Create();
186187
using var textWriter = new StreamWriter(outputStream);
187188

188-
var settings = new OpenApiWriterSettings()
189+
var settings = new OpenApiWriterSettings
189190
{
190191
InlineLocalReferences = options.InlineLocal,
191192
InlineExternalReferences = options.InlineExternal
@@ -211,7 +212,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
211212
}
212213

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

217218
OpenApiDocument document;
@@ -325,7 +326,7 @@ private static Stream ApplyFilterToCsdl(Stream csdlStream, string entitySetOrSin
325326
public static async Task ValidateOpenApiDocument(
326327
string openApi,
327328
ILogger logger,
328-
CancellationToken cancellationToken)
329+
CancellationToken cancellationToken = default)
329330
{
330331
if (string.IsNullOrEmpty(openApi))
331332
{
@@ -360,7 +361,7 @@ public static async Task ValidateOpenApiDocument(
360361
}
361362
}
362363

363-
private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken)
364+
private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inlineExternal, ILogger logger, Stream stream, CancellationToken cancellationToken = default)
364365
{
365366
ReadResult result;
366367
Stopwatch stopwatch = Stopwatch.StartNew();
@@ -453,13 +454,13 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
453454
// Fetch list of methods and urls from collection, store them in a dictionary
454455
var path = request.GetProperty("url").GetProperty("raw").ToString();
455456
var method = request.GetProperty("method").ToString();
456-
if (!paths.ContainsKey(path))
457+
if (paths.TryGetValue(path, out var value))
457458
{
458-
paths.Add(path, new List<string> { method });
459+
value.Add(method);
459460
}
460461
else
461462
{
462-
paths[path].Add(method);
463+
paths.Add(path, new List<string> {method});
463464
}
464465
}
465466
else
@@ -479,7 +480,7 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
479480
/// <summary>
480481
/// Reads stream from file system or makes HTTP request depending on the input string
481482
/// </summary>
482-
private static async Task<Stream> GetStream(string input, ILogger logger, CancellationToken cancellationToken)
483+
private static async Task<Stream> GetStream(string input, ILogger logger, CancellationToken cancellationToken = default)
483484
{
484485
Stream stream;
485486
using (logger.BeginScope("Reading input stream"))
@@ -509,13 +510,15 @@ private static async Task<Stream> GetStream(string input, ILogger logger, Cancel
509510
var fileInput = new FileInfo(input);
510511
stream = fileInput.OpenRead();
511512
}
512-
catch (Exception ex) when (ex is FileNotFoundException ||
513-
ex is PathTooLongException ||
514-
ex is DirectoryNotFoundException ||
515-
ex is IOException ||
516-
ex is UnauthorizedAccessException ||
517-
ex is SecurityException ||
518-
ex is NotSupportedException)
513+
catch (Exception ex) when (
514+
ex is
515+
FileNotFoundException or
516+
PathTooLongException or
517+
DirectoryNotFoundException or
518+
IOException or
519+
UnauthorizedAccessException or
520+
SecurityException or
521+
NotSupportedException)
519522
{
520523
throw new InvalidOperationException($"Could not open the file at {input}", ex);
521524
}
@@ -553,7 +556,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
553556
return extension;
554557
}
555558

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

565-
var document = await GetOpenApi(options, logger, cancellationToken).ConfigureAwait(false);
568+
var document = await GetOpenApi(options, logger, null, cancellationToken).ConfigureAwait(false);
566569

567570
using (logger.BeginScope("Creating diagram"))
568571
{
@@ -661,18 +664,21 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
661664
{
662665
var rootNode = OpenApiUrlTreeNode.Create(document, "main");
663666

664-
writer.WriteLine(@"<!doctype html>
665-
<html>
666-
<head>
667-
<meta charset=""utf-8""/>
668-
<script src=""https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.0.0/mermaid.min.js""></script>
669-
</head>
670-
<style>
671-
body {
672-
font-family: Verdana, sans-serif;
673-
}
674-
</style>
675-
<body>");
667+
writer.WriteLine(
668+
"""
669+
<!doctype html>
670+
<html>
671+
<head>
672+
<meta charset="utf-8"/>
673+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.0.0/mermaid.min.js"></script>
674+
</head>
675+
<style>
676+
body {
677+
font-family: Verdana, sans-serif;
678+
}
679+
</style>
680+
<body>
681+
""");
676682
writer.WriteLine("<h1>" + document.Info.Title + "</h1>");
677683
writer.WriteLine();
678684
writer.WriteLine($"<h3> API Description: <a href='{sourceUrl}'>{sourceUrl}</a></h3>");
@@ -683,30 +689,34 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
683689
{
684690
writer.WriteLine($"<span style=\"padding:2px;background-color:{style.Value.Color};border: 2px solid\">{style.Key.Replace("_", " ", StringComparison.OrdinalIgnoreCase)}</span>");
685691
}
692+
686693
writer.WriteLine("</div>");
687694
writer.WriteLine("<hr/>");
688695
writer.WriteLine("<code class=\"language-mermaid\">");
689696
rootNode.WriteMermaid(writer);
690697
writer.WriteLine("</code>");
691698

692699
// Write script tag to include JS library for rendering markdown
693-
writer.WriteLine(@"<script>
694-
var config = {
695-
startOnLoad:true,
696-
theme: 'forest',
697-
flowchart:{
698-
useMaxWidth:false,
699-
htmlLabels:true
700-
}
701-
};
702-
mermaid.initialize(config);
703-
window.mermaid.init(undefined, document.querySelectorAll('.language-mermaid'));
704-
</script>");
700+
writer.WriteLine(
701+
"""
702+
<script>
703+
var config = {
704+
startOnLoad:true,
705+
theme: 'forest',
706+
flowchart:{
707+
useMaxWidth:false,
708+
htmlLabels:true
709+
}
710+
};
711+
mermaid.initialize(config);
712+
window.mermaid.init(undefined, document.querySelectorAll('.language-mermaid'));
713+
</script>
714+
""");
705715
// Write script tag to include JS library for rendering mermaid
706716
writer.WriteLine("</html");
707717
}
708718

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

718728
// Load OpenAPI document
719-
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion).ConfigureAwait(false);
729+
OpenApiDocument document = await GetOpenApi(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
720730

721731
cancellationToken.ThrowIfCancellationRequested();
722732

@@ -737,7 +747,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
737747
WriteOpenApi(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_0, document, logger);
738748

739749
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
740-
var manifest = new OpenAIPluginManifest()
750+
var manifest = new OpenAIPluginManifest
741751
{
742752
NameForHuman = document.Info.Title,
743753
DescriptionForHuman = document.Info.Description,
@@ -755,7 +765,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
755765
using var file = new FileStream(manifestFile.FullName, FileMode.Create);
756766
using var jsonWriter = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true });
757767
manifest.Write(jsonWriter);
758-
jsonWriter.Flush();
768+
await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false);
759769
}
760770
}
761771
}

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license.
33

44
using System.CommandLine;
5-
using System.CommandLine.Parsing;
65
using System.Threading.Tasks;
76
using Microsoft.OpenApi.Hidi.Handlers;
87
using Microsoft.OpenApi.Hidi.Options;
@@ -22,7 +21,7 @@ static async Task<int> Main(string[] args)
2221

2322
internal static RootCommand CreateRootCommand()
2423
{
25-
var rootCommand = new RootCommand() { };
24+
var rootCommand = new RootCommand { };
2625

2726
var commandOptions = new CommandOptions();
2827

src/Microsoft.OpenApi.Readers/Interface/IOpenApiVersionService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using Microsoft.OpenApi.Interfaces;
65
using Microsoft.OpenApi.Models;
76
using Microsoft.OpenApi.Readers.ParseNodes;

src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
6060
public async Task<ReadResult> ReadAsync(Stream input, CancellationToken cancellationToken = default)
6161
{
6262
MemoryStream bufferedStream;
63-
if (input is MemoryStream)
63+
if (input is MemoryStream stream)
6464
{
65-
bufferedStream = (MemoryStream)input;
65+
bufferedStream = stream;
6666
}
6767
else
6868
{

src/Microsoft.OpenApi.Readers/OpenApiTextReaderReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public T ReadFragment<T>(TextReader input, OpenApiSpecVersion version, out OpenA
104104
{
105105
diagnostic = new OpenApiDiagnostic();
106106
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
107-
return default(T);
107+
return default;
108108
}
109109

110110
return new OpenApiYamlDocumentReader(this._settings).ReadFragment<T>(yamlDocument, version, out diagnostic);

src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.IO;
76
using System.Linq;
87
using System.Threading;
98
using System.Threading.Tasks;
@@ -133,22 +132,22 @@ public async Task<ReadResult> ReadAsync(YamlDocument input, CancellationToken ca
133132
}
134133
}
135134

136-
return new ReadResult()
135+
return new ReadResult
137136
{
138137
OpenApiDocument = document,
139138
OpenApiDiagnostic = diagnostic
140139
};
141140
}
142141

143-
private async Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken)
142+
private async Task<OpenApiDiagnostic> LoadExternalRefs(OpenApiDocument document, CancellationToken cancellationToken = default)
144143
{
145144
// Create workspace for all documents to live in.
146145
var openApiWorkSpace = new OpenApiWorkspace();
147146

148147
// Load this root document into the workspace
149148
var streamLoader = new DefaultStreamLoader(_settings.BaseUrl);
150149
var workspaceLoader = new OpenApiWorkspaceLoader(openApiWorkSpace, _settings.CustomExternalLoader ?? streamLoader, _settings);
151-
return await workspaceLoader.LoadAsync(new OpenApiReference() { ExternalResource = "/" }, document, cancellationToken);
150+
return await workspaceLoader.LoadAsync(new OpenApiReference { ExternalResource = "/" }, document, null, cancellationToken);
152151
}
153152

154153
private void ResolveReferences(OpenApiDiagnostic diagnostic, OpenApiDocument document)

src/Microsoft.OpenApi.Readers/ParseNodes/AnyMapFieldMapParameter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using Microsoft.OpenApi.Any;
7-
using Microsoft.OpenApi.Interfaces;
87
using Microsoft.OpenApi.Models;
98

109
namespace Microsoft.OpenApi.Readers.ParseNodes

src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ public static YamlNode Find(this JsonPointer currentPointer, YamlNode baseYamlNo
2626
var pointer = baseYamlNode;
2727
foreach (var token in currentPointer.Tokens)
2828
{
29-
var sequence = pointer as YamlSequenceNode;
30-
31-
if (sequence != null)
29+
if (pointer is YamlSequenceNode sequence)
3230
{
3331
pointer = sequence.Children[Convert.ToInt32(token)];
3432
}
3533
else
3634
{
37-
var map = pointer as YamlMappingNode;
38-
if (map != null)
35+
if (pointer is YamlMappingNode map)
3936
{
4037
if (!map.Children.TryGetValue(new YamlScalarNode(token), out pointer))
4138
{

0 commit comments

Comments
 (0)