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


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 -----------
                    }
                );
            }
    

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11