forked from abpframework/abp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request abpframework#11675 from abpframework/EngincanV/swa…
…gger Add support to hide ABP endpoints on Swagger UI
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...bp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Volo.Abp.Swashbuckle; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class AbpSwaggerGenOptionsExtensions | ||
{ | ||
public static void HideAbpEndpoints(this SwaggerGenOptions swaggerGenOptions) | ||
{ | ||
swaggerGenOptions.DocumentFilter<AbpSwashbuckleDocumentFilter>(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace Volo.Abp.Swashbuckle; | ||
|
||
public class AbpSwashbuckleDocumentFilter : IDocumentFilter | ||
{ | ||
protected string[] ActionUrlPrefixes = new[] {"Volo.Abp"}; | ||
|
||
public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) | ||
{ | ||
var actionUrls = context.ApiDescriptions | ||
.Select(apiDescription => apiDescription.ActionDescriptor) | ||
.Where(actionDescriptor => !string.IsNullOrEmpty(actionDescriptor.DisplayName) && | ||
ActionUrlPrefixes.Any(actionUrlPrefix => !actionDescriptor.DisplayName.Contains(actionUrlPrefix))) | ||
.DistinctBy(actionDescriptor => actionDescriptor.AttributeRouteInfo?.Template) | ||
.Select(actionDescriptor => actionDescriptor.AttributeRouteInfo?.Template.EnsureStartsWith('/')) | ||
.Where(actionUrl => !string.IsNullOrEmpty(actionUrl)) | ||
.ToList(); | ||
|
||
swaggerDoc | ||
.Paths | ||
.RemoveAll(path => !actionUrls.Contains(path.Key)); | ||
} | ||
} |