2
2
// Licensed under the MIT license.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . IO ;
6
7
using System . Linq ;
7
8
using System . Net ;
8
9
using System . Net . Http ;
9
10
using System . Text ;
11
+ using System . Text . Json ;
10
12
using System . Threading . Tasks ;
11
13
using System . Xml . Linq ;
12
14
using Microsoft . OData . Edm . Csdl ;
20
22
21
23
namespace Microsoft . OpenApi . Hidi
22
24
{
23
- static class OpenApiService
25
+ public static class OpenApiService
24
26
{
25
27
public static void ProcessOpenApiDocument (
26
28
string input ,
27
29
FileInfo output ,
28
- OpenApiSpecVersion version ,
29
- OpenApiFormat format ,
30
+ OpenApiSpecVersion ? version ,
31
+ OpenApiFormat ? format ,
30
32
string filterByOperationIds ,
31
33
string filterByTags ,
34
+ string filterByCollection ,
32
35
bool inline ,
33
36
bool resolveExternal )
34
37
{
@@ -61,21 +64,29 @@ public static void ProcessOpenApiDocument(
61
64
) . ReadAsync ( stream ) . GetAwaiter ( ) . GetResult ( ) ;
62
65
63
66
document = result . OpenApiDocument ;
64
-
67
+ Func < string , OperationType ? , OpenApiOperation , bool > predicate ;
68
+
65
69
// Check if filter options are provided, then execute
66
70
if ( ! string . IsNullOrEmpty ( filterByOperationIds ) && ! string . IsNullOrEmpty ( filterByTags ) )
67
71
{
68
72
throw new InvalidOperationException ( "Cannot filter by operationIds and tags at the same time." ) ;
69
73
}
70
-
71
74
if ( ! string . IsNullOrEmpty ( filterByOperationIds ) )
72
75
{
73
- var predicate = OpenApiFilterService . CreatePredicate ( operationIds : filterByOperationIds ) ;
76
+ predicate = OpenApiFilterService . CreatePredicate ( operationIds : filterByOperationIds ) ;
74
77
document = OpenApiFilterService . CreateFilteredDocument ( document , predicate ) ;
75
78
}
76
79
if ( ! string . IsNullOrEmpty ( filterByTags ) )
77
80
{
78
- var predicate = OpenApiFilterService . CreatePredicate ( tags : filterByTags ) ;
81
+ predicate = OpenApiFilterService . CreatePredicate ( tags : filterByTags ) ;
82
+ document = OpenApiFilterService . CreateFilteredDocument ( document , predicate ) ;
83
+ }
84
+
85
+ if ( ! string . IsNullOrEmpty ( filterByCollection ) )
86
+ {
87
+ var fileStream = GetStream ( filterByCollection ) ;
88
+ var requestUrls = ParseJsonCollectionFile ( fileStream ) ;
89
+ predicate = OpenApiFilterService . CreatePredicate ( requestUrls : requestUrls , source : document ) ;
79
90
document = OpenApiFilterService . CreateFilteredDocument ( document , predicate ) ;
80
91
}
81
92
@@ -101,13 +112,16 @@ public static void ProcessOpenApiDocument(
101
112
{
102
113
ReferenceInline = inline ? ReferenceInlineSetting . InlineLocalReferences : ReferenceInlineSetting . DoNotInlineReferences
103
114
} ;
104
- IOpenApiWriter writer = format switch
115
+
116
+ var openApiFormat = format ?? GetOpenApiFormat ( input ) ;
117
+ var openApiVersion = version ?? result . OpenApiDiagnostic . SpecificationVersion ;
118
+ IOpenApiWriter writer = openApiFormat switch
105
119
{
106
120
OpenApiFormat . Json => new OpenApiJsonWriter ( textWriter , settings ) ,
107
121
OpenApiFormat . Yaml => new OpenApiYamlWriter ( textWriter , settings ) ,
108
122
_ => throw new ArgumentException ( "Unknown format" ) ,
109
123
} ;
110
- document . Serialize ( writer , version ) ;
124
+ document . Serialize ( writer , openApiVersion ) ;
111
125
112
126
textWriter . Flush ( ) ;
113
127
}
@@ -178,6 +192,38 @@ private static Stream GetStream(string input)
178
192
return stream ;
179
193
}
180
194
195
+ /// <summary>
196
+ /// Takes in a file stream, parses the stream into a JsonDocument and gets a list of paths and Http methods
197
+ /// </summary>
198
+ /// <param name="stream"> A file stream.</param>
199
+ /// <returns> A dictionary of request urls and http methods from a collection.</returns>
200
+ public static Dictionary < string , List < string > > ParseJsonCollectionFile ( Stream stream )
201
+ {
202
+ var requestUrls = new Dictionary < string , List < string > > ( ) ;
203
+
204
+ // Convert file to JsonDocument
205
+ using var document = JsonDocument . Parse ( stream ) ;
206
+ var root = document . RootElement ;
207
+ var itemElement = root . GetProperty ( "item" ) ;
208
+ foreach ( var requestObject in itemElement . EnumerateArray ( ) . Select ( item => item . GetProperty ( "request" ) ) )
209
+ {
210
+ // Fetch list of methods and urls from collection, store them in a dictionary
211
+ var path = requestObject . GetProperty ( "url" ) . GetProperty ( "raw" ) . ToString ( ) ;
212
+ var method = requestObject . GetProperty ( "method" ) . ToString ( ) ;
213
+
214
+ if ( ! requestUrls . ContainsKey ( path ) )
215
+ {
216
+ requestUrls . Add ( path , new List < string > { method } ) ;
217
+ }
218
+ else
219
+ {
220
+ requestUrls [ path ] . Add ( method ) ;
221
+ }
222
+ }
223
+
224
+ return requestUrls ;
225
+ }
226
+
181
227
internal static void ValidateOpenApiDocument ( string input )
182
228
{
183
229
if ( input == null )
@@ -209,5 +255,10 @@ internal static void ValidateOpenApiDocument(string input)
209
255
210
256
Console . WriteLine ( statsVisitor . GetStatisticsReport ( ) ) ;
211
257
}
258
+
259
+ private static OpenApiFormat GetOpenApiFormat ( string input )
260
+ {
261
+ return ! input . StartsWith ( "http" ) && Path . GetExtension ( input ) == ".json" ? OpenApiFormat . Json : OpenApiFormat . Yaml ;
262
+ }
212
263
}
213
264
}
0 commit comments