I use openapi generator maven plugin to generate source code for a spring rest api. Here is the pom:
`<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.4</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/openapi/openapi.yml</inputSpec>
<output>${project.basedir}/target/generated-sources/swagger</output>
<generatorName>spring</generatorName>
<library>spring-boot</library>
<generateApis>true</generateApis>
<generateModels>true</generateModels>
<modelPackage>...openapi.model</modelPackage>
<apiPackage>...openapi.api</apiPackage>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<configOptions>
<delegatePattern>true</delegatePattern>
<useBeanValidation>true</useBeanValidation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>`
The code is generated fine, but despite i define a field required in the yml file, it doesnt have any effect - i can send a request with the required field being null and no error thrown. What do i have to do to make the constraints work (others like size limitation arent working either)? I can provide more code, but its simply the generated code with a very simple Delegate implementation.
I also had this issue using the generator in a Spring Boot 3.2.5 project. After trying various solutions online with no resolution, I eventually discovered the solution. You need to set useJakartaEe to true in your configOptions like below:
<configuration>
<configOptions>
<useJakartaEe>true</useJakartaEe>
</configOptions>
</configuration>
After that, add jakarta.validation-api as a dependency and remove the javax version if you have it.
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
That's it. Doing this resolved the issue and validations were no longer ignored. I believe the cause of this is the migration of packages from javax to jakarta. By default, the generator uses javax and recent Spring Boot versions support jakarta. That might have led to some incompatibility issues somewhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With