OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.35k stars 6.46k forks source link

[BUG] [JAVA] Generated classes no longer contain instantiated lists #4091

Open maloef opened 4 years ago

maloef commented 4 years ago

Some time ago, there was a change so that Java lists are instantiated instead of null:

private List<Foo> foos = new ArrayList<>();

instead of

private List<Foo> foos = null;

This was the change that provided instantiated lists: https://github.com/OpenAPITools/openapi-generator/pull/1683

It worked in version 4.0.0 and 4.1.0, but it no longer works in version 4.1.1 and 4.1.2.

macjohnny commented 4 years ago

@maloef thanks for reporting this issue. would you like to implement a fix for this?

wing328 commented 4 years ago

@maloef can you also give the latest stable version (v4.1.3) a try just to ensure the bug is still there?

wing328 commented 4 years ago

Also if I remember correctly, you may want to try marking the property as nullable to see if that helps.

maloef commented 4 years ago

Thanks for your prompt replies! The problem still exists in version 4.1.3.

I tried marking the property as nullable and adding the dependency

<dependency>
    <groupId>org.openapitools</groupId>
    <artifactId>jackson-databind-nullable</artifactId>
    <version>0.2.0</version>
</dependency>

Then this is generated:

private JsonNullable<List<Foo>> foos = JsonNullable.<List<Foo>>undefined();

This is not what we would like to have. We would prefer plain old Java classes.

jmini commented 4 years ago

Some time ago, there was a change so that Java lists are instantiated instead of null.

We reverted that with: https://github.com/OpenAPITools/openapi-generator/pull/3615

See the discussion: https://github.com/OpenAPITools/openapi-generator/issues/1861


The idea behind jackson-databind-nullable is that you need to track if a value is set to null explicitly or not.

This was introduced by @bkabrda in https://github.com/OpenAPITools/openapi-generator/pull/3474, If I remember well you can disable this behavior, but I am not sure.

maloef commented 4 years ago

Hello @jmini Thank you for the links! Now I understand that this behavior makes sense in some cases, but not in others.