记一次swagger3(openApi)源码错误
最近事情太多了,有点忙,博主在经历了5面之后终于拿到了阿里-蚂蚁金服的offer。
没时间更新太多,就记一个简单的源码错误吧。
这次要喷的是:
//OpenApi For Spring compile "io.swagger.core.v3:swagger-models:2.0.10" compile "io.swagger.core.v3:swagger-annotations:2.0.10" compile "io.swagger.core.v3:swagger-integration:2.0.10"
博主的项目不是spring boot项目所以要单独引入swagger3的包(别问我为什么要用3,哈哈,因为博主的项目非常复杂,在同一个项目中还用了2)
好了话不多说,直接上代码:
@Operation(summary = "Move accounts from source-lists to target-lists" ,description = "Move accounts from source-lists to target-lists", responses = { @ApiResponse(responseCode = "200",description = "Success", content = @Content(mediaType = "application/json", schema = @Schema(implementation = BasicRespDTO.class))), @ApiResponse(responseCode = "400", description = "Bad Reqeust - example, improperly formatted start/end date; ",content = @Content(mediaType = "application/json", schema = @Schema(implementation = AlmApiError.class))), @ApiResponse(responseCode = "401", description = "UnAuthorized - for example, the OktaId passed is not associated with the BPN that is passed.",content = @Content(mediaType = "application/json", schema = @Schema(implementation = AlmApiError.class))), @ApiResponse(responseCode = "500", description = "Internal Server Error - ALM Server error processing request.",content = @Content(mediaType = "application/json", schema = @Schema(implementation = AlmApiError.class)))}, requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody( required = true,content = @Content(mediaType = "application/json", schema = @Schema(implementation = AccountsMoveReqDTOV2.class))) )
可以看到很常规的一个注解
但是在最终的openApi3界面上缺显示了一段奇怪的字符:
“永远在requestBody的Schema上面显示-default response”
这尼玛也太奇怪了吧?这明明是个入参DTO为毛显示default response?
博主试了很多方法,都没解决这个奇怪的问题---一直都显示“default response”
-----
实在受不了了就去查openApi3的源码了:
发现openApi3使用了这个类中的:
org/springdoc/core/Constants.java
中的:
public static final String DEFAULT_DESCRIPTION = "default response";
emmmm
-----------
继续找找
有三处源码中用到了这个定义:
1.这个没毛病,说的是响应体的description(有点眉目了)
if (StringUtils.isBlank(apiResponse.getDescription())) { apiResponse.setDescription(DEFAULT_DESCRIPTION); }
2.
这个也没毛病 和1差不多
private void setDescription(io.swagger.v3.oas.annotations.responses.ApiResponse response, ApiResponse apiResponseObject) { if (StringUtils.isNotBlank(response.description())) { apiResponseObject.setDescription(response.description()); } else { apiResponseObject.setDescription(DEFAULT_DESCRIPTION); } }
3.
就是这里,我靠,你说你一个requestBody的description为null
你为什么源码要去用“default response”?????
这不是显然的偷懒吗?
if (StringUtils.isNotBlank(requestBody.description())) { requestBodyObject.setDescription(requestBody.description()); isEmpty = false; } else { requestBodyObject.setDescription(DEFAULT_DESCRIPTION); }
最终博主在自己的openApi3中的requestBody加入了description这个字段,解决了问题。。。
---------------
后续,楼主去swagger3/openApi 的源码库看了,作者好像也发现了这个问题
在后续的版本修复了这个bug
我看了具体的代码,用的是局部变量名在作为requestBody的default description
(源码不在展示)
---------------
所以说使用造好的*其实也会有问题,虽然本次问题很简单,但是一定要有查看源码的能力!!
加油!!