Kirsten
Kirsten

Reputation: 18140

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();
    }
}

Upvotes: 15

Views: 5403

Answers (1)

Chris.ZA
Chris.ZA

Reputation: 1286

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.

Upvotes: 27

Related Questions