Open Closed

How to hide an endpoint from Swagger? #264


User avatar
2
alper created
Support Team Director

I want to hide the organization unit endpoints in Swagger

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

1 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    To hide endpoints in Swagger, you can use IDocumentFilter of the Swashbuckle.

    class HideOrganizationUnitsFilter : IDocumentFilter
            {
                private const string pathToHide = "/identity/organization-units";
    
                public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
                {
                    var organizationUnitPaths = swaggerDoc
                        .Paths
                        .Where(pathItem => pathItem.Key.Contains(pathToHide, StringComparison.OrdinalIgnoreCase))
                        .ToList();
    
                    foreach (var item in organizationUnitPaths)
                    {
                        swaggerDoc.Paths.Remove(item.Key);
                    }
                }
            }
    

    in ConfigureSwaggerServices, add this document filter

            private void ConfigureSwaggerServices(IServiceCollection services)
            {
                services.AddSwaggerGen(
                    options =>
                    {
                        options.SwaggerDoc("v1", new OpenApiInfo {Title = "MyProjectName API", Version = "v1"});
                        options.DocInclusionPredicate((docName, description) => true);
                        options.CustomSchemaIds(type => type.FullName);
                        options.DocumentFilter<HideOrganizationUnitsFilter>(); //<-------- added this -----------
                    }
                );
            }
    

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 16, 2026, 14:50
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.