Skip to content

Fix Jspecify nullable issue with spring boot 4#23649

Open
JFCote wants to merge 2 commits intomasterfrom
bugfixes/fix-jspecify-nullable-with-springboot-4
Open

Fix Jspecify nullable issue with spring boot 4#23649
JFCote wants to merge 2 commits intomasterfrom
bugfixes/fix-jspecify-nullable-with-springboot-4

Conversation

@JFCote
Copy link
Copy Markdown
Member

@JFCote JFCote commented Apr 28, 2026

@cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

Very small fix to the Spring Generator. The path parameter was the only one that did not add nullableAnnotation which did not reset the internal state.

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

Fixes missing nullability on Spring path parameters for Spring Boot 4 by adding the nullableAnnotation. This makes JSpecify handling consistent and prevents state leakage during generation.

  • Bug Fixes
    • Added {{>nullableAnnotation}} before {{>optionalDataType}} in JavaSpring/pathParams.mustache so path params reset nullability state and get the correct annotation.

Written for commit 8284110. Summary will update on new commits. Review in cubic

Copy link
Copy Markdown
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.

No issues found across 1 file

@jpfinne
Copy link
Copy Markdown
Contributor

jpfinne commented Apr 29, 2026

@JFCote pathParams are always required in Spring.
Not sure if there was a bug.

The build succeeds. It means that no Spring sample is impacted.
Can you share your openapi api?

@raichuchuchu
Copy link
Copy Markdown
Contributor

Hay I think you just solve my problem #23653

@JFCote
Copy link
Copy Markdown
Member Author

JFCote commented Apr 29, 2026

@jpfinne See below for my spec but you can also check the @raichuchuchu bugs he just shared, this is exactly the issue!

#23653

Here is the openapi spec I used for my tests:

openapi: 3.0.0
info:
  title: API - Address
  version: 1.0.0
tags:
  - name: Address
    description: Address calls

paths:
  /api/addresses/{addressId}:
    put:
      summary: Updates an address by ID
      operationId: updateAddress
      tags:
        - Address
      parameters:
        - $ref: '#/components/parameters/addressId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AddressCore'
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Address'
        '404':
          description: Not Found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
    delete:
      summary: Deletes an address by ID
      operationId: deleteAddress
      tags:
        - Address
      parameters:
        - $ref: '#/components/parameters/addressId'
      responses:
        '204':
          description: No Content

components:
  parameters:
    addressId:
      name: addressId
      in: path
      required: true
      description: The ID of the address
      schema:
        type: integer
        format: int64
        example: 12345

  schemas:
    AddressCore:
      type: object
      properties:
        friendlyName:
          type: string
          example: "Headquarters"
        addressLine1:
          type: string
          example: "123 Main St"
        addressLine2:
          type: string
          example: "Suite 456"
        city:
          type: string
          example: "Sherbrooke"
        province:
          type: string
          example: "Quebec"
        postalCode:
          type: string
          example: "J1H 5N4"
        country:
          type: string
          example: "Canada"
      required:
        - friendlyName
        - addressLine1
        - city
        - province
        - postalCode
        - country

    Address:
      allOf:
        - $ref: '#/components/schemas/AddressCore'
        - type: object
          properties:
            id:
              type: integer
              format: int64
              example: 12345
            updatedAt:
              type: string
              format: date-time
              example: "2025-08-05T18:48:59.149Z"
          required:
            - id
            - updatedAt
    Error:
      type: object
      properties:
        timestamp:
          type: string
          format: date-time
          example: "2025-08-05T18:48:59.149Z"
        status:
          type: integer
          format: int32
          example: 404
        message:
          type: string
          example: "Resource not found"
        path:
          type: string
          example: "/api/addresses/12345"

And I generate with this command: java.exe -jar openapi-generator-cli.jar generate --generator-name spring --input-spec openapi.yaml --output generatedTest --api-package ca.acme.controllers --model-package ca.acme.models --model-name-suffix Model --additional-properties interfaceOnly=false,useTags=true,library=spring-boot,generateBuilders=true,generateConstructorWithAllArgs=true,hideGenerationTimestamp=true,useSwaggerUI=true,skipDefaultInterface=false,useSpringBoot4=true,useJspecify=true

Without my fix, the first method deleteAddress will have a @nullable even if the path parameter is required. The "updateAddress` will not have the @nullable. I dig very deep with the help of Codex and found that there is an object that is stateful that manage this and without my addition, the first one will always be nullable.

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.

3 participants