Search code examples
asp.net-mvc.net-coreswaggerswagger-ui

Swagger not generating model for object wrapped by IActionResult


With the following code, Swaggger UI shows the RegistrationInfo model but not the UserInfo model.

How do I get that to generate?

[Produces("application/json")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api")]

public class UserController : Controller
{

    [HttpPost("RegisterUser")]
    public  IActionResult RegisterUser([FromBody] RegistrationInfo info)
    {
        UserInfo data =    UserData.RegisterUser(info);
        if (data != null)
        {
            return Ok(data);
        }
        return NoContent();
    }
}

Solution

  • You need to use the ProducesResponseType attribute. Change your controller to this:

    [Produces("application/json")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [Route("api")]
    public class UserController : Controller
    {
        [ProducesResponseType(typeof(UserInfo), StatusCodes.Status200OK)]
        [HttpPost("RegisterUser")]
        public IActionResult RegisterUser([FromBody] RegistrationInfo info)
        {
            UserInfo data = UserData.RegisterUser(info);
            if (data != null)
            {
                return Ok(data);
            }
    
            return NoContent();
        }
    }
    

    See more here.