Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom DocumentFilter not being applied #1426

Open
TaylorCaldwell-FiveTalent opened this issue Jan 26, 2022 · 1 comment
Open

Custom DocumentFilter not being applied #1426

TaylorCaldwell-FiveTalent opened this issue Jan 26, 2022 · 1 comment

Comments

@TaylorCaldwell-FiveTalent
Copy link

TaylorCaldwell-FiveTalent commented Jan 26, 2022

VERSION:

6.2.3

STEPS TO REPRODUCE:

  1. Create a custom IDocumentFilter
  2. Specify the new Custom DocumentFilter in the SwaggerConfig (e.g. c.DocumentFilter<CustomSwaggerFilter>();)

EXPECTED RESULT:

Filters to be applied based on the routes specified within IDocumentFilter's Apply method.

ACTUAL RESULT:

Filtering is not being applied. Apply method never appears to be hit when setting a breakpoint and debugging locally.

ADDITIONAL DETAILS

The below code shows the current implementation in our project. Is there any reason the following would not work? When setting a breakpoint in the Apply( ) method, it never gets hit during the startup of the application. Any help would be greatly appreciated, thanks.

Custom filter class:

namespace SwaggerFilter.Filters
{
    public class CustomSwaggerFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var mvcRoutesToRemove = swaggerDoc.Paths 
                .Where(x => x.Key.ToLower().Contains("foo.bar"))
                .ToList();
            mvcRoutesToRemove.ForEach(x => { swaggerDoc.Paths.Remove(x.Key); });
        }
    }
}

Swagger setup:

.AddSwaggerGen(c =>
{
    c.DocumentFilter<CustomSwaggerFilter>();
    c.EnableAnnotations();
    c.CustomOperationIds(e =>
    ...
})
@hlouzek
Copy link

hlouzek commented Jan 23, 2023

add this line
app.UseSwagger(new Swashbuckle.AspNetCore.Swagger.SwaggerOptions());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants