FastEndpoints - cannot generate Swagger documentation query parameters for GET requests

1.5k Views Asked by At

I am trying to make query parameters displayed in the Swagger Doc generated for an ASP.NET Core 7 application relying on FastEndpoints.

The issue is that I get an empty parameter list. My code is as follows:

The endpoint class


public class EchoEndpoint : EndpointBase<EchoPayload, EchoPayload>
{
    private readonly ILogger<EchoEndpoint> _logger;

    public EchoEndpoint(ILogger<EchoEndpoint> logger)
    {
        _logger = logger;
    }

    public override void Configure()
    {
        Get("test/echo");

        Description(b => b
                .Produces<EchoPayload>(StatusCodes.Status200OK, "application/json")
                .Produces(StatusCodes.Status401Unauthorized), 
            clearDefaults:true
        );

        Summary(s =>
        {
            s.Summary = "quick test to echo the received payload";
            s.Params["message"] = "message to echo back";
        });

        base.Configure();
    }

    public override async Task HandleAsync(EchoPayload req, CancellationToken token)
    {
        _logger.LogInformation("Echo called");

        await SendOkAsync(req, token);
    }
}

public class EchoPayload
{
    [QueryParam] 
    public string Message { get; set; } = "";
}

Wiring in Program.cs


    public static IServiceCollection ConfigureSwagger(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddSwaggerDoc(AddSwaggerDocs_ConfigureAuth, 
            addJWTBearerAuth: false, serializerSettings: c =>
        {
            c.PropertyNamingPolicy = null;
            c.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
        }, removeEmptySchemas: false);

        return services;
    }

    private static void ConfigureSwagger(this IApplicationBuilder app)
    {
        app.UseOpenApi(c =>
        {
            // no options yet
        });

        app.UseSwaggerUi3(c =>
        {
            c.ConfigureDefaults();

            c.OAuth2Client = new OAuth2ClientSettings
            {
                //TODO: read from config
                ClientId = "",
                AppName = "WebApi",
                UsePkceWithAuthorizationCodeGrant = true
            };
        });
    }

I have dived a bit in the FastEndpoint's sources and the issue seems to be related to having any parameters in OperationProcessor class -> Process method -> var apiDescription = ((AspNetCoreOperationProcessorContext)ctx).ApiDescription; variable -> ParameterDescriptions property.

This leads to the DTO's properties not being checked for the [QueryParam] attribute.

Any idea what I am missing to have the query parameters showing in the generated NSwag swagger doc?

1

There are 1 best solutions below

1
On BEST ANSWER

just tried the following:

var builder = WebApplication.CreateBuilder();
builder.Services.AddFastEndpoints();
builder.Services.AddSwaggerDoc(addJWTBearerAuth: false);

var app = builder.Build();
app.UseAuthorization();
app.UseFastEndpoints();
app.UseSwaggerGen(
    config: c => { },
    uiConfig: ui =>
    {
        ui.OAuth2Client = new()
        {
            ClientId = "",
            AppName = "WebApi",
            UsePkceWithAuthorizationCodeGrant = true
        };
    });
app.Run();

public class EchoPayload
{
    [QueryParam]
    public string Message { get; set; } = "";
}

public class Endpoint : Endpoint<EchoPayload, EchoPayload>
{
    public override void Configure()
    {
        AllowAnonymous(); //just for testing
        Get("test/echo");
        Description(x => x.Produces(StatusCodes.Status401Unauthorized));
        Summary(x =>
        {
            x.Summary = "this is the summary text";
            x.RequestParam(r => r.Message, "this the message description");
        });
    }

    public override async Task HandleAsync(EchoPayload r, CancellationToken c)
    {
        await SendAsync(r);
    }
}

and it successfully creates the following swagger operation, including the query parameter for the GET request: enter image description here

if you need further help, pls update my code to replicate the issue you're seeing and create a gist or update your original post above.