Skip to content

Add support for JSpecify annotations in SpringCodegen#23233

Open
felldo wants to merge 3 commits intoOpenAPITools:masterfrom
felldo:spring-jspecify
Open

Add support for JSpecify annotations in SpringCodegen#23233
felldo wants to merge 3 commits intoOpenAPITools:masterfrom
felldo:spring-jspecify

Conversation

@felldo
Copy link

@felldo felldo commented Mar 13, 2026

  • Introduced useJSpecify flag to enable the use of JSpecify's @Nullable annotations.
  • Modified POJO templates to correctly place JSpecify annotations for TYPE_USE compatibility.
  • Updated importMappings to handle hardcoded JSpecify annotations when enabled.
  • Added a test case for verifying correct annotation placement and behavior (issue_23206.yaml and corresponding Java test).
  • fixes [BUG] [JAVA] [SPRING] Impossible to use JSpecify with SchemaMappings #23206

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • 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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • 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

Adds an optional useJSpecify flag in SpringCodegen to generate JSpecify @Nullable as a TYPE_USE annotation in POJOs and include the org.jspecify:jspecify dependency when enabled. Fixes incorrect @Nullable placement and enables JSpecify-compatible builds (fixes #23206).

  • New Features

    • useJSpecify: when true, maps Nullable to org.jspecify.annotations.Nullable (overrides any user importMapping).
    • Emits @Nullable on the type for fields, getters, and setters (e.g., a.b.c.@Nullable TypeName).
    • Simplifies pojo.mustache to insert type-use annotations via x-jspecify-annotated-type.
    • Conditionally adds org.jspecify:jspecify:1.0.0 to Spring pom.mustache templates (spring-boot, spring-cloud, spring-http-interface).
    • Adds tests for schema-mapped and imported types; updates docs for Spring and Java Camel generators.
  • Migration

    • Off by default. Enable with --additional-properties useJSpecify=true.

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

@bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg

- Introduced `useJSpecify` flag to enable the use of JSpecify's `@Nullable` annotations.
- Modified POJO templates to correctly place JSpecify annotations for `TYPE_USE` compatibility.
- Updated importMappings to handle hardcoded JSpecify annotations when enabled.
- Added a test case for verifying correct annotation placement and behavior (`issue_23206.yaml` and corresponding Java test).
Copy link
Contributor

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

Choose a reason for hiding this comment

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

1 issue found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="docs/generators/spring.md">

<violation number="1" location="docs/generators/spring.md:105">
P2: `useJSpecify` is documented and generates JSpecify annotation usage, but Spring build templates do not appear to add the required `org.jspecify:jspecify` dependency, risking uncompilable generated projects.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

|useEnumCaseInsensitive|Use `equalsIgnoreCase` when String for enum comparison| |false|
|useFeignClientContextId|Whether to generate Feign client with contextId parameter.| |true|
|useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|
|useJSpecify|Use JSpecify's @Nullable (org.jspecify.annotations.Nullable) instead of Spring's @Nullable. Overrides any user-supplied importMapping for 'Nullable'.| |false|
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 13, 2026

Choose a reason for hiding this comment

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

P2: useJSpecify is documented and generates JSpecify annotation usage, but Spring build templates do not appear to add the required org.jspecify:jspecify dependency, risking uncompilable generated projects.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At docs/generators/spring.md, line 105:

<comment>`useJSpecify` is documented and generates JSpecify annotation usage, but Spring build templates do not appear to add the required `org.jspecify:jspecify` dependency, risking uncompilable generated projects.</comment>

<file context>
@@ -102,6 +102,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
 |useEnumCaseInsensitive|Use `equalsIgnoreCase` when String for enum comparison| |false|
 |useFeignClientContextId|Whether to generate Feign client with contextId parameter.| |true|
 |useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|
+|useJSpecify|Use JSpecify's @Nullable (org.jspecify.annotations.Nullable) instead of Spring's @Nullable. Overrides any user-supplied importMapping for 'Nullable'.| |false|
 |useJackson3|Set it in order to use jackson 3 dependencies (only allowed when `useSpringBoot4` is set and incompatible with `openApiNullable`).| |false|
 |useJakartaEe|whether to use Jakarta EE namespace instead of javax| |false|
</file context>
Fix with Cubic

Co-authored-by: Christopher Molin <28791817+Chrimle@users.noreply.github.com>
Copy link
Contributor

@Chrimle Chrimle left a comment

Choose a reason for hiding this comment

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

Agree that the generated pom.xml will need a jspecify dependency... unless it's a transitive dependency already? 🤔

@felldo
Copy link
Author

felldo commented Mar 13, 2026

Agree that the generated pom.xml will need a jspecify dependency... unless it's a transitive dependency already? 🤔

Spring Boot 4 should already include JSpecify because they just switched to it from their own Null annotations. But earlier versions shouldn't provide it. So I guess we still need to add it?

@Chrimle
Copy link
Contributor

Chrimle commented Mar 13, 2026

Hmm... there is a configOption useSpring4/useSpring3... perhaps wrap the dependency import based on that or something?

Edit:

{{#useSpringBoot4}}
spring.jackson.datatype.datetime.WRITE_DATES_AS_TIMESTAMPS=false
{{/useSpringBoot4}}
{{^useSpringBoot4}}
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
{{/useSpringBoot4}}

It's useSpringBoot4.

So these files:

  • JavaSpring/libraries/spring-boot/pom.mustache
  • JavaSpring/libraries/spring-boot/pom-sb3.mustache
  • JavaSpring/libraries/spring-boot/pom-sb4.mustache
  • JavaSpring/libraries/spring-cloud/pom.mustache
  • JavaSpring/libraries/spring-cloud/pom-sb3.mustache
  • JavaSpring/libraries/spring-cloud/pom-sb4.mustache
  • JavaSpring/libraries/spring-http-interface/pom.mustache
  • JavaSpring/libraries/spring-http-interface/pom-sb4.mustache

- Integrated the `useJSpecify` flag to conditionally include the JSpecify dependency in multiple `pom.mustache` templates.
Copy link
Contributor

@Chrimle Chrimle left a comment

Choose a reason for hiding this comment

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

Beautiful ❤️

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] Impossible to use JSpecify with SchemaMappings

2 participants