diff --git a/CHANGELOG.md b/CHANGELOG.md index f2f9475..8d060b6 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.4 `(2020-03-02)` + +- [x] Add `RemovePathsAndComponentsWithoutAcceptedRolesFor` overloaded extension method (with `actionName` parameter) + ## v2.2.3 `(2020-03-02)` - [x] Allow to use `RemovePathsAndComponentsWithoutAcceptedRolesForController` extension method without `new()` constraint diff --git a/README.md b/README.md index 00efe4c..6b651a0 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,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"}); + // or + //openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesFor(nameof(SomeController.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 5dad186..499a4ac 100644 --- a/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/OpenApiDocumentExtensions.cs +++ b/src/Unchase.Swashbuckle.AspNetCore.Extensions/Extensions/OpenApiDocumentExtensions.cs @@ -40,6 +40,32 @@ public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesFor + /// Remove Paths and Components from OpenApi documentation for specific controller action without accepted roles. + /// + /// . + /// Action name. + /// Collection of accepted roles. + /// + /// Returns . + /// + public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesFor(this OpenApiDocument openApiDoc, string actionName, + IReadOnlyList acceptedRoles) where TController : class + { + var actionDescriptor = ApiDescriptionFactory.Create(typeof(TController), actionName, typeof(TController).GetCustomAttribute().Template)?.ActionDescriptor; + if (actionDescriptor != null) + { + var paths = new Dictionary + { + { ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)actionDescriptor).MethodInfo, actionDescriptor.AttributeRouteInfo.Template } + }; + + HidePathsAndDefinitionsByRolesDocumentFilter.RemovePathsAndComponents(openApiDoc, paths, openApiDoc.Components.Schemas, acceptedRoles); + } + + return openApiDoc; + } + /// /// Remove Paths and Components from OpenApi documentation for specific controller without accepted roles. /// diff --git a/test/WebApi3.1-Swashbuckle/Startup.cs b/test/WebApi3.1-Swashbuckle/Startup.cs index 176c35f..aa1161c 100644 --- a/test/WebApi3.1-Swashbuckle/Startup.cs +++ b/test/WebApi3.1-Swashbuckle/Startup.cs @@ -114,6 +114,10 @@ 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"}); + // or + //openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesFor(nameof(HidedController.HidedAction), new List { "AcceptedRole" }); + + // remove Paths and Components from OpenApi documentation for all controller actions without accepted roles openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesForController(new List {"AcceptedRole"});