Skip to content

[BUG] [JAVA] [webclient] addXYZ Builder Method in Pojo produces NullPointerException for nullable array properties #14583

@kzander91

Description

@kzander91

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

For nullable arrays, the POJO generated by 6.3.0 no longer initializes the property with an empty list when using the addXYZ(T item) builder method to add an item to the property. This produces a NullPointerException. With 6.2.1, if the JsonNullable didn't contain a value, it was initialized with a new ArrayList.

Generated builder method with 6.2.1:

public class TestObject {
  // other stuff

  private JsonNullable<List<String>> nullableArrayProp = JsonNullable.<List<String>>undefined();

  // other stuff

  public TestObject addNullableArrayPropItem(String nullableArrayPropItem) {
    if (this.nullableArrayProp == null || !this.nullableArrayProp.isPresent()) {
      this.nullableArrayProp = JsonNullable.<List<String>>of(new ArrayList<>()); // <-- initialized with empty list
    }
    try {
      this.nullableArrayProp.get().add(nullableArrayPropItem); // <-- no NPE here
    } catch (java.util.NoSuchElementException e) {
      // this can never happen, as we make sure above that the value is present
    }
    return this;
  }

  // other stuff
}

Generated builder method with 6.3.0:

public class TestObject {
  // other stuff

  private JsonNullable<List<String>> nullableArrayProp = JsonNullable.<List<String>>undefined();

  // other stuff

  public TestObject addNullableArrayPropItem(String nullableArrayPropItem) {
    if (this.nullableArrayProp == null || !this.nullableArrayProp.isPresent()) {
      this.nullableArrayProp = JsonNullable.<List<String>>of(null); // <-- initialized with null
    }
    try {
      this.nullableArrayProp.get().add(nullableArrayPropItem); // NPE here
    } catch (java.util.NoSuchElementException e) {
      // this can never happen, as we make sure above that the value is present
    }
    return this;
  }

  // other stuff
}
openapi-generator version

6.3.0 (no errors with 6.2.1)

OpenAPI declaration file content or url
openapi: 3.0.3
info:
    title: Title
    version: "1"
paths:
  /test:
    get:
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TestObject'
components:
  schemas:
    TestObject:
      type: object
      properties:
        myNullableArrayProp:
          type: array
          nullable: true
          items:
            type: string
Generation Details

java -jar openapi-generator-cli-6.3.0.jar generate -g java --library webclient -i spec.yaml -o out

Steps to reproduce
  1. Model with attribute of type array and nullable: true.
  2. Generate with generator java and library webclient.
  3. TestObject#addMyNullableArrayPropItem initializes property value with null, causing a NullPointerException in the next statement.
Related issues/PRs

#14130

Suggest a fix

Check if the property's type is array, initialize it with an empty ArrayList, as before.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions