Browse Source

Merge pull request #11675 from abpframework/EngincanV/swagger

Add support to hide ABP endpoints on Swagger UI
pull/11681/head
albert 5 years ago
committed by GitHub
parent
commit
981c776105
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs
  2. 28
      framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs

12
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<AbpSwashbuckleDocumentFilter>();
}
}

28
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));
}
}
Loading…
Cancel
Save