提问人:K 2 提问时间:9/29/2023 最后编辑:AdaK 2 更新时间:9/30/2023 访问量:88
验证不适用于使用 swagger codegen for spring boot 应用程序生成的代码
Validation not working for generated code using swagger codegen for spring boot app
问:
描述
验证在运行时不起作用。 我正在使用 spring boot - 3.1.4 和 swagger-codegen-maven-plugin - 2.4.32。 我的配置如下:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.32</version>
<executions>
<execution>
<id>swagger-codegen</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.build.directory}/BOOT-INF/classes/${serviceYamlName}.yaml</inputSpec>
<language>spring</language>
<apiPackage>com.k2.openapi.${serviceName}.api</apiPackage>
<modelPackage>com.k2.openapi.${serviceName}.model</modelPackage>
<configOptions>
<delegatePattern>true</delegatePattern>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
<useBeanValidation>true</useBeanValidation>
<performBeanValidation>true</performBeanValidation>
<library>spring-mvc</library>
</configOptions>
<addCompileSourceRoot>true</addCompileSourceRoot>
<generateSupportingFiles>false</generateSupportingFiles>
</configuration>
</execution>
</executions>
</plugin>
但是,在运行时,不会对必填字段执行 Bean 验证。
生成的接口方法有注解:@valid
ResponseEntity\<Void\> createUser(@ApiParam(value = "Created user object" ,required=true )
@Valid
@RequestBody
User body);
生成的 POJO 有注解:@validated
@Validated
public class User {
但是,我没有看到任何为必填字段生成的消息:
@ApiModelProperty(required = true, value = "")
@NotNull
public Long getId() {
return id;
}
Swagger-codegen 版本
2.4.32
Swagger 声明文件内容或 URL
swagger: "2.0"
info:
title: Reflectoring
description: "Tutorials on Spring Boot and Java, thoughts about the Software Craft, and relevant book reviews. Because it's just as important to understand the Why as it is to understand the How. Have fun!"
version: 0.0.1-SNAPSHOT
termsOfService: http://swagger.io/terms/
contact:
email: [email protected]
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
externalDocs:
description: Find out more about Reflectoring
url: https://reflectoring.io/about/
host: reflectoring.swagger.io
basePath: /v2
tags:
- name: user
description: Operations about user
paths:
/user:
post:
tags:
- user
summary: Create user
description: Create user functionality
operationId: createUser
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: body
description: Created user object
required: true
schema:
$ref: '#/definitions/User'
responses:
"default":
description: successful operation
/user/{username}:
get:
tags:
- user
summary: Get user by user name
description: Get user functionality
operationId: getUserByName
produces:
- application/json
parameters:
- name: username
in: path
description: "The name that needs to be fetched. Use user1 for testing."
required: true
type: string
responses:
"200":
description: successful operation
schema:
$ref: '#/definitions/User'
"404":
description: User not found
put:
tags:
- user
summary: Updated user
description: This can only be done by the logged in user.
operationId: updateUser
consumes:
- application/json
produces:
- application/json
parameters:
- name: username
in: path
description: name that need to be updated
required: true
type: string
- in: body
name: body
description: Updated user object
required: true
schema:
$ref: '#/definitions/User'
responses:
"200":
description: successful operation
schema:
$ref: '#/definitions/User'
"400":
description: Invalid user supplied
"404":
description: User not found
delete:
tags:
- user
summary: Delete user
description: This can only be done by the logged in user.
operationId: deleteUser
produces:
- application/json
parameters:
- name: username
in: path
description: The name that needs to be deleted
required: true
type: string
responses:
"201":
description: operation successful
"400":
description: Invalid username supplied
"404":
description: User not found
definitions:
User:
type: object
required:
- id
- username
properties:
id:
type: integer
format: int64
username:
type: string
firstName:
type: string
lastName:
type: string
email:
type: string
password:
type: string
phone:
type: string
userStatus:
type: integer
description: User Status
format: int32
我尝试降级 spring boot 和 swagger 代码生成插件版本。也尝试了 jakarta 而不是 javax,但生成的代码有 javax。
答: 暂无答案
评论