-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApiVersionOperationFilter.cs
31 lines (29 loc) · 1.09 KB
/
ApiVersionOperationFilter.cs
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
using System.Linq;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Jimismith.Versioning.Example
{
public class ApiVersionOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
var actionApiVersionModel = context.ApiDescription.ActionDescriptor?.GetApiVersion();
if (actionApiVersionModel == null)
{
return;
}
if (actionApiVersionModel.DeclaredApiVersions.Any())
{
operation.Produces = operation.Produces
.SelectMany(p => actionApiVersionModel.DeclaredApiVersions
.Select(version => $"{p};v={version.ToString()}")).ToList();
}
else
{
operation.Produces = operation.Produces
.SelectMany(p => actionApiVersionModel.ImplementedApiVersions.OrderByDescending(v => v)
.Select(version => $"{p};v={version.ToString()}")).ToList();
}
}
}
}