Skip to content

[Java] Generate nullability annotations on all-args constructor parameters#24114

Open
seonwooj0810 wants to merge 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-24109-allargs-ctor-nullable
Open

[Java] Generate nullability annotations on all-args constructor parameters#24114
seonwooj0810 wants to merge 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-24109-allargs-ctor-nullable

Conversation

@seonwooj0810

@seonwooj0810 seonwooj0810 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #24109

Root cause

For the Java client generators, model fields and fluent setters are annotated with the configured nullability annotation (@jakarta/javax.annotation.Nullable / @Nonnull, or JSpecify @Nullable), but the all-args constructor generated by generateConstructorWithAllArgs emitted bare parameter types ({{{datatypeWithEnum}}}). So a nullable optional property appeared as private @javax.annotation.Nullable String baz; with a matching @Nullable setter, yet the all-args constructor parameter baz carried no annotation — an inconsistency for null-safety tooling.

Change

The all-args constructor now reuses the same nullability partial each template already applies to its fluent setter, so the constructor parameters stay consistent with the fields/setters:

  • Java/pojo.mustache{{>nullable_var_annotations}}
  • Java/libraries/webclient, restclient, resttemplate pojo.mustache{{>nullableArgumentWithEnum}}

JavaSpring/pojo.mustache already applied {{>nullableAnnotation}} to its all-args constructor and is left unchanged.

Effect (regenerated samples)

e.g. samples/.../resttemplate/.../Pet.java all-args constructor now matches its setters — required props get @Nonnull, optional props get @Nullable:

public Pet(@JsonProperty(JSON_PROPERTY_ID) @javax.annotation.Nullable Long id,
           @JsonProperty(JSON_PROPERTY_NAME) @javax.annotation.Nonnull String name, ...)

Test / verification

  • Extended JavaClientCodegenTest#generateAllArgsConstructor to assert the all-args constructor parameters carry @javax.annotation.Nullable (optional dateOfBirth, name) and @javax.annotation.Nonnull (required type). Fails before the template change, passes after.
  • Regenerated the affected samples with the locally built CLI (./bin/generate-samples.sh configs java-resttemplate*); the only sample changes are the added annotations on all-args constructor parameters.

Verification done: built modules/openapi-generator-cli (JDK 21), ran the updated JavaClientCodegenTest all-args tests (2 passed), and confirmed the regenerated sample diff contains exclusively the nullability annotations on all-args constructor parameters.

PR checklist:

  • Read the contribution guidelines.
  • Ran the associated tests (mvn test -Dtest=JavaClientCodegenTest).
  • Filed against master.

Summary by cubic

Generate nullability annotations on all-args constructor parameters for Java client models to match fields and setters. Fixes #24109 and makes null-safety consistent for tooling.

  • Bug Fixes
    • Apply the same nullability partial used by setters to all-args constructor params:
      • Java/pojo.mustache{{>nullable_var_annotations}}
      • Java/libraries/{webclient,restclient,resttemplate}/pojo.mustache{{>nullableArgumentWithEnum}}
    • JavaSpring/pojo.mustache already correct; no change.
    • Extend JavaClientCodegenTest#generateAllArgsConstructor to assert @Nullable/@Nonnull on constructor params.
    • Regenerate Java RestTemplate samples (including -jakarta); diffs add only the annotations on constructor parameters.

Written for commit d1db710. Summary will update on new commits.

Review in cubic

…eters

The all-args constructor (generateConstructorWithAllArgs) emitted bare
parameter types, while the fields and fluent setters of the same model
carry @Nullable/@nonnull (jakarta/javax or JSpecify). As a result the
all-args constructor parameters were missing the nullability annotations,
making the generated API inconsistent for null-safety tooling.

Apply the same nullable-annotation partial already used by the fluent
setter in each template to the all-args constructor parameters:
Java/pojo.mustache (nullable_var_annotations) and the webclient,
restclient and resttemplate libraries (nullableArgumentWithEnum).
JavaSpring already applied nullableAnnotation and is unchanged.

Fixes OpenAPITools#24109

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 54 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [JAVA] @Nullable annotations are not generated for constructor with all parameters

1 participant