Description
Validations are not working at runtime. I am using spring boot - 3.1.4 and swagger-codegen-maven-plugin - 2.4.32. My configuration is as below :
<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>
However at runtime, bean validations are not performed for required fields.
Generated interface method has @valid
annotation:
ResponseEntity\<Void\> createUser(@ApiParam(value = "Created user object" ,required=true )
@Valid
@RequestBody
User body);
Generated POJO has @validated
annotation:
@Validated
public class User {
However, I do not see any generated message for required fields :
@ApiModelProperty(required = true, value = "")
@NotNull
public Long getId() {
return id;
}
Swagger-codegen version
2.4.32
Swagger declaration file content or 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
I tried downgrading spring boot and swagger code gen plugin version. Also tried with jakarta instead of javax but generated code has javax.