Skip to content

Commit c4aed08

Browse files
committed
Add a tags parameter to the filtering service to allow for slicing of OpenApiDocument based on tags provided
1 parent 8a60acf commit c4aed08

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/Microsoft.OpenApi/Services/OpenApiFilterService.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Text.RegularExpressions;
78
using Microsoft.OpenApi.Models;
89

910
namespace Microsoft.OpenApi.Services
@@ -17,8 +18,9 @@ public static class OpenApiFilterService
1718
/// Create predicate function based on passed query parameters
1819
/// </summary>
1920
/// <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>
2022
/// <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)
2224
{
2325
string predicateSource = null;
2426

@@ -37,10 +39,26 @@ public static Func<OpenApiOperation, bool> CreatePredicate(string operationIds)
3739

3840
predicateSource = $"operationIds: {operationIds}";
3941
}
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+
}
4058

4159
else
4260
{
43-
throw new InvalidOperationException("OperationId needs to be specified.");
61+
throw new InvalidOperationException("Either operationId(s) or tag(s) need to be specified.");
4462
}
4563

4664
return predicate;

0 commit comments

Comments
 (0)