[Java] Generate nullability annotations on all-args constructor parameters#24114
Open
seonwooj0810 wants to merge 1 commit into
Open
[Java] Generate nullability annotations on all-args constructor parameters#24114seonwooj0810 wants to merge 1 commit into
seonwooj0810 wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 bygenerateConstructorWithAllArgsemitted bare parameter types ({{{datatypeWithEnum}}}). So a nullable optional property appeared asprivate @javax.annotation.Nullable String baz;with a matching@Nullablesetter, yet the all-args constructor parameterbazcarried 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,resttemplatepojo.mustache→{{>nullableArgumentWithEnum}}JavaSpring/pojo.mustachealready applied{{>nullableAnnotation}}to its all-args constructor and is left unchanged.Effect (regenerated samples)
e.g.
samples/.../resttemplate/.../Pet.javaall-args constructor now matches its setters — required props get@Nonnull, optional props get@Nullable:Test / verification
JavaClientCodegenTest#generateAllArgsConstructorto assert the all-args constructor parameters carry@javax.annotation.Nullable(optionaldateOfBirth,name) and@javax.annotation.Nonnull(requiredtype). Fails before the template change, passes after../bin/generate-samples.shconfigsjava-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 updatedJavaClientCodegenTestall-args tests (2 passed), and confirmed the regenerated sample diff contains exclusively the nullability annotations on all-args constructor parameters.PR checklist:
mvn test -Dtest=JavaClientCodegenTest).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.
Java/pojo.mustache→{{>nullable_var_annotations}}Java/libraries/{webclient,restclient,resttemplate}/pojo.mustache→{{>nullableArgumentWithEnum}}JavaSpring/pojo.mustachealready correct; no change.JavaClientCodegenTest#generateAllArgsConstructorto assert@Nullable/@Nonnullon constructor params.-jakarta); diffs add only the annotations on constructor parameters.Written for commit d1db710. Summary will update on new commits.