Swashbuckle - swagger documentation of returned response?

空扰寡人 提交于 2020-06-13 20:04:00

问题


Swashbuckle would not generate swagger.json with an output of "UserCreateResponse", how do you fix this?

    [HttpPost]
    public async Task<IActionResult> Update([FromBody]UserCreate Request)
    {
        UserCreateResponse response = new UserCreateResponse();

        //do something here

        // returns UserCreateResponse with http status code 200
        return Ok(response);
    }

You can't do this, because its not going to return the http status code, 200,400,401 etc

    [HttpPost]
    public async Task<UserCreateResponse> Update([FromBody]UserCreate Request)
    {
        UserCreateResponse response = new UserCreateResponse();

        //do something here

        // returns UserCreateResponse
        return response;
    }

回答1:


You can specify the response type with the following attribute:

[ProducesResponseType(typeof(UserCreateResponse), 200)]



回答2:


The below solution works only for Swashbuckle versions prior to V6.0!

From V6.0 onwards SwaggerResponse isn't supported anymore, see here.


Another variant is the use of the SwaggerResponse attribute, which also allows to provide an additional description:

[SwaggerResponse(HttpStatusCode.OK, "UserDTO", typeof(UserDTO))]
public async Task<IHttpActionResult> Get([FromODataUri] int key)
{
    var result = await UserRepo.GetAsync(key);
    ...
    return Ok(result);
}

which produces output as shown here:

It's also possible to omit the type to document other status codes which do not return an entity:

[SwaggerResponse(HttpStatusCode.NotFound, "no data found")]
[SwaggerResponse(HttpStatusCode.BadRequest, "requiered request headers not found")]



来源:https://stackoverflow.com/questions/45182448/swashbuckle-swagger-documentation-of-returned-response

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