Skip to content

Commit 4fa15b4

Browse files
committed
Merge remote-tracking branch 'upstream/vnext' into inline-some-out-variables
2 parents 9aa7897 + 306dda2 commit 4fa15b4

File tree

159 files changed

+2776
-2639
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

+2776
-2639
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: 53 additions & 44 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
{
@@ -85,7 +85,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
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
@@ -186,7 +186,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
186186
using var outputStream = options.Output.Create();
187187
using var textWriter = new StreamWriter(outputStream);
188188

189-
var settings = new OpenApiWriterSettings()
189+
var settings = new OpenApiWriterSettings
190190
{
191191
InlineLocalReferences = options.InlineLocal,
192192
InlineExternalReferences = options.InlineExternal
@@ -212,7 +212,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
212212
}
213213

214214
// Get OpenAPI document either from OpenAPI or CSDL
215-
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)
216216
{
217217

218218
OpenApiDocument document;
@@ -326,7 +326,7 @@ private static Stream ApplyFilterToCsdl(Stream csdlStream, string entitySetOrSin
326326
public static async Task ValidateOpenApiDocument(
327327
string openApi,
328328
ILogger logger,
329-
CancellationToken cancellationToken)
329+
CancellationToken cancellationToken = default)
330330
{
331331
if (string.IsNullOrEmpty(openApi))
332332
{
@@ -361,7 +361,7 @@ public static async Task ValidateOpenApiDocument(
361361
}
362362
}
363363

364-
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)
365365
{
366366
ReadResult result;
367367
Stopwatch stopwatch = Stopwatch.StartNew();
@@ -480,7 +480,7 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
480480
/// <summary>
481481
/// Reads stream from file system or makes HTTP request depending on the input string
482482
/// </summary>
483-
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)
484484
{
485485
Stream stream;
486486
using (logger.BeginScope("Reading input stream"))
@@ -510,13 +510,15 @@ private static async Task<Stream> GetStream(string input, ILogger logger, Cancel
510510
var fileInput = new FileInfo(input);
511511
stream = fileInput.OpenRead();
512512
}
513-
catch (Exception ex) when (ex is FileNotFoundException ||
514-
ex is PathTooLongException ||
515-
ex is DirectoryNotFoundException ||
516-
ex is IOException ||
517-
ex is UnauthorizedAccessException ||
518-
ex is SecurityException ||
519-
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)
520522
{
521523
throw new InvalidOperationException($"Could not open the file at {input}", ex);
522524
}
@@ -554,7 +556,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
554556
return extension;
555557
}
556558

557-
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)
558560
{
559561
try
560562
{
@@ -563,7 +565,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
563565
throw new ArgumentException("Please input a file path or URL");
564566
}
565567

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

568570
using (logger.BeginScope("Creating diagram"))
569571
{
@@ -662,18 +664,21 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
662664
{
663665
var rootNode = OpenApiUrlTreeNode.Create(document, "main");
664666

665-
writer.WriteLine(@"<!doctype html>
666-
<html>
667-
<head>
668-
<meta charset=""utf-8""/>
669-
<script src=""https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.0.0/mermaid.min.js""></script>
670-
</head>
671-
<style>
672-
body {
673-
font-family: Verdana, sans-serif;
674-
}
675-
</style>
676-
<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+
""");
677682
writer.WriteLine("<h1>" + document.Info.Title + "</h1>");
678683
writer.WriteLine();
679684
writer.WriteLine($"<h3> API Description: <a href='{sourceUrl}'>{sourceUrl}</a></h3>");
@@ -684,30 +689,34 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
684689
{
685690
writer.WriteLine($"<span style=\"padding:2px;background-color:{style.Value.Color};border: 2px solid\">{style.Key.Replace("_", " ", StringComparison.OrdinalIgnoreCase)}</span>");
686691
}
692+
687693
writer.WriteLine("</div>");
688694
writer.WriteLine("<hr/>");
689695
writer.WriteLine("<code class=\"language-mermaid\">");
690696
rootNode.WriteMermaid(writer);
691697
writer.WriteLine("</code>");
692698

693699
// Write script tag to include JS library for rendering markdown
694-
writer.WriteLine(@"<script>
695-
var config = {
696-
startOnLoad:true,
697-
theme: 'forest',
698-
flowchart:{
699-
useMaxWidth:false,
700-
htmlLabels:true
701-
}
702-
};
703-
mermaid.initialize(config);
704-
window.mermaid.init(undefined, document.querySelectorAll('.language-mermaid'));
705-
</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+
""");
706715
// Write script tag to include JS library for rendering mermaid
707716
writer.WriteLine("</html");
708717
}
709718

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

719728
// Load OpenAPI document
720-
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion).ConfigureAwait(false);
729+
OpenApiDocument document = await GetOpenApi(options, logger, options.MetadataVersion, cancellationToken).ConfigureAwait(false);
721730

722731
cancellationToken.ThrowIfCancellationRequested();
723732

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

740749
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
741-
var manifest = new OpenAIPluginManifest()
750+
var manifest = new OpenAIPluginManifest
742751
{
743752
NameForHuman = document.Info.Title,
744753
DescriptionForHuman = document.Info.Description,

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
{

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
7373
{
7474
Context.StartObject(key);
7575
value = n.Value as YamlMappingNode == null
76-
? default(T)
76+
? default
7777
: map(new MapNode(Context, n.Value as YamlMappingNode));
7878
}
7979
finally
@@ -119,7 +119,7 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
119119
// If the component isn't a reference to another component, then point it to itself.
120120
if (entry.value.Reference == null)
121121
{
122-
entry.value.Reference = new OpenApiReference()
122+
entry.value.Reference = new OpenApiReference
123123
{
124124
Type = referenceType,
125125
Id = entry.key
@@ -151,8 +151,7 @@ public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
151151
try
152152
{
153153
Context.StartObject(key);
154-
YamlScalarNode scalarNode = n.Value as YamlScalarNode;
155-
if (scalarNode == null)
154+
if (n.Value is not YamlScalarNode scalarNode)
156155
{
157156
throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context);
158157
}
@@ -183,7 +182,7 @@ public override string GetRaw()
183182
public T GetReferencedObject<T>(ReferenceType referenceType, string referenceId)
184183
where T : IOpenApiReferenceable, new()
185184
{
186-
return new T()
185+
return new T
187186
{
188187
UnresolvedReference = true,
189188
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId, referenceType)
@@ -202,8 +201,7 @@ public string GetReferencePointer()
202201

203202
public string GetScalarValue(ValueNode key)
204203
{
205-
var scalarNode = _node.Children[new YamlScalarNode(key.GetScalarValue())] as YamlScalarNode;
206-
if (scalarNode == null)
204+
if (_node.Children[new YamlScalarNode(key.GetScalarValue())] is not YamlScalarNode scalarNode)
207205
{
208206
throw new OpenApiReaderException($"Expected scalar at line {_node.Start.Line} for key {key.GetScalarValue()}", Context);
209207
}

0 commit comments

Comments
 (0)