Use Swashbuckle IOperationFilter with Minimal API

Viewed 60

I have my own OperationFilter used to output few querystring fields from a single concrete type. For some reason it isn't called when using the Minimal API.

This is my endpoint registration.

public static IEndpointRouteBuilder AddSearchEndpoints(this IEndpointRouteBuilder builder)
{
    builder.MapPost("/search", Search)
        .Produces<SearchResponse>();
   
    return builder;
}

private static IResult Search(
    SortingModel sorting,
    [FromBody] SearchRequest body,
    CancellationToken cancellationToken)
{
    return Results.Ok(new { sorting, body });
}

This is how I register the filter in Program.cs

builder.Services.AddSwaggerGen(c =>
{
    // ...

    c.OperationFilter<SortingOperationFilter>();
});

For some reason, the filter is invoked but the context.ApiDescription.ParameterDescriptions collection contains only the SearchRequest parameter.

Note: the same implementation works without issues with old-style controllers.

0 Answers
Related