-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathOpenApiDocumentExtensions.cs
More file actions
108 lines (95 loc) · 5.19 KB
/
OpenApiDocumentExtensions.cs
File metadata and controls
108 lines (95 loc) · 5.19 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi;
using Unchase.Swashbuckle.AspNetCore.Extensions.Factories;
using Unchase.Swashbuckle.AspNetCore.Extensions.Filters;
namespace Unchase.Swashbuckle.AspNetCore.Extensions.Extensions
{
/// <summary>
/// Extension methods for <see cref=" OpenApiDocument"/>.
/// </summary>
public static class OpenApiDocumentExtensions
{
#region Extension methods
/// <summary>
/// Remove Paths and Components from OpenApi documentation for specific controller action without accepted roles.
/// </summary>
/// <param name="openApiDoc"><see cref="OpenApiDocument"/>.</param>
/// <param name="actionNameSelector">Action name selector.</param>
/// <param name="acceptedRoles">Collection of accepted roles.</param>
/// <returns>
/// Returns <see cref="OpenApiDocument"/>.
/// </returns>
public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesFor<TController>(
this OpenApiDocument openApiDoc,
Func<TController, string> actionNameSelector,
IReadOnlyList<string> acceptedRoles)
where TController : class, new()
{
var actionDescriptor = ApiDescriptionFactory.Create(actionNameSelector, typeof(TController).GetCustomAttribute<RouteAttribute>()?.Template)?.ActionDescriptor;
if (actionDescriptor?.AttributeRouteInfo?.Template != null && openApiDoc.Components?.Schemas != null)
{
var paths = new Dictionary<(MethodInfo, Type), string>
{
{ (((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)actionDescriptor).MethodInfo, typeof(TController)), actionDescriptor.AttributeRouteInfo.Template }
};
HidePathsAndDefinitionsByRolesDocumentFilter.RemovePathsAndComponents(openApiDoc, paths, openApiDoc.Components.Schemas, acceptedRoles);
}
return openApiDoc;
}
/// <summary>
/// Remove Paths and Components from OpenApi documentation for specific controller action without accepted roles.
/// </summary>
/// <param name="openApiDoc"><see cref="OpenApiDocument"/>.</param>
/// <param name="actionName">Action name.</param>
/// <param name="acceptedRoles">Collection of accepted roles.</param>
/// <returns>
/// Returns <see cref="OpenApiDocument"/>.
/// </returns>
public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesFor<TController>(
this OpenApiDocument openApiDoc,
string actionName,
IReadOnlyList<string> acceptedRoles) where TController : class
{
var actionDescriptor = ApiDescriptionFactory.Create(typeof(TController), actionName, typeof(TController).GetCustomAttribute<RouteAttribute>()?.Template)?.ActionDescriptor;
if (actionDescriptor?.AttributeRouteInfo?.Template != null && openApiDoc.Components?.Schemas != null)
{
var paths = new Dictionary<(MethodInfo, Type), string>
{
{ (((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)actionDescriptor).MethodInfo, typeof(TController)), actionDescriptor.AttributeRouteInfo.Template }
};
HidePathsAndDefinitionsByRolesDocumentFilter.RemovePathsAndComponents(openApiDoc, paths, openApiDoc.Components.Schemas, acceptedRoles);
}
return openApiDoc;
}
/// <summary>
/// Remove Paths and Components from OpenApi documentation for specific controller without accepted roles.
/// </summary>
/// <param name="openApiDoc"><see cref="OpenApiDocument"/>.</param>
/// <param name="acceptedRoles">Collection of accepted roles.</param>
/// <returns>
/// Returns <see cref="OpenApiDocument"/>.
/// </returns>
public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesForController<TController>(
this OpenApiDocument openApiDoc,
IReadOnlyList<string> acceptedRoles) where TController : class
{
if (openApiDoc.Components?.Schemas == null) return openApiDoc;
var paths = new Dictionary<(MethodInfo, Type), string>();
foreach (var methodInfo in typeof(TController).GetMethods().Where(m => !m.IsSpecialName))
{
var actionDescriptor = ApiDescriptionFactory.Create<TController>(methodInfo, typeof(TController).GetCustomAttribute<RouteAttribute>()?.Template)?.ActionDescriptor;
if (actionDescriptor?.AttributeRouteInfo?.Template != null)
{
paths.Add((((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)actionDescriptor).MethodInfo, typeof(TController)), actionDescriptor.AttributeRouteInfo.Template);
}
}
HidePathsAndDefinitionsByRolesDocumentFilter.RemovePathsAndComponents(openApiDoc, paths, openApiDoc.Components.Schemas, acceptedRoles);
return openApiDoc;
}
#endregion
}
}