From f371e5d30868e7df9558e195c4caf9d430b04ad1 Mon Sep 17 00:00:00 2001 From: Chebotov Nikolay Date: Sat, 29 Feb 2020 09:31:47 +0300 Subject: [PATCH] Add feature: remove Paths and Definitions from OpenApi documentation for all controller actions without accepted roles. --- CHANGELOG.md | 4 +++ README.md | 5 +++- .../Extensions/OpenApiDocumentExtensions.cs | 28 ++++++++++++++++++- .../Factories/ApiDescriptionFactory.cs | 13 +++++++-- ...athsAndDefinitionsByRolesDocumentFilter.cs | 2 +- ...e.Swashbuckle.AspNetCore.Extensions.csproj | 8 +++--- ...hase.Swashbuckle.AspNetCore.Extensions.xml | 14 ++++++++-- test/WebApi3.1-Swashbuckle/Startup.cs | 3 ++ 8 files changed, 66 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 462ebcc..e46e8d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ These are the changes to each version that has been released on the [nuget](https://www.nuget.org/packages/Unchase.Swashbuckle.AspNetCore.Extensions/). +## v2.2.1 `(2020-02-29)` + +- [x] Add feature: remove Paths and Definitions from OpenApi documentation for all controller actions without accepted roles + ## v2.2.0 `(2020-02-28)` - [x] Add feature: remove Paths and Definitions from OpenApi documentation for specific controller action without accepted roles diff --git a/README.md b/README.md index 7447528..439bbd4 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ public void ConfigureServices(IServiceCollection services) } ``` -- Since [v2.2.0](https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/releases/tag/v2.2.0) you can hide Paths and Definitions from OpenApi documentation for specific controller action without accepted roles like this: +- Since [v2.2.1](https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/releases/tag/v2.2.1) you can hide Paths and Definitions from OpenApi documentation for specific controller action (or all actions) without accepted roles like this: ```csharp // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -142,6 +142,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // remove Paths and Components from OpenApi documentation for specific controller action without accepted roles openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesFor(controller => nameof(controller.SomeAction), new List {"AcceptedRole"}); + + // remove Paths and Components from OpenApi documentation for all controller actions without accepted roles + openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesForController(new List {"AcceptedRole"}); }); }); diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/OpenApiDocumentExtensions.cs b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/OpenApiDocumentExtensions.cs index 322b254..be64f9a 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/OpenApiDocumentExtensions.cs +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/OpenApiDocumentExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Mvc; using Microsoft.OpenApi.Models; @@ -14,7 +15,7 @@ namespace Unchase.Swashbuckle.AspNetCore.Extensions.Extensions public static class OpenApiDocumentExtensions { /// - /// Remove Paths and Components from OpenApi documentation without accepted roles. + /// Remove Paths and Components from OpenApi documentation for specific controller action without accepted roles. /// /// . /// Action name selector. @@ -38,5 +39,30 @@ public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesFor + /// Remove Paths and Components from OpenApi documentation for specific controller without accepted roles. + /// + /// . + /// Collection of accepted roles. + /// + /// Returns . + /// + public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesForController(this OpenApiDocument openApiDoc, + IReadOnlyList acceptedRoles) where TController : class, new() + { + var paths = new Dictionary(); + foreach (var methodInfo in typeof(TController).GetMethods().Where(m => !m.IsSpecialName)) + { + var actionDescriptor = ApiDescriptionFactory.Create(c => methodInfo.Name, typeof(TController).GetCustomAttribute().Template)?.ActionDescriptor; + if (actionDescriptor != null) + { + paths.Add(((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)actionDescriptor).MethodInfo, actionDescriptor.AttributeRouteInfo.Template); + } + } + HidePathsAndDefinitionsByRolesDocumentFilter.RemovePathsAndComponents(openApiDoc, paths, openApiDoc.Components.Schemas, acceptedRoles); + + return openApiDoc; + } } } diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Factories/ApiDescriptionFactory.cs b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Factories/ApiDescriptionFactory.cs index 44cac4c..b9cd6bc 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Factories/ApiDescriptionFactory.cs +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Factories/ApiDescriptionFactory.cs @@ -39,7 +39,16 @@ internal static ApiDescription Create( IEnumerable supportedRequestFormats = null, IEnumerable supportedResponseTypes = null) { - var methodInfo = controllerType.GetMethod(actionName); + MethodInfo methodInfo; + + try + { + methodInfo = controllerType.GetMethod(actionName); + } + catch (AmbiguousMatchException) + { + return null; + } if (methodInfo == null) return null; @@ -48,7 +57,7 @@ internal static ApiDescription Create( var routAttr = controllerType.GetCustomAttributes().OfType().LastOrDefault(); - if (string.IsNullOrWhiteSpace(actionDescriptor.AttributeRouteInfo.Template)) + if (string.IsNullOrWhiteSpace(actionDescriptor?.AttributeRouteInfo?.Template)) return null; //throw new InvalidOperationException($"HttpMethod attribute of \"{methodInfo.Name}\" action in \"{controllerType.Name}\" controller must have a template specified."); diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/HidePathsAndDefinitionsByRolesDocumentFilter.cs b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/HidePathsAndDefinitionsByRolesDocumentFilter.cs index 6093c7a..5782d96 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/HidePathsAndDefinitionsByRolesDocumentFilter.cs +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/HidePathsAndDefinitionsByRolesDocumentFilter.cs @@ -143,7 +143,7 @@ private static List GetRequiredDefinitions(IDictionary /// . /// Dictionary of openApi paths with keys. - /// Dictionary with openApi schemas with schame name keys. + /// Dictionary with openApi schemas with scheme name keys. /// Collection of accepted roles. internal static void RemovePathsAndComponents(OpenApiDocument openApiDoc, IDictionary paths, IDictionary schemas, IReadOnlyList acceptedRoles) { diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.csproj b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.csproj index 4884ce9..6789f7c 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.csproj +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.csproj @@ -6,7 +6,7 @@ Unchase Some additional useful extensions for Swashbuckle.AspNetCore. Copyright 2019 Unchase - https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/blob/master/LICENSE + https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions git @@ -14,9 +14,9 @@ 7.3 https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/blob/master/assets/icon.png?raw=true - 2.2.0 - 2.2.0.0 - 2.2.0.0 + 2.2.1 + 2.2.1.0 + 2.2.1.0 false Unchase.Swashbuckle.AspNetCore.Extensions.xml diff --git a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.xml b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.xml index d154045..8c7b3c3 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.xml +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Unchase.Swashbuckle.AspNetCore.Extensions.xml @@ -11,7 +11,7 @@ - Remove Paths and Components from OpenApi documentation without accepted roles. + Remove Paths and Components from OpenApi documentation for specific controller action without accepted roles. . Action name selector. @@ -20,6 +20,16 @@ Returns . + + + Remove Paths and Components from OpenApi documentation for specific controller without accepted roles. + + . + Collection of accepted roles. + + Returns . + + Extension methods for . @@ -159,7 +169,7 @@ . Dictionary of openApi paths with keys. - Dictionary with openApi schemas with schame name keys. + Dictionary with openApi schemas with scheme name keys. Collection of accepted roles. diff --git a/test/WebApi3.1-Swashbuckle/Startup.cs b/test/WebApi3.1-Swashbuckle/Startup.cs index 3e79c8d..7517acb 100644 --- a/test/WebApi3.1-Swashbuckle/Startup.cs +++ b/test/WebApi3.1-Swashbuckle/Startup.cs @@ -113,6 +113,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // remove Paths and Components from OpenApi documentation for specific controller action without accepted roles openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesFor(controller => nameof(controller.HidedAction), new List {"AcceptedRole"}); + + // remove Paths and Components from OpenApi documentation for all controller actions without accepted roles + openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesForController(new List {"AcceptedRole"}); }); });