diff --git a/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs new file mode 100644 index 00000000000..43130410647 --- /dev/null +++ b/framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs @@ -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(); + } +} \ 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