Skip to content

Commit

Permalink
Add feature: remove Paths and Definitions from OpenApi documentation …
Browse files Browse the repository at this point in the history
…for all controller actions without accepted roles.
  • Loading branch information
unchase committed Feb 29, 2020
1 parent 7d89e80 commit f371e5d
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 11 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.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
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<SomeController>(controller => nameof(controller.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
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
Expand All @@ -14,7 +15,7 @@ namespace Unchase.Swashbuckle.AspNetCore.Extensions.Extensions
public static class OpenApiDocumentExtensions
{
/// <summary>
/// Remove Paths and Components from OpenApi documentation without accepted roles.
/// 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>
Expand All @@ -38,5 +39,30 @@ public static OpenApiDocument RemovePathsAndComponentsWithoutAcceptedRolesFor<TC

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, new()
{
var paths = new Dictionary<MethodInfo, string>();
foreach (var methodInfo in typeof(TController).GetMethods().Where(m => !m.IsSpecialName))
{
var actionDescriptor = ApiDescriptionFactory.Create<TController>(c => methodInfo.Name, typeof(TController).GetCustomAttribute<RouteAttribute>().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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ internal static ApiDescription Create(
IEnumerable<ApiRequestFormat> supportedRequestFormats = null,
IEnumerable<ApiResponseType> supportedResponseTypes = null)
{
var methodInfo = controllerType.GetMethod(actionName);
MethodInfo methodInfo;

try
{
methodInfo = controllerType.GetMethod(actionName);
}
catch (AmbiguousMatchException)
{
return null;
}

if (methodInfo == null)
return null;
Expand All @@ -48,7 +57,7 @@ internal static ApiDescription Create(

var routAttr = controllerType.GetCustomAttributes().OfType<RouteAttribute>().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.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private static List<string> GetRequiredDefinitions(IDictionary<string, OpenApiSc
/// </summary>
/// <param name="openApiDoc"><see cref="OpenApiDocument"/>.</param>
/// <param name="paths">Dictionary of openApi paths with <see cref="MethodInfo"/> keys.</param>
/// <param name="schemas">Dictionary with openApi schemas with schame name keys.</param>
/// <param name="schemas">Dictionary with openApi schemas with scheme name keys.</param>
/// <param name="acceptedRoles">Collection of accepted roles.</param>
internal static void RemovePathsAndComponents(OpenApiDocument openApiDoc, IDictionary<MethodInfo, string> paths, IDictionary<string, OpenApiSchema> schemas, IReadOnlyList<string> acceptedRoles)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
<Authors>Unchase</Authors>
<Description>Some additional useful extensions for Swashbuckle.AspNetCore.</Description>
<Copyright>Copyright 2019 Unchase</Copyright>
<PackageLicenseUrl>https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/blob/master/LICENSE</PackageLicenseUrl>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageProjectUrl>https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions</PackageProjectUrl>
<RepositoryUrl>https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>Swagger;Swashbuckle;Filters;NSwag</PackageTags>
<NeutralLanguage></NeutralLanguage>
<LangVersion>7.3</LangVersion>
<PackageIconUrl>https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/blob/master/assets/icon.png?raw=true</PackageIconUrl>
<Version>2.2.0</Version>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0.0</FileVersion>
<Version>2.2.1</Version>
<AssemblyVersion>2.2.1.0</AssemblyVersion>
<FileVersion>2.2.1.0</FileVersion>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<DocumentationFile>Unchase.Swashbuckle.AspNetCore.Extensions.xml</DocumentationFile>
</PropertyGroup>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/WebApi3.1-Swashbuckle/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HidedController>(controller => nameof(controller.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 f371e5d

Please sign in to comment.