Swashbuckle set manualy operationId, mutiple opertaions with same verb

早过忘川 提交于 2019-11-27 03:20:14

问题


I need to know if it's possible to set up custom operationid, or a naming convention, I mean I know that operation filter can be overwritten the way how operationId is generated

https://azure.microsoft.com/en-us/documentation/articles/app-service-api-dotnet-swashbuckle-customize/

using Swashbuckle.Swagger;
using System.Web.Http.Description;

namespace Something
{
    public class MultipleOperationsWithSameVerbFilter : IOperationFilter
    {
        public void Apply(
            Operation operation,
            SchemaRegistry schemaRegistry,
            ApiDescription apiDescription)
        {
            if (operation.parameters != null)
            {
                operation.operationId += "By";
                foreach (var parm in operation.parameters)
                {
                    operation.operationId += string.Format("{0}",parm.name);
                }
            }
        }
    }
}

in SwaggerConfig.cs

 c.OperationFilter<MultipleOperationsWithSameVerbFilter>();

Now this is helpful transforming swagger description, check bellow:

All good, now I endup in a darker place, example similar with may cases: on same controller I have two endpoints

  • Post: /customer boddy: {email, location....}
  • Post: /customer/search boddy: {a filter, whatever}

The example is not quite correct (last post should be a get) but still le assume that webapi cannot be changed (new controller for separation) for this particular case I will try to figure out a way to generate operationId diffrent for each action somehow, but my question is this:

Is it possible to decorate somehow the controller actions similar with [JsonIgnore] or with [Route("customer/delete")], to be explicit about the operationId.


回答1:


You can use the SwaggerOperationAttribute provided by Swashbuckle for that.

[SwaggerOperation("get")]
public IEnumerable<Contact> Get()
{
    ....
}

[SwaggerOperation("getById")]
public IEnumerable<Contact> Get(string id)
{
    ...
}

You can use that attribute to add tags and schemes to your operation as well by the way. Have a look at the source code




回答2:


For swashbuckle 5.0, you can use the Name attribute

[HttpGet("{id}", Name = "GetProductById")]
public IActionResult Get(int id) // operationId = "GetProductById"'

There are a few other options listed here



来源:https://stackoverflow.com/questions/39412998/swashbuckle-set-manualy-operationid-mutiple-opertaions-with-same-verb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!