OpenAPITools / OpenAPITools/openapi-generator
[BUG][Spring] @Validated and @Valid annotations are in wrong place
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
@Validated annotation is in wrong place while generating endpoint. Annotation should be at method level, not at class level:
https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-validation.html
"If a controller has a class level @Validated, then method validation is applied through an AOP proxy. In order to take advantage of the Spring MVC built-in support for method validation added in Spring Framework 6.1, you need to remove the class level @Validated annotation from the controller."
@Validated on the class level, leads, that validation for the parameters is done twice.
@Valid annotation is at field and getter level, if attribute is type List. This leads, that List wit all it's objects is validated twice.
In worst scenario validation is done 4 times, that may be really expencive
openapi-generator version
7.6.0
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Swagger Petstore - OpenAPI 3.0
description: |-
This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about
Swagger at [https://swagger.io](https://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!
You can now help us improve the API whether it's by making changes to the definition itself or to the code.
That way, with time, we can improve the API in general, and expose some of the new features in OAS3.
_If you're looking for the Swagger 2.0/OAS 2.0 version of Petstore, then click [here](https://editor.swagger.io/?url=https://petstore.swagger.io/v2/swagger.yaml). Alternatively, you can load via the `Edit > Load Petstore OAS 2.0` menu option!_
Some useful links:
- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)
- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)
termsOfService: http://swagger.io/terms/
contact:
email: apiteam@swagger.io
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.11
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
servers:
- url: https://petstore3.swagger.io/api/v3
tags:
- name: pet
description: Everything about your Pets
externalDocs:
description: Find out more
url: http://swagger.io
- name: store
description: Access to Petstore orders
externalDocs:
description: Find out more about our store
url: http://swagger.io
- name: user
description: Operations about user
paths:
/pet:
post:
tags:
- pet
summary: Add a new pet to the store
description: Add a new pet to the store
operationId: addPet
requestBody:
description: Create a new pet in the store
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
required: true
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
'400':
description: Invalid input
'422':
description: Validation exception
components:
schemas:
Tag:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: tag
Pet:
required:
- name
- photoUrls
type: object
properties:
id:
type: integer
format: int64
example: 10
name:
type: string
example: doggie
tags:
type: array
xml:
wrapped: true
items:
$ref: '#/components/schemas/Tag'
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
xml:
name: pet
ApiResponse:
type: object
properties:
code:
type: integer
format: int32
type:
type: string
message:
type: string
requestBodies:
Pet:
description: Pet object that needs to be added to the store
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
Generation Details
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>openapi-validated-bug</artifactId>
<version>1.0-SNAPSHOT</version>
<name>openapi-validated-bug</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openapi.generator.maven.plugin.version>7.6.0</openapi.generator.maven.plugin.version>
<build-helper-maven-plugin.version>3.6.0</build-helper-maven-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<jackson.version>2.17.1</jackson.version>
<jakarta-annotation.version>3.0.0</jakarta-annotation.version>
<jakarta-validation.version>3.1.0</jakarta-validation.version>
<jackson-databind-nullable.version>0.2.6</jackson-databind-nullable.version>
<spring-test.version>6.1.8</spring-test.version>
<spring-cloud-contract-wiremock.version>4.1.2</spring-cloud-contract-wiremock.version>
<springdoc-openapi-starter-webmvc-ui.version>2.5.0</springdoc-openapi-starter-webmvc-ui.version>
<org.springframework.boot.version>3.2.5</org.springframework.boot.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi.generator.maven.plugin.version}</version>
<executions>
<execution>
<id>openapi-validated-api</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<inputSpec>
${basedir}/src/main/resources/openapi.yaml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>
org.openapi.validated.bug.api
</apiPackage>
<modelPackage>
org.openapi.validated.bug.model
</modelPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<skipDefaultInterface>true</skipDefaultInterface>
<generateSpringApplication>false</generateSpringApplication>
<generateSpringBootApplication>false</generateSpringBootApplication>
<dateLibrary>java8</dateLibrary>
<interfaceOnly>true</interfaceOnly>
<implicitHeaders>true</implicitHeaders>
<useSpringBoot3>true</useSpringBoot3>
<reactive>false</reactive>
<useTags>true</useTags>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/openapi/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Jopo dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>${jackson-databind-nullable.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi-starter-webmvc-ui.version}</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>${jakarta-validation.version}</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta-annotation.version}</version>
</dependency>
</dependencies>
</project>
Steps to reproduce
Create maven project and run
mvn install
See generated class PetApi that has @Validated on class level, not method level:
@Validated
@Tag(name = "pet", description = "Everything about your Pets")
public interface PetApi {
See as well generated class Pet. field Tag and getter getTags(), both has @Valid annotation. Field should not have @Valid annotation:
...
@Valid
private List<@Valid Tag> tags = new ArrayList<>();.
....
@Valid
@Schema(name = "tags", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@JsonProperty("tags")
public List<@Valid Tag> getTags() {
return tags;
}
...
Related issues/PRs
Suggest a fix
Remove @Validated annotation from class level and add it to the method level.
Remove @Valid annotation from field level in case List
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the supplied Maven project by running mvn install, then inspect the generated PetApi and Pet classes. Confirm that @Validated is generated at method level and that collection validation does not duplicate @Valid; done means the generated annotations no longer cause repeated validation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100