OpenAPITools / OpenAPITools/openapi-generator

[BUG] [java] [spring] type: [string, "null"] isn't generating JsonNullable properties

Open
#20,827 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
    See below
  • Have you validated the input using an OpenAPI validator
    https://apitools.dev/swagger-parser/online no longer works, so I used https://oas-validation.com/
  • Have you tested with the latest master to confirm the issue still exists?
    I used openapi-generator-cli-7.13.0-20250307.080352-24.jar from sonatype
  • Have you searched for related issues/PRs?
    I've read many issues about nullable and type: 'null', I don't think there is an open issue for this.
  • What's the actual output vs expected output?
    See below
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using allOf and 2 references, the generated classes do not use JsonNullable. However, when the properties are defined inline (without using $ref) JsonNullable is generated. A type: [string, "null"] should generate JsonNullable (or Optional) regardless of whether the field is included with allOf or not.

openapi-generator version

I tested this with 7.11.0 with the maven plugin, as well as the 7.13.0 snapshot build above.
I'm upgrading from version 6.6.0, and spec version 3.0 (replacing type: [string, "null"] with type: string and nullable: true), and I see JsonNullable in both cases.

OpenAPI declaration file content or url

Here is my spec, nullableAllOfSpec.yaml

openapi: 3.1.0
info:
  title: Test API
  version: "1.0"

paths:
  /itemsInline:
    get:
      operationId: getInline
      summary: Get a list of items
      description: Retrieves a list of items
      responses:
        default: # creates GetInlineDefaultResponse, with 1 JsonNullable field and one plain String field
          description: Items retrieved
          content:
            application/json:
              schema:
                type: object
                properties:
                  nonNullablePropertyInline:
                    type: string
                  nullablePropertyInline:
                    type: [string, "null"]
  /itemsAllOf:
    get:
      operationId: getAllOf
      summary: Get a list of items
      description: Retrieves a list of items
      responses:
        default: # creates GetAllOfDefaultResponse, with 2 plain String field, even though NullablePropertyList has a JsonNullable property
          description: Items retrieved
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/NullablePropertyList'
                  - $ref: '#/components/schemas/NonNullablePropertyList'
components:
  schemas:
    NullablePropertyList: # Creates NullablePropertyList, with a JsonNullableProperty
      type: object
      properties:
        nullableProperty:
          type: [ string, "null" ]
    NonNullablePropertyList:
      type: object
      properties:
        nonNullableProperty:
          type: string
Generation Details

java -jar openapi-generator-cli-7.13.0-20250307.080352-24.jar generate -i NullableAllOf/src/main/resources/nullableAllOfSpec.yaml -g spring --library 'spring-boot'

Steps to reproduce

Run the generate command in generation details.
Specifically, I expected GetAllOfDefaultResponse.java to have.

  private @Nullable JsonNullable<String> nullableProperty;

  private @Nullable String nonNullableProperty;

instead of

  private @Nullable String nullableProperty;

  private @Nullable String nonNullableProperty;

Also, I'm not sure about the @Nullable annotation from org.springframework.lang.Nullable. I didn't have this in the previous generator and OAS version, but it seems like JsonNullable is still supported and I'd like to continue using that.

Related issues/PRs

Didn't find any

Suggest a fix

I added some comments in nullableDataType.mustache and nullableDataTypeBeanValidation.mustache, and it appears the isNullable flag is not getting set to true when being passed to nullableDataTypeBeanValidation.mustache, but only in the allOf context when generating GetAllOfDefaultResponse. The same comments indicate isNullable is set to true when generating the NullablePropertyList.

I could look into this further and possibly create a PR, but at this point I was looking for confirmation on if this is a bug or something else happened like I missed a configuration.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided openapi-generator command with nullableAllOfSpec.yaml and compare GetAllOfDefaultResponse.java with NullablePropertyList.java. Start by reading nullableDataType.mustache and nullableDataTypeBeanValidation.mustache, then trace where isNullable is set for the allOf response. Done means the allOf-generated nullableProperty uses JsonNullable like the inline and referenced cases, while the non-nullable property remains a plain String.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.