具有最少 API 的文档查询参数

问题描述 投票:0回答:1

我尝试使用

RouteHandlerBuilder
扩展方法将一些 OpenAPI 文档放在最小 API 中的查询参数上。我还没有找到任何关于如何实现这一目标的示例或文档。

        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

如果我添加

In = ParameterLocation.Query
,我会得到 2 个相同的,但没有描述。

Second try

感谢您的帮助。

.net openapi minimal-apis
1个回答
0
投票

假设您正在使用 .NET 8 和

Swashbuckle.AspNetCore
。您可以使用
Swashbuckle.AspNetCore.Annotations
包:

<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>

启用注释处理:

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

并添加描述:

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

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.