@@ -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}
0 commit comments