5
5
using System . Collections . Generic ;
6
6
using System . Diagnostics ;
7
7
using System . IO ;
8
+ using System . Linq ;
8
9
using System . Net ;
9
10
using System . Net . Http ;
11
+ using System . Reflection ;
10
12
using System . Security ;
11
13
using System . Text ;
12
- using System . Threading . Tasks ;
13
14
using System . Text . Json ;
14
- using Microsoft . Extensions . Logging ;
15
+ using System . Threading ;
16
+ using System . Threading . Tasks ;
17
+ using System . Xml ;
15
18
using System . Xml . Linq ;
19
+ using System . Xml . Xsl ;
20
+ using Microsoft . Extensions . Configuration ;
21
+ using Microsoft . Extensions . Logging ;
16
22
using Microsoft . OData . Edm . Csdl ;
23
+ using Microsoft . OpenApi . ApiManifest ;
24
+ using Microsoft . OpenApi . ApiManifest . OpenAI ;
17
25
using Microsoft . OpenApi . Extensions ;
26
+ using Microsoft . OpenApi . Hidi . Formatters ;
27
+ using Microsoft . OpenApi . Hidi . Options ;
28
+ using Microsoft . OpenApi . Hidi . Utilities ;
18
29
using Microsoft . OpenApi . Models ;
19
30
using Microsoft . OpenApi . OData ;
20
31
using Microsoft . OpenApi . Readers ;
21
32
using Microsoft . OpenApi . Services ;
22
33
using Microsoft . OpenApi . Writers ;
23
34
using static Microsoft . OpenApi . Hidi . OpenApiSpecVersionHelper ;
24
- using System . Threading ;
25
- using System . Xml . Xsl ;
26
- using System . Xml ;
27
- using System . Reflection ;
28
- using Microsoft . OpenApi . Hidi . Options ;
29
- using Microsoft . Extensions . Configuration ;
30
- using Microsoft . OpenApi . Hidi . Utilities ;
31
- using Microsoft . OpenApi . Hidi . Formatters ;
32
- using System . Linq ;
33
- using Microsoft . OpenApi . ApiManifest . OpenAI ;
34
- using Microsoft . OpenApi . ApiManifest ;
35
35
36
36
namespace Microsoft . OpenApi . Hidi
37
37
{
38
- internal class OpenApiService
38
+ internal static class OpenApiService
39
39
{
40
40
/// <summary>
41
41
/// Implementation of the transform command
@@ -70,7 +70,8 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
70
70
71
71
// If ApiManifest is provided, set the referenced OpenAPI document
72
72
var apiDependency = await FindApiDependency ( options . FilterOptions ? . FilterByApiManifest , logger , cancellationToken ) ;
73
- if ( apiDependency != null ) {
73
+ if ( apiDependency != null )
74
+ {
74
75
options . OpenApi = apiDependency . ApiDescripionUrl ;
75
76
}
76
77
@@ -207,7 +208,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
207
208
// Get OpenAPI document either from OpenAPI or CSDL
208
209
private static async Task < OpenApiDocument > GetOpenApi ( HidiOptions options , ILogger logger , CancellationToken cancellationToken , string metadataVersion = null )
209
210
{
210
-
211
+
211
212
OpenApiDocument document ;
212
213
Stream stream ;
213
214
@@ -257,7 +258,7 @@ private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogg
257
258
if ( ! string . IsNullOrEmpty ( filterbyoperationids ) )
258
259
{
259
260
logger . LogTrace ( "Creating predicate based on the operationIds supplied." ) ;
260
- predicate = OpenApiFilterService . CreatePredicate ( operationIds : filterbyoperationids ) ;
261
+ predicate = OpenApiFilterService . CreatePredicate ( tags : filterbyoperationids ) ;
261
262
262
263
}
263
264
if ( ! string . IsNullOrEmpty ( filterbytags ) )
@@ -280,10 +281,10 @@ private static Dictionary<string, List<string>> GetRequestUrlsFromManifest(ApiDe
280
281
{
281
282
// Get the request URLs from the API Dependencies in the API manifest
282
283
var requests = apiDependency
283
- . Requests . Where ( r => ! r . Exclude )
284
- . Select ( r => new { UriTemplate = r . UriTemplate , Method = r . Method } )
285
- . GroupBy ( r => r . UriTemplate )
286
- . ToDictionary ( g => g . Key , g => g . Select ( r => r . Method ) . ToList ( ) ) ;
284
+ . Requests . Where ( static r => ! r . Exclude )
285
+ . Select ( static r => new { UriTemplate = r . UriTemplate , Method = r . Method } )
286
+ . GroupBy ( static r => r . UriTemplate )
287
+ . ToDictionary ( static g => g . Key , static g => g . Select ( static r => r . Method ) . ToList ( ) ) ;
287
288
// This makes the assumption that the UriTemplate in the ApiManifest matches exactly the UriTemplate in the OpenAPI document
288
289
// This does not need to be the case. The URI template in the API manifest could map to a set of OpenAPI paths.
289
290
// Additional logic will be required to handle this scenario. I sugggest we build this into the OpenAPI.Net library at some point.
@@ -699,51 +700,54 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
699
700
700
701
internal async static Task PluginManifest ( HidiOptions options , ILogger logger , CancellationToken cancellationToken )
701
702
{
702
- // If ApiManifest is provided, set the referenced OpenAPI document
703
- var apiDependency = await FindApiDependency ( options . FilterOptions ? . FilterByApiManifest , logger , cancellationToken ) ;
704
- if ( apiDependency != null ) {
705
- options . OpenApi = apiDependency . ApiDescripionUrl ;
706
- }
703
+ // If ApiManifest is provided, set the referenced OpenAPI document
704
+ var apiDependency = await FindApiDependency ( options . FilterOptions ? . FilterByApiManifest , logger , cancellationToken ) ;
705
+ if ( apiDependency != null )
706
+ {
707
+ options . OpenApi = apiDependency . ApiDescripionUrl ;
708
+ }
707
709
708
- // Load OpenAPI document
709
- OpenApiDocument document = await GetOpenApi ( options , logger , cancellationToken , options . MetadataVersion ) ;
710
+ // Load OpenAPI document
711
+ OpenApiDocument document = await GetOpenApi ( options , logger , cancellationToken , options . MetadataVersion ) ;
710
712
711
- cancellationToken . ThrowIfCancellationRequested ( ) ;
713
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
712
714
713
- if ( options . FilterOptions != null )
714
- {
715
- document = ApplyFilters ( options , logger , apiDependency , null , document ) ;
716
- }
715
+ if ( options . FilterOptions != null )
716
+ {
717
+ document = ApplyFilters ( options , logger , apiDependency , null , document ) ;
718
+ }
717
719
718
- // Ensure path in options.OutputFolder exists
719
- var outputFolder = new DirectoryInfo ( options . OutputFolder ) ;
720
- if ( ! outputFolder . Exists )
720
+ // Ensure path in options.OutputFolder exists
721
+ var outputFolder = new DirectoryInfo ( options . OutputFolder ) ;
722
+ if ( ! outputFolder . Exists )
723
+ {
724
+ outputFolder . Create ( ) ;
725
+ }
726
+ // Write OpenAPI to Output folder
727
+ options . Output = new FileInfo ( Path . Combine ( options . OutputFolder , "openapi.json" ) ) ;
728
+ options . TerseOutput = true ;
729
+ WriteOpenApi ( options , OpenApiFormat . Json , OpenApiSpecVersion . OpenApi3_0 , document , logger ) ;
730
+
731
+ // Create OpenAIPluginManifest from ApiDependency and OpenAPI document
732
+ var manifest = new OpenAIPluginManifest ( )
733
+ {
734
+ NameForHuman = document . Info . Title ,
735
+ DescriptionForHuman = document . Info . Description ,
736
+ Api = new ( )
721
737
{
722
- outputFolder . Create ( ) ;
738
+ Type = "openapi" ,
739
+ Url = "./openapi.json"
723
740
}
724
- // Write OpenAPI to Output folder
725
- options . Output = new FileInfo ( Path . Combine ( options . OutputFolder , "openapi.json" ) ) ;
726
- options . TerseOutput = true ;
727
- WriteOpenApi ( options , OpenApiFormat . Json , OpenApiSpecVersion . OpenApi3_0 , document , logger ) ;
728
-
729
- // Create OpenAIPluginManifest from ApiDependency and OpenAPI document
730
- var manifest = new OpenAIPluginManifest ( ) {
731
- NameForHuman = document . Info . Title ,
732
- DescriptionForHuman = document . Info . Description ,
733
- Api = new ( ) {
734
- Type = "openapi" ,
735
- Url = "./openapi.json"
736
- }
737
- } ;
738
- manifest . NameForModel = manifest . NameForHuman ;
739
- manifest . DescriptionForModel = manifest . DescriptionForHuman ;
740
-
741
- // Write OpenAIPluginManifest to Output folder
742
- var manifestFile = new FileInfo ( Path . Combine ( options . OutputFolder , "ai-plugin.json" ) ) ;
743
- using var file = new FileStream ( manifestFile . FullName , FileMode . Create ) ;
744
- using var jsonWriter = new Utf8JsonWriter ( file , new JsonWriterOptions { Indented = true } ) ;
745
- manifest . Write ( jsonWriter ) ;
746
- jsonWriter . Flush ( ) ;
741
+ } ;
742
+ manifest . NameForModel = manifest . NameForHuman ;
743
+ manifest . DescriptionForModel = manifest . DescriptionForHuman ;
744
+
745
+ // Write OpenAIPluginManifest to Output folder
746
+ var manifestFile = new FileInfo ( Path . Combine ( options . OutputFolder , "ai-plugin.json" ) ) ;
747
+ using var file = new FileStream ( manifestFile . FullName , FileMode . Create ) ;
748
+ using var jsonWriter = new Utf8JsonWriter ( file , new JsonWriterOptions { Indented = true } ) ;
749
+ manifest . Write ( jsonWriter ) ;
750
+ jsonWriter . Flush ( ) ;
747
751
}
748
752
}
749
753
}
0 commit comments