Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
jmuellerdeposco opened this issue Mar 7, 2025 · 0 comments
Open
5 of 6 tasks

Comments

@jmuellerdeposco
Copy link

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.

@jmuellerdeposco jmuellerdeposco changed the title [BUG] [java] [spring] type: [string, "null"] isn't [BUG] [java] [spring] type: [string, "null"] isn't generating JsonNullable properties Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant