将WebApi方法的参数标记为Swashbuckle / Swagger已过时/已弃用

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

根据我对Swagger Specification的理解,可以将参数标记为过时:

不推荐使用的参数

使用deprecated: true将参数标记为已弃用。

        - in: query
          name: format
          required: true
          schema:
            type: string
            enum: [json, xml, yaml]
          deprecated: true
          description: Deprecated, use the appropriate `Accept` header instead.```

我如何让Swashbuckle为参数生成它?

为什么?

我有一个类似下面的控制器方法:

[HttpGet]
public async Task<IActionResult> Execute(bool? someName)
{
}

而且我想更改querystring参数名称,同时暂时保持向后兼容,所以我想做类似的事情:

[HttpGet]
public async Task<IActionResult> Execute([Obsolete("Use someNewName instead.")] bool? someName, bool? someNewName)
{
    someNewName = someNewName ?? someName;
}

但是,Obsolete无法应用于参数。我期望Swashbuckle.AspNetCore.Annotations可能是找到这种功能的地方,但似乎没有它。

c# swashbuckle webapi
1个回答
0
投票

您不会将参数标记为过时,如果参数过时,则整个方法都将过时。您需要声明一个具有新方法签名的新方法,并将旧方法标记为过时。像这样

[HttpGet]
[Obsolete("Use Execute with bool? someNewName instead.")]
public async Task<IActionResult> Execute(bool? someName)
{
}

[HttpGet]
public async Task<IActionResult> Execute(bool? someNewName)
{
}

如果仅更改参数名称,则可以使用Bind属性将URI元素绑定到其他命名的变量,如下所示:

[HttpGet]
public async Task<IActionResult> Execute([Bind(Prefix = "someNewName")] bool? someName)
{
}

这将使您可以继续使用相同的方法,而不必强行更改所有客户端。但是,如果更改的不仅是参数名称(例如类型),您将需要一个新方法

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