-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ApiVersionProcessor.cs
62 lines (55 loc) · 2.87 KB
/
ApiVersionProcessor.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//-----------------------------------------------------------------------
// <copyright file="ApiVersionProcessor.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using NJsonSchema.Infrastructure;
using NSwag.SwaggerGeneration.Processors;
using NSwag.SwaggerGeneration.Processors.Contexts;
namespace NSwag.SwaggerGeneration.Processors
{
/// <summary>An operation processor which replaces {version:apiVersion} route placeholders and filters the included versions.</summary>
public class ApiVersionProcessor : IOperationProcessor
{
/// <summary>Gets the list of versions which should be included in the generated document (leave empty to include all versions).</summary>
public IList<string> IncludedVersions { get; } = new List<string>();
/// <summary>Processes the specified method information.</summary>
/// <param name="context">The processor context.</param>
/// <returns>true if the operation should be added to the Swagger specification.</returns>
public Task<bool> ProcessAsync(OperationProcessorContext context)
{
var versionAttributes = context.MethodInfo.GetCustomAttributes()
.Concat(context.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes())
.Concat(context.ControllerType.GetTypeInfo().GetCustomAttributes())
.Where(a => a.GetType().IsAssignableTo("MapToApiVersionAttribute", TypeNameStyle.Name) ||
a.GetType().IsAssignableTo("ApiVersionAttribute", TypeNameStyle.Name))
.Select(a => (dynamic)a)
.ToArray();
var versionAttribute = versionAttributes.FirstOrDefault();
if (ReflectionExtensions.HasProperty(versionAttribute, "Versions"))
{
var versions = ((IEnumerable)versionAttribute.Versions)
.OfType<object>()
.Select(v => v.ToString())
.ToArray();
var version = versions.FirstOrDefault(v => IncludedVersions.Count == 0 || IncludedVersions.Contains(v));
if (version != null)
{
var operationDescription = context.OperationDescription;
operationDescription.Path = operationDescription.Path.Replace("{version:apiVersion}", version);
return Task.FromResult(true);
}
else
return Task.FromResult(false);
}
return Task.FromResult(true);
}
}
}