[BUG][Java][Spring] useBeanValidation=false still generates jakarta.validation.constraints.NotNull
Description
Bug Report Checklist
Description
When useBeanValidation is set to false, the generator continues to produce jakarta.validation.constraints.NotNull annotations and imports for required properties. This creates a technical contradiction: the generator assumes the existence of a validation library that the user has explicitly opted out of.
The "Masking" Problem:
This compilation error is frequently masked because the default documentationProvider=spring pulls in the Jakarta Validation API transitively. However, when a user sets documentationProvider=none and useBeanValidation=false, the project lacks the necessary classpath dependency, leading to a build failure.
Better Alternatives:
Since the original implementation of this feautre, we have better ways to handle nullability hints without forcing a runtime validation dependency:
useJSpecify=true: Provides annotations for IDE-level and compile-time linting.
org.springframework.lang.Nullable: same as JSpecify, and generated by default .
openapi-generator version
Detected in 7.x. Based on git history, this appears to be a regression/side-effect of changes made around version 6.2.1 and 6.8.
OpenAPI declaration file content or url
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_1/petstore.yaml
Generation Details
{
"generatorName": "spring",
"inputSpec": "petstore.yaml",
"outputDir": "out",
"additionalProperties": {
"library": "spring-cloud",
"useBeanValidation": false,
"documentationProvider": "none"
}
}
Steps to reproduce
# clone the openapi-generator repo
git clone https://github.com/OpenAPITools/openapi-generator.git
# into the dir of the petstore yaml
cd openapi-generator/modules/openapi-generator/src/test/resources/3_1
# generate code with validation disabled and no documentation provider
docker run --rm -v ${PWD}:/ws -w /ws openapitools/openapi-generator-cli generate -g spring -i petstore.yaml -o out -p 'library=spring-cloud,useBeanValidation=false,documentationProvider=none'
# attempt to compile
docker run --rm -v ${PWD}/out:/ws -w /ws maven mvn package
Maven package output (Click to expand)
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project openapi-spring: Compilation failure:
[ERROR] /ws/src/main/java/org/openapitools/model/Pet.java:[16,38] package jakarta.validation.constraints does not exist
[ERROR] /ws/src/main/java/org/openapitools/model/Pet.java:[142,4] cannot find symbol
[ERROR] symbol: class NotNull
[ERROR] location: class org.openapitools.model.Pet
Related issues/PRs
Suggest a fix
- Respect the Flag: If
useBeanValidation=false, the generator should not produce any jakarta.validation imports or annotations. Proposed Fix
- Runtime Safety: If guarding against null is required at the generated code level, consider using
Objects.requireNonNull in the constructor and setter instead of a validation annotation. Proposed Fix
- Linting: Users who want compile-time hints without validation overhead should be encouraged to use the
useJSpecify=true flag.
- Proposed Feature (Breaking Change): To provide a clear path forward and avoid surprises, consider evolving
useBeanValidation into a multi-value option:
NONE: No jakarta.validation annotations (The proposed fix for the current false setting).
REQUIRED_ONLY: Generates @NotNull for required fields only (The current behavior for false).
ALL: Full bean validation (The current behavior for true).
- implementation spec/plan
[BUG][Java][Spring] useBeanValidation=false still generates jakarta.validation.constraints.NotNull
Description
Bug Report Checklist
Description
When
useBeanValidationis set tofalse, the generator continues to producejakarta.validation.constraints.NotNullannotations and imports for required properties. This creates a technical contradiction: the generator assumes the existence of a validation library that the user has explicitly opted out of.The "Masking" Problem:
This compilation error is frequently masked because the default
documentationProvider=springpulls in the Jakarta Validation API transitively. However, when a user setsdocumentationProvider=noneanduseBeanValidation=false, the project lacks the necessary classpath dependency, leading to a build failure.Better Alternatives:
Since the original implementation of this feautre, we have better ways to handle nullability hints without forcing a runtime validation dependency:
useJSpecify=true: Provides annotations for IDE-level and compile-time linting.org.springframework.lang.Nullable: same as JSpecify, and generated by default .openapi-generator version
Detected in 7.x. Based on git history, this appears to be a regression/side-effect of changes made around version 6.2.1 and 6.8.
OpenAPI declaration file content or url
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_1/petstore.yaml
Generation Details
{ "generatorName": "spring", "inputSpec": "petstore.yaml", "outputDir": "out", "additionalProperties": { "library": "spring-cloud", "useBeanValidation": false, "documentationProvider": "none" } }Steps to reproduce
Maven package output (Click to expand)
Related issues/PRs
PR #13580: Introduced
@NotNullfor required fields even whenuseBeanValidation=falseto reflect schema requirements.PR #13941: Added the missing
jakarta.validationimport to fix compilation errors reported in [BUG][JAVA] Bug generating @NotNull when useBeanValidation is set to false #13885.The Conflict: While put back missing import for NotNull annotation in #13365 fix #13885 #13941 fixed the immediate missing import error, it solidified a mandatory dependency on Jakarta Validation that ignores the
useBeanValidation=falseflag.issue [BUG] javax.annotation.NonNull added in fields even without bean validation selected #16441 similar observation
Suggest a fix
useBeanValidation=false, the generator should not produce anyjakarta.validationimports or annotations. Proposed FixObjects.requireNonNullin the constructor and setter instead of a validation annotation. Proposed FixuseJSpecify=trueflag.useBeanValidationinto a multi-value option:NONE: Nojakarta.validationannotations (The proposed fix for the currentfalsesetting).REQUIRED_ONLY: Generates@NotNullfor required fields only (The current behavior forfalse).ALL: Full bean validation (The current behavior fortrue).