@@ -41,7 +41,7 @@ internal static class OpenApiService
41
41
/// <summary>
42
42
/// Implementation of the transform command
43
43
/// </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 )
45
45
{
46
46
if ( string . IsNullOrEmpty ( options . OpenApi ) && string . IsNullOrEmpty ( options . Csdl ) && string . IsNullOrEmpty ( options . FilterOptions ? . FilterByApiManifest ) )
47
47
{
@@ -81,11 +81,11 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
81
81
if ( ! string . IsNullOrEmpty ( options . FilterOptions ? . FilterByCollection ) )
82
82
{
83
83
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 ) ;
85
85
}
86
86
87
87
// 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 ) ;
89
89
90
90
if ( options . FilterOptions != null )
91
91
{
@@ -116,7 +116,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
116
116
}
117
117
}
118
118
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 )
120
120
{
121
121
ApiDependency ? apiDependency = null ;
122
122
// 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
132
132
}
133
133
using ( var fileStream = await GetStream ( apiManifestRef [ 0 ] , logger , cancellationToken ) . ConfigureAwait ( false ) )
134
134
{
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 ) ;
136
137
}
137
138
138
139
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
185
186
using var outputStream = options . Output . Create ( ) ;
186
187
using var textWriter = new StreamWriter ( outputStream ) ;
187
188
188
- var settings = new OpenApiWriterSettings ( )
189
+ var settings = new OpenApiWriterSettings
189
190
{
190
191
InlineLocalReferences = options . InlineLocal ,
191
192
InlineExternalReferences = options . InlineExternal
@@ -211,7 +212,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
211
212
}
212
213
213
214
// 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 )
215
216
{
216
217
217
218
OpenApiDocument document ;
@@ -325,7 +326,7 @@ private static Stream ApplyFilterToCsdl(Stream csdlStream, string entitySetOrSin
325
326
public static async Task ValidateOpenApiDocument (
326
327
string openApi ,
327
328
ILogger logger ,
328
- CancellationToken cancellationToken )
329
+ CancellationToken cancellationToken = default )
329
330
{
330
331
if ( string . IsNullOrEmpty ( openApi ) )
331
332
{
@@ -360,7 +361,7 @@ public static async Task ValidateOpenApiDocument(
360
361
}
361
362
}
362
363
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 )
364
365
{
365
366
ReadResult result ;
366
367
Stopwatch stopwatch = Stopwatch . StartNew ( ) ;
@@ -453,13 +454,13 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
453
454
// Fetch list of methods and urls from collection, store them in a dictionary
454
455
var path = request . GetProperty ( "url" ) . GetProperty ( "raw" ) . ToString ( ) ;
455
456
var method = request . GetProperty ( "method" ) . ToString ( ) ;
456
- if ( ! paths . ContainsKey ( path ) )
457
+ if ( paths . TryGetValue ( path , out var value ) )
457
458
{
458
- paths . Add ( path , new List < string > { method } ) ;
459
+ value . Add ( method ) ;
459
460
}
460
461
else
461
462
{
462
- paths [ path ] . Add ( method ) ;
463
+ paths . Add ( path , new List < string > { method } ) ;
463
464
}
464
465
}
465
466
else
@@ -479,7 +480,7 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
479
480
/// <summary>
480
481
/// Reads stream from file system or makes HTTP request depending on the input string
481
482
/// </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 )
483
484
{
484
485
Stream stream ;
485
486
using ( logger . BeginScope ( "Reading input stream" ) )
@@ -509,13 +510,15 @@ private static async Task<Stream> GetStream(string input, ILogger logger, Cancel
509
510
var fileInput = new FileInfo ( input ) ;
510
511
stream = fileInput . OpenRead ( ) ;
511
512
}
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 )
519
522
{
520
523
throw new InvalidOperationException ( $ "Could not open the file at { input } ", ex ) ;
521
524
}
@@ -553,7 +556,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
553
556
return extension ;
554
557
}
555
558
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 )
557
560
{
558
561
try
559
562
{
@@ -562,7 +565,7 @@ private static string GetInputPathExtension(string? openapi = null, string? csdl
562
565
throw new ArgumentException ( "Please input a file path or URL" ) ;
563
566
}
564
567
565
- var document = await GetOpenApi ( options , logger , cancellationToken ) . ConfigureAwait ( false ) ;
568
+ var document = await GetOpenApi ( options , logger , null , cancellationToken ) . ConfigureAwait ( false ) ;
566
569
567
570
using ( logger . BeginScope ( "Creating diagram" ) )
568
571
{
@@ -661,18 +664,21 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
661
664
{
662
665
var rootNode = OpenApiUrlTreeNode . Create ( document , "main" ) ;
663
666
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
+ """ ) ;
676
682
writer . WriteLine ( "<h1>" + document . Info . Title + "</h1>" ) ;
677
683
writer . WriteLine ( ) ;
678
684
writer . WriteLine ( $ "<h3> API Description: <a href='{ sourceUrl } '>{ sourceUrl } </a></h3>") ;
@@ -683,30 +689,34 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
683
689
{
684
690
writer . WriteLine ( $ "<span style=\" padding:2px;background-color:{ style . Value . Color } ;border: 2px solid\" >{ style . Key . Replace ( "_" , " " , StringComparison . OrdinalIgnoreCase ) } </span>") ;
685
691
}
692
+
686
693
writer . WriteLine ( "</div>" ) ;
687
694
writer . WriteLine ( "<hr/>" ) ;
688
695
writer . WriteLine ( "<code class=\" language-mermaid\" >" ) ;
689
696
rootNode . WriteMermaid ( writer ) ;
690
697
writer . WriteLine ( "</code>" ) ;
691
698
692
699
// 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
+ """ ) ;
705
715
// Write script tag to include JS library for rendering mermaid
706
716
writer . WriteLine ( "</html" ) ;
707
717
}
708
718
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 )
710
720
{
711
721
// If ApiManifest is provided, set the referenced OpenAPI document
712
722
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
716
726
}
717
727
718
728
// 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 ) ;
720
730
721
731
cancellationToken . ThrowIfCancellationRequested ( ) ;
722
732
@@ -737,7 +747,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
737
747
WriteOpenApi ( options , OpenApiFormat . Json , OpenApiSpecVersion . OpenApi3_0 , document , logger ) ;
738
748
739
749
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
740
- var manifest = new OpenAIPluginManifest ( )
750
+ var manifest = new OpenAIPluginManifest
741
751
{
742
752
NameForHuman = document . Info . Title ,
743
753
DescriptionForHuman = document . Info . Description ,
@@ -755,7 +765,7 @@ internal static async Task PluginManifest(HidiOptions options, ILogger logger, C
755
765
using var file = new FileStream ( manifestFile . FullName , FileMode . Create ) ;
756
766
using var jsonWriter = new Utf8JsonWriter ( file , new JsonWriterOptions { Indented = true } ) ;
757
767
manifest . Write ( jsonWriter ) ;
758
- jsonWriter . Flush ( ) ;
768
+ await jsonWriter . FlushAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
759
769
}
760
770
}
761
771
}
0 commit comments