如何使用Springdoc在OpenAPI 3.0中创建链接?

问题描述 投票:3回答:1

我正在尝试为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中看不到任何结果,但描述为“ 无链接”。

No links

检查生成的JSON规范后,我也没有找到links API的任何requestAvatar密钥。

我在创建@Link的过程中错过了什么吗,还是Springdoc不支持链接?

java spring-boot swagger-ui openapi springdoc
1个回答
0
投票

使用@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"
                    }
                }
            }
        }
    }
}
...
© www.soinside.com 2019 - 2024. All rights reserved.