Bug Report Checklist
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
- Model with attribute of type
array and nullable: true.
- Generate with generator
java and library webclient.
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.
Bug Report Checklist
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 aNullPointerException. With 6.2.1, if theJsonNullabledidn't contain a value, it was initialized with a newArrayList.Generated builder method with 6.2.1:
Generated builder method with 6.3.0:
openapi-generator version
6.3.0 (no errors with 6.2.1)
OpenAPI declaration file content or url
Generation Details
java -jar openapi-generator-cli-6.3.0.jar generate -g java --library webclient -i spec.yaml -o outSteps to reproduce
arrayandnullable: true.javaand librarywebclient.TestObject#addMyNullableArrayPropIteminitializes property value withnull, causing aNullPointerExceptionin the next statement.Related issues/PRs
#14130
Suggest a fix
Check if the property's type is
array, initialize it with an emptyArrayList, as before.