@@ -67,21 +67,18 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
67
67
OpenApiFormat openApiFormat = options . OpenApiFormat ?? ( ! string . IsNullOrEmpty ( options . OpenApi ) ? GetOpenApiFormat ( options . OpenApi , logger ) : OpenApiFormat . Yaml ) ;
68
68
OpenApiSpecVersion openApiVersion = options . Version != null ? TryParseOpenApiSpecVersion ( options . Version ) : OpenApiSpecVersion . OpenApi3_0 ;
69
69
70
- // If API Manifest is provided, load it, use it get the OpenAPI path
71
- ApiManifestDocument apiManifest = null ;
72
- if ( ! string . IsNullOrEmpty ( options . FilterOptions ? . FilterByApiManifest ) )
73
- {
74
- using ( var fileStream = await GetStream ( options . FilterOptions . FilterByApiManifest , logger , cancellationToken ) ) {
75
- apiManifest = ApiManifestDocument . Load ( JsonDocument . Parse ( fileStream ) . RootElement ) ;
76
- }
77
- options . OpenApi = apiManifest . ApiDependencies [ 0 ] . ApiDescripionUrl ;
70
+ // If ApiManifest is provided, set the referenced OpenAPI document
71
+ var apiDependency = await FindApiDependency ( options . FilterOptions ? . FilterByApiManifest , logger , cancellationToken ) ;
72
+ if ( apiDependency != null ) {
73
+ options . OpenApi = apiDependency . ApiDescripionUrl ;
78
74
}
79
75
80
76
// If Postman Collection is provided, load it
81
77
JsonDocument postmanCollection = null ;
82
78
if ( ! String . IsNullOrEmpty ( options . FilterOptions ? . FilterByCollection ) )
83
79
{
84
- using ( var collectionStream = await GetStream ( options . FilterOptions . FilterByCollection , logger , cancellationToken ) ) {
80
+ using ( var collectionStream = await GetStream ( options . FilterOptions . FilterByCollection , logger , cancellationToken ) )
81
+ {
85
82
postmanCollection = JsonDocument . Parse ( collectionStream ) ;
86
83
}
87
84
}
@@ -91,7 +88,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
91
88
92
89
if ( options . FilterOptions != null )
93
90
{
94
- document = ApplyFilters ( options , logger , apiManifest , postmanCollection , document , cancellationToken ) ;
91
+ document = ApplyFilters ( options , logger , apiDependency , postmanCollection , document , cancellationToken ) ;
95
92
}
96
93
97
94
var languageFormat = options . SettingsConfig ? . GetSection ( "LanguageFormat" ) ? . Value ;
@@ -118,20 +115,50 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
118
115
}
119
116
}
120
117
121
- private static OpenApiDocument ApplyFilters ( HidiOptions options , ILogger logger , ApiManifestDocument apiManifest , JsonDocument postmanCollection , OpenApiDocument document , CancellationToken cancellationToken )
118
+ private static async Task < ApiDependency > FindApiDependency ( string apiManifestPath , ILogger logger , CancellationToken cancellationToken )
119
+ {
120
+ ApiDependency apiDependency = null ;
121
+ // If API Manifest is provided, load it, use it get the OpenAPI path
122
+ ApiManifestDocument apiManifest = null ;
123
+ if ( ! string . IsNullOrEmpty ( apiManifestPath ) )
124
+ {
125
+ // Extract fragment identifier if passed as the name of the ApiDependency
126
+ var apiManifestRef = apiManifestPath . Split ( '#' ) ;
127
+ string apiDependencyName = null ;
128
+ if ( apiManifestRef . Length > 1 )
129
+ {
130
+ apiDependencyName = apiManifestRef [ 1 ] ;
131
+ }
132
+ using ( var fileStream = await GetStream ( apiManifestRef [ 0 ] , logger , cancellationToken ) )
133
+ {
134
+ apiManifest = ApiManifestDocument . Load ( JsonDocument . Parse ( fileStream ) . RootElement ) ;
135
+ }
136
+ if ( apiDependencyName != null )
137
+ {
138
+ apiDependency = apiManifest . ApiDependencies [ apiDependencyName ] ;
139
+ }
140
+ else
141
+ {
142
+ apiDependency = apiManifest . ApiDependencies . First ( ) . Value ;
143
+ }
144
+ }
145
+
146
+ return apiDependency ;
147
+ }
148
+
149
+ private static OpenApiDocument ApplyFilters ( HidiOptions options , ILogger logger , ApiDependency apiDependency , JsonDocument postmanCollection , OpenApiDocument document , CancellationToken cancellationToken )
122
150
{
123
151
Dictionary < string , List < string > > requestUrls = null ;
124
- if ( apiManifest != null )
152
+ if ( apiDependency != null )
125
153
{
126
- requestUrls = GetRequestUrlsFromManifest ( apiManifest , document ) ;
154
+ requestUrls = GetRequestUrlsFromManifest ( apiDependency , document ) ;
127
155
}
128
156
else if ( postmanCollection != null )
129
157
{
130
158
requestUrls = EnumerateJsonDocument ( postmanCollection . RootElement , requestUrls ) ;
131
159
logger . LogTrace ( "Finished fetching the list of paths and Http methods defined in the Postman collection." ) ;
132
160
}
133
161
134
-
135
162
logger . LogTrace ( "Creating predicate from filter options." ) ;
136
163
var predicate = FilterOpenApiDocument ( options . FilterOptions . FilterByOperationIds ,
137
164
options . FilterOptions . FilterByTags ,
@@ -253,15 +280,14 @@ private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csd
253
280
return predicate ;
254
281
}
255
282
256
- private static Dictionary < string , List < string > > GetRequestUrlsFromManifest ( ApiManifestDocument apiManifestDocument , OpenApiDocument document )
283
+ private static Dictionary < string , List < string > > GetRequestUrlsFromManifest ( ApiDependency apiDependency , OpenApiDocument document )
257
284
{
258
285
// Get the request URLs from the API Dependencies in the API manifest that have a baseURL that matches the server URL in the OpenAPI document
259
286
var serversUrls = document . Servers . Select ( s => s . Url ) ;
260
- var requests = apiManifestDocument . ApiDependencies
261
- . Where ( a => serversUrls . Any ( s => s == a . BaseUrl ) )
262
- . SelectMany ( ad => ad . Requests . Where ( r => r . Exclude == false )
263
- . Select ( r=> new { BaseUrl = ad . BaseUrl , UriTemplate = r . UriTemplate , Method = r . Method } ) )
264
- . GroupBy ( r => r . BaseUrl . TrimEnd ( '/' ) + r . UriTemplate ) // The OpenApiFilterService expects non-relative URLs.
287
+ var requests = apiDependency
288
+ . Requests . Where ( r => r . Exclude == false )
289
+ . Select ( r=> new { UriTemplate = r . UriTemplate , Method = r . Method } )
290
+ . GroupBy ( r => r . UriTemplate )
265
291
. ToDictionary ( g => g . Key , g => g . Select ( r => r . Method ) . ToList ( ) ) ;
266
292
// This makes the assumption that the UriTemplate in the ApiManifest matches exactly the UriTemplate in the OpenAPI document
267
293
// This does not need to be the case. The URI template in the API manifest could map to a set of OpenAPI paths.
0 commit comments