Ends in:
1 DAY
7 HRS
28 MIN
57 SEC
Ends in:
1 D
7 H
28 M
57 S
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 v9.1.0-preview. Updated on December 05, 2024, 12:19