如何解决OpenAPI Swagger代码生成Spring Boot:将枚举验证为路径变量
我正在使用swagger 2.0
我的终点
/api/ideas/{idea_id}/{field}:
put:
tags:
- ideas
operationId: UpdateIdeaField
description: Update a field in an idea
parameters:
- name: idea_id
in: path
type: integer
required: true
- name: field
in: path
required: true
type: string
enum: [division,executive_sponsor,platform_contact]
我希望它在使用无效的枚举时会抛出400。但是现在使用无效的枚举,它会接受它。
@ApiOperation(value = "",nickname = "updateIdeaField",notes = "Update a field in an idea",response = IdeaVM.class,authorizations = {
@Authorization(value = "openId",scopes = {
})
},tags={ "ideas",})
@ApiResponses(value = {
@ApiResponse(code = 200,message = "Idea updated",response = IdeaVM.class),@ApiResponse(code = 400,message = "Bad Request",response = ProblemVM.class),@ApiResponse(code = 401,message = "Unauthorized",@ApiResponse(code = 403,message = "Forbidden",@ApiResponse(code = 404,message = "Not Found",@ApiResponse(code = 422,message = "Unprocessable Entity",@ApiResponse(code = 200,message = "Something went wrong",response = ProblemVM.class) })
@RequestMapping(value = "/api/ideas/{idea_id}/{field}",produces = { "application/json","application/problem+json" },method = RequestMethod.PUT)
default ResponseEntity<IdeaVM> _updateIdeaField(@ApiParam(value = "",required=true) @PathVariable("idea_id") Integer ideaId,@ApiParam(value = "",required=true,allowableValues = "\"division\",\"executive_sponsor\",\"platform_contact\"") @PathVariable("field") String field) {
return updateIdeaField(ideaId,field);
}
// Override this method
default ResponseEntity<IdeaVM> updateIdeaField(Integer ideaId,String field) {
if(getobjectMapper().isPresent() && getAcceptHeader().isPresent()) {
if (getAcceptHeader().get().contains("application/json")) {
try {
return new ResponseEntity<>(getobjectMapper().get().readValue("{ \"submitted_date\" : \"2019-10-03T22:45:465Z\",\"gp_status\" : \"IN_REVIEW\",\"challenge_id\" : 897,\"account_executive\" : \"0f4c0f7e-18ce-4b7c-bd48-f4121790dd90\",\"platform_contact\" : \"Walter Waldo\",\"public_id\" : \"201e6466-6924-11ea-bc55-0242ac130003\",\"entered_gate_date\" : \"2019-10-03T22:45:465Z\",\"division\" : \"Asett Managment\",\"idea_type\" : \"201e6466-6924-11ea-bc55-0242ac130003\",\"read_only\" : false,\"collaborators\" : [ \"b4bd2cc9-def8-4b85-86d3-40fb0225542a\",\"400f3605-864a-4cb1-8968-03c831fc49e8\" ],\"publication_date\" : \"2019-10-03T22:45:465Z\",\"rank\" : 1576844,\"id\" : 1,\"views_count\" : 201,\"pending_collaborators\" : [ \"john.smith1@companydomain.com\",\"john.smith2@companydomain.com\" ],\"brief\" : \"We want to explore and ideate solutions which can be utilised to increase last minute bookings\",\"owner\" : \"0f4c0f7e-18ce-4b7c-bd48-f4121790dd90\",\"image_link\" : \"https\",\"idea_description\" : { \"answers\" : [ { \"answer\" : \"The Answer to the Ultimate Question of Life,the Universe,and Everything is... 42\",\"question_id\" : 1 },{ \"answer\" : \"The Answer to the Ultimate Question of Life,\"question_id\" : 1 } ] },\"creation_date\" : \"2019-10-03T22:45:465Z\",\"modified_date\" : \"2019-10-03T22:45:465Z\",\"killed_on_hold_date\" : \"2019-10-03T22:45:465Z\",\"executive_sponsor\" : \"Wally Waldo\",\"tags\" : [ \"Cancellation\",\"Last minute bookins\",\"Delays\",\"Satisfaction\" ],\"total_invested\" : 132000,\"likes_count\" : 126,\"review_date\" : \"2019-10-03T22:45:465Z\",\"mentors\" : [ \"b4bd2cc9-def8-4b85-86d3-40fb0225542a\",\"followers_count\" : 7,\"name\" : \"Last minute booking promotions\",\"is_public\" : false,\"gate\" : { \"active\" : true,\"display_name\" : \"discovery\",\"key\" : \"gate_discovery\",\"order\" : 1 },\"status\" : \"DRAFT\"}",IdeaVM.class),HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type application/json",e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
} else {
log.warn("ObjectMapper or HttpServletRequest not configured in default IdeasApi interface so no example is generated");
}
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
感谢您的帮助
下面的建议是,生成的输出仍然相同。该字段是生成的接口中的字符串,我可以发送无效的枚举
/api/ideas/{idea_id}/{field}:
put:
tags:
- ideas
operationId: UpdateIdeaField
description: Update a field in an idea
parameters:
- name: idea_id
in: path
type: integer
required: true
- name: field
in: path
required: true
type: string
schema:
$ref: '#/deFinitions/MyEnum'
- in: body
name: value
required: true
schema:
$ref: '#/deFinitions/UpdateIdeaFieldRequest'
解决方法
您尝试创建一个单独的枚举对象吗?
MyEnum:
type: string
enum:
- value1
- value2
/api/ideas/{idea_id}/{field}:
put:
tags:
- ideas
operationId: UpdateIdeaField
description: Update a field in an idea
parameters:
- name: idea_id
in: path
type: integer
required: true
- name: field
in: path
required: true
schema:
$ref: '#/definitions/MyEnum'
- in: body
name: value
required: true
schema:
$ref: '#/definitions/UpdateIdeaFieldRequest'
然后使用$ref
来枚举
您可以尝试创建一个自定义转换器来将字符串转换为枚举,如下所示:https://www.baeldung.com/spring-enum-request-param#clone
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。