Skip to content

[Java][Spring] keep @Nullable on inherited fluent setters in allOf children - #25850

Open
shu-chen-dot-ca wants to merge 1 commit into
OpenAPITools:masterfrom
shu-chen-dot-ca:fix-25831-jspecify-parent-fluent-setter
Open

shu-chen-dot-ca wants to merge 1 commit into
OpenAPITools:masterfrom
shu-chen-dot-ca:fix-25831-jspecify-parent-fluent-setter

Conversation

@shu-chen-dot-ca

@shu-chen-dot-ca shu-chen-dot-ca commented Nov 27, 2026 •

Copy link
Copy Markdown

Fixes #25831

With useJspecify=true, the fluent setter a model declares for its own property gets @Nullable (via the nullableArgument_chainSetter partial), but the fluent setters an allOf child re-declares for the parent's properties (the ones that call super.<name>(...)) used the bare {{{datatypeWithEnum}}}. In a @NullMarked package the override then declares a non-null parameter for a method that accepts null, which NullAway and other JSpecify checkers report.

The fix makes the inherited fluent setter in JavaSpring/pojo.mustache use the same nullableArgument_chainSetter partial as the parent's own setter, so the child's signature now always matches the parent's (including type-use placement such as java.time.@Nullable Instant, and the useOptional / optionalAcceptNullable rules). Required non-nullable properties stay unannotated. add*Item / put*Item are unchanged.

Without JSpecify, the child also gets the same @Nullable the parent setter already had for optional properties, which is why the regenerated samples include non-JSpecify Spring samples (signature-only changes on BigCat, ChildWithNullable, the oneOf samples, and so on).

Testing

  • New SpringCodegenTest#testJspecify_inheritedFluentSettersKeepNullable (new spec 3_0/java/jspecify-allof-inherited-setters.yaml), run for all 8 jspecifyLibraries combinations: it fails on master (8 failures) and passes with this change.
  • ./mvnw clean package: build and all unit tests passed.
  • ./bin/generate-samples.sh ./bin/configs/*.yaml (841 generators) and ./bin/utils/export_docs_generators.sh: committed the regenerated samples, no doc changes.
  • mvn clean package on each of the 25 changed Spring samples that CI builds: all passed.
  • Generated the issue's spec with useJspecify=true and openApiNullable true/false, useOptional=true, and useJspecify=false: in every case each Child fluent setter signature is now identical to the Base one, and the output compiles.

Java Spring technical committee: @cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @banlevente @Zomzog @martin-mfg @KannaKim

Before After
before after

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes the inherited fluent setters in allOf children so they keep @Nullable, preventing JSpecify checkers like NullAway from flagging the override in @NullMarked packages.

The change reuses the same nullableArgument_chainSetter partial used for a model's own property setters, so child signatures always match the parent's, including type-use placement (java.time.@Nullable Instant) and the useOptional/optionalAcceptNullable rules. Required properties stay unannotated and add*Item/put*Item are untouched. Without JSpecify, children now also get the same @Nullable as the parent setter for optional properties.

Testing

  • Added SpringCodegenTest#testJspecify_inheritedFluentSettersKeepNullable with a new spec, run across all 8 jspecifyLibraries combinations (fails on master, passes here).
  • Regenerated all samples; the diff is signature-only @Nullable additions on the affected models.

Written for commit 9a6b7b3. Summary will update on new commits.

View guided diff

@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.

2 issues found across 75 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/test/resources/3_0/java/jspecify-allof-inherited-setters.yaml">

<violation number="1" location="modules/openapi-generator/src/test/resources/3_0/java/jspecify-allof-inherited-setters.yaml:15">
P2: The fixture has no required property that is also nullable, so the regression test never covers that case. Add a `nullable: true` required property to `Base` and assert its inherited `Child` setter keeps `@Nullable`.</violation>
</file>

<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java:8419">
P3: This regression test only covers the `useOptional=false` path. The PR changes inherited setters to use `nullableArgument_chainSetter`, which has separate `useOptional` / `optionalAcceptNullable` branches, but no test case asserts those inherited setters. Add a case with `useOptional` enabled (and `optionalAcceptNullable` on and off) to lock in the intended signatures.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

properties:
kind:
type: string
description:

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.

P2: The fixture has no required property that is also nullable, so the regression test never covers that case. Add a nullable: true required property to Base and assert its inherited Child setter keeps @Nullable.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/test/resources/3_0/java/jspecify-allof-inherited-setters.yaml, line 15:

<comment>The fixture has no required property that is also nullable, so the regression test never covers that case. Add a `nullable: true` required property to `Base` and assert its inherited `Child` setter keeps `@Nullable`.</comment>

<file context>
@@ -0,0 +1,29 @@
+      properties:
+        kind:
+          type: string
+        description:
+          type: string
+          nullable: true
</file context>

}

@Test(dataProvider = "jspecifyLibraries")
public void testJspecify_inheritedFluentSettersKeepNullable(String library, int springBootVersion) throws IOException {

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.

P3: This regression test only covers the useOptional=false path. The PR changes inherited setters to use nullableArgument_chainSetter, which has separate useOptional / optionalAcceptNullable branches, but no test case asserts those inherited setters. Add a case with useOptional enabled (and optionalAcceptNullable on and off) to lock in the intended signatures.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java, line 8419:

<comment>This regression test only covers the `useOptional=false` path. The PR changes inherited setters to use `nullableArgument_chainSetter`, which has separate `useOptional` / `optionalAcceptNullable` branches, but no test case asserts those inherited setters. Add a case with `useOptional` enabled (and `optionalAcceptNullable` on and off) to lock in the intended signatures.</comment>

<file context>
@@ -8415,6 +8415,37 @@ public void testJspecify_useOptional(String library, int springBootVersion, bool
     }
 
+    @Test(dataProvider = "jspecifyLibraries")
+    public void testJspecify_inheritedFluentSettersKeepNullable(String library, int springBootVersion) throws IOException {
+        // https://github.com/OpenAPITools/openapi-generator/issues/25831
+        String springVersionProperty = springBootVersion == 4? USE_SPRING_BOOT4: USE_SPRING_BOOT3;
</file context>

This branch has not been deployed

No deployments
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][Spring] useJspecify: fluent setter override in an allOf child drops @Nullable from the parent's nullable property

1 participant