Swashbuckle - swagger documentation of returned response?

Viewed 8624

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;
    }
3 Answers

Starting from .NET Core 2.1, using ActionResult<T> would be the recommended approach to specify the returned type. It gets picked by Swashbuckle and also adds type checks at the compilation.

You also can add description on the response via XML comment (docs).

So for the OP's example it would be

/// <summary> 
///     Update the user 
/// </summary>
/// <response code="200"> User's data </response>
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<UserCreateResponse>> Update(...) 
{
   ...
}
Related