OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java][Spring] useBeanValidation=false still generates jakarta.validation.constraints.NotNull
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
[BUG][Java][Spring] useBeanValidation=false still generates jakarta.validation.constraints.NotNull
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue? (See repro steps)
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- Actual: Compilation failure due to missing
jakarta.validationpackages.- Expected: Code should compile without requiring Jakarta Validation dependencies when
useBeanValidation=false.
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
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
-
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 #13885. -
The Conflict: While #13941 fixed the immediate missing import error, it solidified a mandatory dependency on Jakarta Validation that ignores the
useBeanValidation=falseflag. -
issue https://github.com/OpenAPITools/openapi-generator/issues/16441 similar observation
Suggest a fix
- Respect the Flag: If
useBeanValidation=false, the generator should not produce anyjakarta.validationimports or annotations. Proposed Fix - Runtime Safety: If guarding against null is required at the generated code level, consider using
Objects.requireNonNullin 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=trueflag. - Proposed Feature (Breaking Change): To provide a clear path forward and avoid surprises, consider evolving
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).- implementation spec/plan
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 from modules/openapi-generator/src/test/resources/3_1/petstore.yaml using the shown Spring configuration, then run Maven package on the generated output. Review PRs #13580 and #13941 alongside the proposed fixes to determine the intended behavior; done means validation is not required when useBeanValidation=false and the generated project compiles without jakarta.validation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100