-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathSwaggerGenOptionsExtensions.cs
More file actions
33 lines (28 loc) · 947 Bytes
/
SwaggerGenOptionsExtensions.cs
File metadata and controls
33 lines (28 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Reflection;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace DocumentationSnippets;
public static class SwaggerGenOptionsExtensions
{
public static void Configure(SwaggerGenOptions options)
{
// begin-snippet: SwaggerGen-DocInclusionPredicate
options.DocInclusionPredicate((docName, apiDesc) =>
{
if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo))
{
return false;
}
var versions = methodInfo.DeclaringType?
.GetCustomAttributes(true)
.OfType<ApiVersionAttribute>()
.SelectMany(attribute => attribute.Versions) ?? [];
return versions.Any(version => $"v{version}" == docName);
});
// end-snippet
}
}
[AttributeUsage(AttributeTargets.Method)]
public class ApiVersionAttribute : Attribute
{
public List<string> Versions { get;set; } = [];
}