Repository navigation
Description
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?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
For Java, the default value of an array is an empty ArrayList as soon as it is declared as "required" in the enclosing object. In our case, we must reject the null value with the appropriate validation: @javax.validation.constraints.NotNull. However, with this default value, we can only trigger another kind of validation: @javax.validation.constraints.NotEmpty, which can be misleading for our API users since the field is not present at all. This behavior does not seem to be consistent with the other types (string, number, integer, boolean, object) which remain at null despite the required property.
openapi-generator version
'org.openapi.generator' version '5.3.0' but reproduced in 6.0.0
OpenAPI declaration file content or url
Given :
MyEnclosingObject:
type: object
required:
- my_array
properties:
my_array:
type: array
items:
$ref: '#/components/schemas/MyItem'Expected generated source :
public class MyEnclosingObject {
@JsonProperty("my_array")
@Valid
private List<MyArray> myArray = null;
public MyEnclosingObject myArray(List<MyArray> myArray) {
this.myArray = myArray;
return this;
}
public MyEnclosingObject addMyArray(MyItem myItem) {
if (myArray == null) {
myArray = new ArrayList<>();
}
this.myArray.add(myItem);
return this;
}
@ApiModelProperty(required = true)
@NotNull @Valid
public List<StandardPrice> getStandardPrices() {
return standardPrices;
}
}Actual generated source :
public class MyEnclosingObject {
@JsonProperty("my_array")
@Valid
private List<MyArray> myArray = new ArrayList<>();
public MyEnclosingObject myArray(List<MyArray> myArray) {
this.myArray = myArray;
return this;
}
public MyEnclosingObject addMyArray(MyItem myItem) {
this.myArray.add(myItem);
return this;
}
@ApiModelProperty(required = true)
@NotNull @Valid
public List<MyItem> getMyArray() {
return standardPrices;
}
}Generation Details
Maven plugin in generate goal.
org.openapitools
openapi-generator-maven-plugin
Steps to reproduce
Define a contract of an object with a required array and call the API without that array and observe that the received array is empty instead of null.
Related issues/PRs
None found
Suggest a fix
The default value should be moved to addMyArray when we know we have at least one myItem
Activity
[-]BUG][JAVA] Default value of required array[/-][+][BUG][JAVA] Default value of required array[/+]wing328 commentedon Jul 20, 2023
can you please try v7.0.0-beta? there are some improvements related to java array default value if I remember correctly.
cpandya231 commentedon Aug 8, 2023
@wing328 I tried with 7.0.0-beta, the issue still remains.
wing328 commentedon Aug 8, 2023
can you please provide a minimal spec to reproduce the issue?
cpandya231 commentedon Aug 8, 2023
Generated method
wing328 commentedon Aug 8, 2023
I did a test with the latest master using
javagenerator (default library: oktthp-gson) and gotwhich looks to be correct.
cpandya231 commentedon Aug 9, 2023
@wing328 You are right, it works with 7.0.0-beta