How can we set List to null by default in models?

See original GitHub issue

While 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:open
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
andreas-schillingcommented, Aug 16, 2019

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/Map types even when the respective property is not required. We don’t use (or rather allow) null as 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 the PATCH use case we explicitly use nullable so that JsonNullable is 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!

0reactions
jminicommented, Aug 14, 2019

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) a null list 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 null explicitly, which is an uncommon pattern in Java.


With PR #3615 I have proposed to align the Java template:

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 of isNullable is better?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Model Field Default to Null - python - Stack Overflow
Try default=None . There is no NULL in python.
Read more >
Django Tutorial Part 3: Using models - Learn web development
SET_NULL , which will set the value of the book's author field to Null if the associated author record is deleted. Warning: By...
Read more >
Understanding null safety - Dart
Code should be safe by default. If you write new Dart code and don't use any explicitly unsafe features, it never throws a...
Read more >
Filtering for Empty or Null Values in a Django QuerySet - Chartio
Easily the most important method when working with Django models and the underlying QuerySets is the filter() method, which allows you to generate...
Read more >
Creating forms from models - Django documentation
CharField with max_length set to the model field's max_length and empty_value set to None if null=True . DateField · DateField · DateTimeField ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found