From 354af0bc48e6e45b74d259ded16d8a221b10238d Mon Sep 17 00:00:00 2001 From: Engincan VESKE Date: Wed, 23 Feb 2022 09:04:55 +0300 Subject: [PATCH 1/2] Add support to hide ABP endpoints on Swagger UI --- .../SwaggerGenOptionsExtensions.cs | 12 ++++++++ .../AbpSwashbuckleDocumentFilter.cs | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/SwaggerGenOptionsExtensions.cs create mode 100644 framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs diff --git a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/SwaggerGenOptionsExtensions.cs b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/SwaggerGenOptionsExtensions.cs new file mode 100644 index 00000000000..89d75f3e644 --- /dev/null +++ b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/SwaggerGenOptionsExtensions.cs @@ -0,0 +1,12 @@ +using Swashbuckle.AspNetCore.SwaggerGen; +using Volo.Abp.Swashbuckle; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class SwaggerGenOptionsExtensions +{ + public static void HideAbpEndpoints(this SwaggerGenOptions swaggerGenOptions) + { + swaggerGenOptions.DocumentFilter(); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs new file mode 100644 index 00000000000..5fd05a1460a --- /dev/null +++ b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs @@ -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)); + } +} \ No newline at end of file From 84111165b89d1dc333e4f57100295aad43dc760f Mon Sep 17 00:00:00 2001 From: Engincan VESKE Date: Wed, 23 Feb 2022 09:07:44 +0300 Subject: [PATCH 2/2] Update AbpSwaggerGenOptionsExtensions.cs --- ...enOptionsExtensions.cs => AbpSwaggerGenOptionsExtensions.cs} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/{SwaggerGenOptionsExtensions.cs => AbpSwaggerGenOptionsExtensions.cs} (85%) diff --git a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/SwaggerGenOptionsExtensions.cs b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs similarity index 85% rename from framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/SwaggerGenOptionsExtensions.cs rename to framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs index 89d75f3e644..43130410647 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/SwaggerGenOptionsExtensions.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs @@ -3,7 +3,7 @@ namespace Microsoft.Extensions.DependencyInjection; -public static class SwaggerGenOptionsExtensions +public static class AbpSwaggerGenOptionsExtensions { public static void HideAbpEndpoints(this SwaggerGenOptions swaggerGenOptions) {