Max
Max

Reputation: 679

Document query parameter with minimal API

I try to put some OpenAPI documentation on a query parameter in a minimal API, using RouteHandlerBuilder extension methods. I haven't manage to find any examples or documentation on how this should be achieved.

        group
            .MapGet(
                "/paymentCard",
                async (
                    string osbToken,
                    [FromServices] IDataversePaymentCardService paymentCardService
                ) =>
                {
                    // Removed for brevity
                    return Results.Ok();
                }
            )
            .Produces<PaymentCard?>(StatusCodes.Status200OK)
            .WithCommonErrorResponses()
            .WithSummary("Find a payment card based on osbToken (XYZ handle)")
            .WithOpenApi(static operation =>
            {
                operation.Parameters.Add(new OpenApiParameter
                {
                    Name = "osbToken",
                    Description = "The handle from XYZ, which must start with 'ca_'",
                    Required = true,
                    Schema = new OpenApiSchema { Type = "string" }
                });

                return operation;
            });

First try

If I add In = ParameterLocation.Query I get 2 identical, but with no description.

Second try

Thanks for your help.

Upvotes: 0

Views: 32

Answers (1)

Guru Stron
Guru Stron

Reputation: 141565

Assuming you are using .NET 8 with Swashbuckle.AspNetCore. You can use the Swashbuckle.AspNetCore.Annotations package:

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.11"/>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.1.0" />
</ItemGroup>

enable annotations processing:

builder.Services.AddSwaggerGen(
    options => options.EnableAnnotations()
    );

And add the description:

app
    .MapGet(
        "/paymentCard",
        (
            [SwaggerParameter("The handle from XYZ, which must start with 'ca_'")] string osbToken
        ) => ...
    )

Result:

enter image description here

Upvotes: 0

Related Questions