Skip to content

Commit

Permalink
Merge pull request abpframework#11675 from abpframework/EngincanV/swa…
Browse files Browse the repository at this point in the history
…gger

Add support to hide ABP endpoints on Swagger UI
  • Loading branch information
ebicoglu authored Feb 23, 2022
2 parents 602ee6c + 8411116 commit 981c776
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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>();
}
}
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));
}
}

0 comments on commit 981c776

Please sign in to comment.