openapi-generator: [BUG] [JAVA] [webclient] `addXYZ` Builder Method in Pojo produces `NullPointerException` for nullable array properties
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
- Model with attribute of type
arrayandnullable: true. - Generate with generator
javaand librarywebclient. TestObject#addMyNullableArrayPropIteminitializes property value withnull, causing aNullPointerExceptionin the next statement.
Related issues/PRs
Suggest a fix
Check if the property’s type is array, initialize it with an empty ArrayList, as before.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 3
- Comments: 16 (9 by maintainers)
thanks for the info. i’ll try to come up with a PR to address it this or next week.
Also happens for the
JavaSpringgenerator.