Swashbuckle Swagger - How to annotate content types?

前端 未结 4 838
南方客
南方客 2020-12-16 12:02

How do I annotate my ASP.NET WebAPI actions so that the swagger metadata includes the content-types that my resources support?

Specifically, I want the documentation

4条回答
  •  甜味超标
    2020-12-16 12:55

    What you need to do is this; Swagger spec: you need to add your response-type to the list of response-types for that operation:

    "produces": [
                "application/json",
                "text/json"
                ],
    

    This can be done with an OperationFilter.

    Pseudo Code incoming!!!

    public class CustomResponseType : IOperationFilter
    {        
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {            
                if (operation.operationId == "myCustomName")
                {
                    operation.produces.Add("application/vnd.blah+json");
                }            
        }      
    }
    

    the OperationId can be set through the [SwaggerOperation("myCustomName")] annotation.

    Then apply the operationsFilter in the swaggerConfig.cs:

    c.OperationFilter();
    

    Note: instead of operation.operationId == "myCustomName" you could do it for a particular route or anything else basically. ApiDescription gives a LOT of info about context.

提交回复
热议问题