How can we set List to null by default in models?
See original GitHub issueWhile generating Java models from OpenAPI spec (v3.0.0) using openapi-generator-maven-plugin, all the Lists in java class are initialized by default to new ArrayList<>(). How do we prevent this?
Description
We have an attribute defined as an array in our OpenAPI spec. While trying to generate the Java model classes, the resulting List is initialized by default to new ArrayList<>(). This is causing problems in our application since an empty arraylist and null are treated differently.
openapi-generator version
openapi-generator-maven-plugin version : 3.3.0
OpenAPI declaration file content or url
"references": {"type" : "array", "items" : {
"type": "object",
"properties" : {
"type": {"type" : "string"},
"source": {"type" : "string"},
"value": {"type" : "string"}
},
"required": ["type", "value"],
"additionalProperties": false
}}
When the maven plugin is run, the resulting java code is
private List<References> references= new ArrayList<>();
The reference field is not mandatory(optional)
What we want is:
private List<References> references = null;
Command line used for generation
Generated using openapi-generator-maven-plugin version : 3.3.0
Steps to reproduce
Related issues/PRs
Suggest a fix
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Related StackOverflow Question
Hi all! Jumping in, as we’re also discussing around those things right now. In our case it would be really cool to have an option to allow using the default initializers for
List/Maptypes even when the respective property is notrequired. We don’t use (or rather allow)nullas a value for container types and Jackson is configured to omit empty containers upon serialization. Anything down to the persistence level is not using the generated classes, so the issue of unwanted behaviour on DB level is out of scope for us as well. For thePATCHuse case we explicitly usenullableso thatJsonNullableis used as the type and all variants for the property (drop/reset, update, ignore) work.We know that @wing328 raised concerns related to too many options (totally valid), still it seems this is the only viable option in this case, allowing to employ the behaviour that is the subject of this issue (which is understandably undesired for many, but also desired for quite a few, including us 😃 )
We’d be glad to discuss on this and also be happy to contribute!
Thank you for the analysis.
An other important reason why null is better than empty list was provided by @bkabrda in https://github.com/OpenAPITools/openapi-generator/issues/3585#issuecomment-519450159
In some scenario (typically with
PATCH) anulllist means “do not change the value” and an empty list means “remove the items of that list”.Setting the value to empty list is confusing because in that case the user needs to set the value to
nullexplicitly, which is an uncommon pattern in Java.With PR #3615 I have proposed to align the
Javatemplate:https://github.com/OpenAPITools/openapi-generator/blob/7f36f2642209c6d7070e1d76209ba1928d816922/modules/openapi-generator/src/main/resources/Java/pojo.mustache#L61
With
JavaJaxRS:https://github.com/OpenAPITools/openapi-generator/blob/7f36f2642209c6d7070e1d76209ba1928d816922/modules/openapi-generator/src/main/resources/JavaJaxRS/pojo.mustache#L24-L29
There the trigger used to determine if the list should be initialized or not is
required. Maybe usage ofisNullableis better?