Skip to content

[BUG][Java][Spring] useBeanValidation=false still generates jakarta.validation.constraints.NotNull #23751

@zaenk

Description

@zaenk

[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.validation packages.
    • 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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions