Swashbuckle - 返回响应的大肆文档?

2023-12-23

Swashbuckle 不会生成输出为“UserCreateResponse”的 swagger.json,如何解决此问题?

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

你不能这样做,因为它不会返回 http 状态代码,200,400,401 等

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

        //do something here

        // returns UserCreateResponse
        return response;
    }

以下解决方案仅适用于 V6.0 之前的 Swashbuckle 版本!

从V6.0开始SwaggerResponse不再支持,请参阅here https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/159.


另一种变体是使用SwaggerResponse属性,它还允许提供附加描述:

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

其产生的输出如下所示:

也可以省略类型来记录不返回实体的其他状态代码:

[SwaggerResponse(HttpStatusCode.NotFound, "no data found")]
[SwaggerResponse(HttpStatusCode.BadRequest, "requiered request headers not found")]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swashbuckle - 返回响应的大肆文档? 的相关文章

随机推荐