我正在尝试为OpenAPI 3.0规范中的响应创建链接。更具体地说,我想提供一个响应与其他可用操作之间的已知关系(参见Link Object)。
在我的Spring Boot项目中,我正在使用Springdoc(版本:1.3.9)来生成我的API文档。根据@ApiResponse#links文档,我尝试通过以下端点代码实现我的目标:
@GetMapping(value = "/avatar", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Request avatar info", operationId = "requestAvatar")
@ResponseStatus(HttpStatus.OK)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", links = {
@Link(name = "Download Avatar", operationId = "downloadAvatar",
parameters = {
@LinkParameter(name = "userId"),
@LinkParameter(name = "uuid")
})
}),
...
@ResponseBody
ResponseEntity<Avatar> requestAvatar();
不幸的是,我在Swagger UI中看不到任何结果,但描述为“ 无链接”。
检查生成的JSON规范后,我也没有找到links
API的任何requestAvatar
密钥。
我在创建@Link
的过程中错过了什么吗,还是Springdoc不支持链接?
使用@ApiResponses
时似乎无法进行链接。这可能是一个错误。 🤔
当在@Operation
中定义响应列表时,将正确生成API规范。
示例:
...
@Operation(summary = "Request avatar info", operationId = "requestAvatar", responses = {
@ApiResponse(responseCode = "200", description = "OK", links = {
@Link(name = "Download Avatar", operationId = "downloadAvatar", parameters = {
@LinkParameter(name = "userId", expression = "$request.query.userId"),
@LinkParameter(name = "uuid", expression = "$request.query.uuid")
})
})
})
...
生成的API规范:
...
"get": {
"summary": "Request avatar info",
"operationId": "requestAvatar",
"responses": {
"200": {
"description": "OK",
"content": {
...
},
"links": {
"Download Avatar": {
"operationId": "downloadAvatar",
"parameters": {
"userId": "$request.query.userId",
"uuid": "$request.query.uuid"
}
}
}
}
}
}
...