简体   繁体   English

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

[英]Mark a WebApi method's parameter as obsolete / deprecated for Swashbuckle / Swagger

As per my understanding of the Swagger Specification , it's possible to mark a parameter as obsolete:根据我对Swagger Specification的理解,可以将参数标记为过时:

Deprecated Parameters弃用的参数

Use deprecated: true to mark a parameter as deprecated.使用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.```

How can I have Swashbuckle generate this for a parameter?我怎样才能让 Swashbuckle 为参数生成这个?

Why?为什么?

I have a controller method something like the following:我有一个 controller 方法,如下所示:

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

And I want to change the querystring parameter name while temporarily remaining backwards compatible, so I want to do something like:我想更改查询字符串参数名称,同时暂时保持向后兼容,所以我想做类似的事情:

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

However, Obsolete cannot be applied to a parameter.但是, Obsolete不能应用于参数。 I expected that Swashbuckle.AspNetCore.Annotations might be a place to find this kind of functionality, but it doesn't seem to have it.我预计Swashbuckle.AspNetCore.Annotations可能是找到这种功能的地方,但它似乎没有。

You don't mark a parameter as obsolete, if a parameter becomes obsolete, the whole method becomes obsolete.您不会将参数标记为过时,如果参数过时,则整个方法都将过时。 You'll need to declare a new method, with a new method signature, and mark the old method as obsolete.您需要声明一个具有新方法签名的新方法,并将旧方法标记为过时。 Like so像这样

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

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

If you only changed the name of the parameter, you can instead use the Bind attribute to bind a URI element to a differently named variable, like so:如果您只更改了参数的名称,则可以改为使用Bind属性将 URI 元素绑定到不同命名的变量,如下所示:

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

This would allow you to continue to use the same method, without having to forcibly change all your clients.这将允许您继续使用相同的方法,而不必强行更改所有客户端。 But, if more than just the name of the parameter changed, for example the type, you'll need a new method但是,如果改变的不仅仅是参数的名称,例如类型,您将需要一个新方法

it is possible to go through a OperationFilter可以通过 OperationFilter 到 go

in programe.cs set在 programe.cs 集中

options.OperationFilter<OpenApiParameterObsoleteFilter>();

create OpenApiParameterObsoleteFilter.cs file in OpenApiParameterObsoleteFilter set在 OpenApiParameterObsoleteFilter 集中创建 OpenApiParameterObsoleteFilter.cs 文件

 public class OpenApiParameterIgnoreFilter : Swashbuckle.AspNetCore.SwaggerGen.IOperationFilter
{
    public void Apply(Microsoft.OpenApi.Models.OpenApiOperation operation, Swashbuckle.AspNetCore.SwaggerGen.OperationFilterContext context)
    {
        if (operation == null || context == null || context.ApiDescription?.ParameterDescriptions == null)
            return;

        var parametersToHide = context.ApiDescription.ParameterDescriptions
            .Where(parameterDescription => ParameterHasIgnoreAttribute(parameterDescription))
            .ToList();


        var parametersToObsolete = context.ApiDescription.ParameterDescriptions
                .Where(parameterDescription => ParameterHasDepreciate(parameterDescription))
                .ToList();
        foreach (var parameterToObsolete in parametersToObsolete)
        {
            var parameter = operation.Parameters.FirstOrDefault(parameter => string.Equals(parameter.Name, parameterToObsolete.Name, System.StringComparison.Ordinal));
            parameter.Deprecated = true;
        }

    }
    private static bool ParameterHasDepreciate(Microsoft.AspNetCore.Mvc.ApiExplorer.ApiParameterDescription parameterDescription)
    {
        if (parameterDescription.ModelMetadata is Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata metadata)
        {
            return
                (metadata.Attributes.Attributes?.Any(attribute => attribute.GetType() == typeof(ObsoleteAttribute)) ?? false);
        }

        return false;
    }
}

Add Obsolete attribut on your parameter(in an object in my case)在您的参数上添加过时属性(在我的例子中是 object)

    [Obsolete]
    public long? ID_CONDUCT { get => iD_CONDUCT; set => iD_CONDUCT = value; }

In swagger you have this result在 swagger 你有这个结果结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM