-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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] [webclient] addXYZ
Builder Method in Pojo produces NullPointerException
for nullable array properties
#14583
Comments
addXYZ
Builder Method in Pojo produces NullPointerException
for nullable array properties
can you set the option |
Both |
what about setting |
With public TestObject addMyNullableArrayPropItem(String myNullableArrayPropItem) {
if (this.myNullableArrayProp == null) {
this.myNullableArrayProp = new ArrayList<>();
}
this.myNullableArrayProp.add(myNullableArrayPropItem);
return this;
} With public TestObject addMyNullableArrayPropItem(String myNullableArrayPropItem) {
if (this.myNullableArrayProp == null) {
this.myNullableArrayProp = null;
}
this.myNullableArrayProp.add(myNullableArrayPropItem);
return this;
} For completeness, here's the other two permutations: public TestObject addMyNullableArrayPropItem(String myNullableArrayPropItem) {
if (this.myNullableArrayProp == null || !this.myNullableArrayProp.isPresent()) {
this.myNullableArrayProp = JsonNullable.<List<String>>of(null);
}
try {
this.myNullableArrayProp.get().add(myNullableArrayPropItem);
} catch (java.util.NoSuchElementException e) {
// this can never happen, as we make sure above that the value is present
}
return this;
}
public TestObject addMyNullableArrayPropItem(String myNullableArrayPropItem) {
if (this.myNullableArrayProp == null || !this.myNullableArrayProp.isPresent()) {
this.myNullableArrayProp = JsonNullable.<List<String>>of(null);
}
try {
this.myNullableArrayProp.get().add(myNullableArrayPropItem);
} catch (java.util.NoSuchElementException e) {
// this can never happen, as we make sure above that the value is present
}
return this;
} |
thanks for the info. i'll try to come up with a PR to address it this or next week. |
We want to use |
Also happens for the |
@wing328 do you know why this changes from a empty collection to a null value? Make this sense into a JsonNullable? For us the empty collection (List or Set) was fine and works. |
Hi all, I've file a fix: #14703. Please test it when you've time to confirm it addresses the issues in your use cases. Thank you. |
PR merged. Please give it a try with the SNAPSHOT JAR |
@wing328: The SNAPSHOT jar produces identical output and thus doesn't fix the issue. |
Can you try https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/6.4.0-SNAPSHOT/openapi-generator-cli-6.4.0-20230216.072505-21.jar instead ? (6.4.0-SNAPSHOT JAR) Sorry that the one I provided earlier was for v6.3.0 (which was released already). |
The new snapshot fixed the issue for the example spec. Give me a bit more time to verify that with my actual application. |
Please run more tests 🙏 |
@wing328: I successfully tested the snapshot with my actual application, so this fix works for me, thanks for the fix. |
Same issue |
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 theJsonNullable
didn'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 out
Steps to reproduce
array
andnullable: true
.java
and librarywebclient
.TestObject#addMyNullableArrayPropItem
initializes property value withnull
, causing aNullPointerException
in 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.The text was updated successfully, but these errors were encountered: