Skip to content

Commit

Permalink
Add "RemovePathsAndComponentsWithoutAcceptedRolesFor" overloaded exte…
Browse files Browse the repository at this point in the history
…nsion method (with "actionName" parameter).
  • Loading branch information
Chebotov Nikolay committed Mar 2, 2020
1 parent 466eb24 commit a8ec15c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<SomeController>(controller => nameof(controller.SomeAction), new List<string> {"AcceptedRole"});

// or
//openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesFor<SomeController>(nameof(SomeController.SomeAction), new List<string> { "AcceptedRole" });

// remove Paths and Components from OpenApi documentation for all controller actions without accepted roles
openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesForController<AnotherController>(new List<string> {"AcceptedRole"});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesFor<TC
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 != null)
{
var paths = new Dictionary<MethodInfo, string>
{
{ ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)actionDescriptor).MethodInfo, 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>
Expand Down
4 changes: 4 additions & 0 deletions test/WebApi3.1-Swashbuckle/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HidedController>(controller => nameof(controller.HidedAction), new List<string> {"AcceptedRole"});

// or
//openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesFor<HidedController>(nameof(HidedController.HidedAction), new List<string> { "AcceptedRole" });


// remove Paths and Components from OpenApi documentation for all controller actions without accepted roles
openApiDoc.RemovePathsAndComponentsWithoutAcceptedRolesForController<TodoController>(new List<string> {"AcceptedRole"});

Expand Down

0 comments on commit a8ec15c

Please sign in to comment.