OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Default value of required array
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the schema with the Maven plugin's generate goal and inspect the generated MyEnclosingObject Java model, especially the myArray field and addMyArray method. Done means a required array remains null when absent from the API request, while addMyArray initializes it when an item is added.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100