[英]Swashbuckle - swagger documentation of returned response?
Swashbuckle would not generate swagger.json with an output of "UserCreateResponse", how do you fix this? 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);
}
You can't do this, because its not going to return the http status code, 200,400,401 etc你不能这样做,因为它不会返回 http 状态代码,200,400,401 等
[HttpPost]
public async Task<UserCreateResponse> Update([FromBody]UserCreate Request)
{
UserCreateResponse response = new UserCreateResponse();
//do something here
// returns UserCreateResponse
return response;
}
From V6.0 onwards SwaggerResponse
isn't supported anymore, see here .从 V6.0 开始,不再支持SwaggerResponse
,请参见此处。
Another variant is the use of the SwaggerResponse
attribute, which also allows to provide an additional description:另一个变体是使用SwaggerResponse
属性,它也允许提供额外的描述:
[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:它产生如下所示的输出:
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")]
您可以使用以下属性指定响应类型:
[ProducesResponseType(typeof(UserCreateResponse), 200)]
Starting from .NET Core 2.1, using ActionResult<T> would be the recommended approach to specify the returned type.从 .NET Core 2.1 开始,使用ActionResult<T>将是指定返回类型的推荐方法。 It gets picked by Swashbuckle and also adds type checks at the compilation.它由 Swashbuckle 选择,并在编译时添加类型检查。
You also can add description on the response via XML comment ( docs ).您还可以通过 XML 注释 ( docs ) 在响应中添加描述。
So for the OP's example it would be因此,对于 OP 的示例,它将是
/// <summary>
/// Update the user
/// </summary>
/// <response code="200"> User's data </response>
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<UserCreateResponse>> Update(...)
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.