4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . Linq ;
7
+ using System . Text . RegularExpressions ;
7
8
using Microsoft . OpenApi . Models ;
8
9
9
10
namespace Microsoft . OpenApi . Services
@@ -17,8 +18,9 @@ public static class OpenApiFilterService
17
18
/// Create predicate function based on passed query parameters
18
19
/// </summary>
19
20
/// <param name="operationIds">Comma delimited list of operationIds or * for all operations.</param>
21
+ /// <param name="tags">Comma delimited list of tags or a single regex.</param>
20
22
/// <returns>A predicate.</returns>
21
- public static Func < OpenApiOperation , bool > CreatePredicate ( string operationIds )
23
+ public static Func < OpenApiOperation , bool > CreatePredicate ( string operationIds = null , string tags = null )
22
24
{
23
25
string predicateSource = null ;
24
26
@@ -37,10 +39,26 @@ public static Func<OpenApiOperation, bool> CreatePredicate(string operationIds)
37
39
38
40
predicateSource = $ "operationIds: { operationIds } ";
39
41
}
42
+ else if ( tags != null )
43
+ {
44
+ var tagsArray = tags . Split ( ',' ) ;
45
+ if ( tagsArray . Length == 1 )
46
+ {
47
+ var regex = new Regex ( tagsArray [ 0 ] ) ;
48
+
49
+ predicate = ( o ) => o . Tags . Any ( t => regex . IsMatch ( t . Name ) ) ;
50
+ }
51
+ else
52
+ {
53
+ predicate = ( o ) => o . Tags . Any ( t => tagsArray . Contains ( t . Name ) ) ;
54
+ }
55
+
56
+ predicateSource = $ "tags: { tags } ";
57
+ }
40
58
41
59
else
42
60
{
43
- throw new InvalidOperationException ( "OperationId needs to be specified." ) ;
61
+ throw new InvalidOperationException ( "Either operationId(s) or tag(s) need to be specified." ) ;
44
62
}
45
63
46
64
return predicate ;
0 commit comments