Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle - swagger documentation of returned response?

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;
    }
like image 677
001 Avatar asked Jul 19 '17 06:07

001


1 Answers

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:

enter image description here enter image description 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")]

enter image description here

like image 167
jps Avatar answered Sep 28 '22 19:09

jps