You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG][JAVA] optional collection fields are initialized since 7.5.0 #18735
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Hello,
first: thank you for the great tool.
Description
Our classes look not the same anymore since generator version 7.5.0. All the collections are initialized. I could use the 'containerDefaultToNull' property, but than all collections are null, also the required. Before I change our code I would like to know if this is an expected behavior?
As the test SpringCodegenTest#testCollectionTypesWithDefaults_issue_collection requires I expect the nullable fields to be null and the required fields to be initialised, but both are.
If it's an issue and not expected behavior, I can work on a fix.
Reacted by Daniel Hammer, haeuserd, Christian Ciach, Tom Deering, Patrick Boos, Dmitry Lebedko, Svaťa Šimara, Max Dudzinski, Kevin Nammour and Kristina Atanasova
Maybe the main issue is with the handling of containerDefaultToNull that could have a wrong implementation for required collections.
As you say: "I could use the 'containerDefaultToNull' property, but than all collections are null, also the required"
See #14310
I have difficulties finding the "correct" implementation for required and nullable yes/no (not to mention openapiNullable and useOptional for the spring generator). Look for example at the crazy discussion in #14765 (47 comments!)
Finally it was implemented and merged in #17202. That produces unmaintanable mustache templates, multiple bugs and regressions (for example there are NullPointerExceptions when using optional in spring!)
It seems people agree to disagree.
To end the debate we could define a matrix defining the behaviour of nullable/required in the contracts + configOptions in the generator. And potentially adding new configOptions to satisfy everyone's opinion.
Reacted by Daniel Hammer, Randy Hector Bartumeu Huergo and Tom Deering
@mosesonline, what's the status of this fix? I saw that you created a PR, but that it had at least one failing suite. Is there a way that I or my team can help? This same issue is causing a downstream problem in the Java client. Thanks.
@mosesonline I understand your frustation.
I could work on that issue. But currently I have the feeling that several PR are not reviewed nor merged.
It's quite discouraging.
Thanks @jpfinne. I see that the project has a huge number of open PR's. Is there a way that I can help shepherd this one or encourage it to get attention? At least for the Java client, it is making current releases of the client unusable because the client generates empty content "{}" for fields that should be unset.
I could work on that issue. But currently I have the feeling that several PR are not reviewed nor merged.
It's quite discouraging.
@jpfinne sorry to hear that. if you can keep the PR small, definitely it will be much easier to review.
i know contributors like to have one (big) PRs with several enhancements/bug fixes to make their life easier but a PR changing several hundred files (including samples update) is just not easy to review/get it merged.
@rjeberhard if you can help review other PRs and ideally test these locally, that would be great (you can mark the PR as "approved" to draw our attentions).
Yes, often I could modify the code or modify the api. That's what you have to do in case of breaking change. But in our case the API definition is not in our hands and we would like to prevent to modificate the definition on every new release.
I understand why you decided to change the behavior. I don't agree to do the change in 7.x. But if you don't want to change it back it's ok. I can imagine that most people don't care or changed the implementation already.
Yes, often I could modify the code or modify the api. That's what you have to do in case of breaking change. But in our case the API definition is not in our hands and we would like to prevent to modificate the definition on every new release.
I understand why you decided to change the behavior. I don't agree to do the change in 7.x. But if you don't want to change it back it's ok. I can imagine that most people don't care or changed the implementation already.
To clarify, I have not changed the behaviour. I in version 7.5.0 found that behaviour change and I have adapted to it (I have hundreds of apis...) because I think it is correct that if the nullable field is not defined, the collection must be initialized.
Also, correct me if I'm wrong, but for collections not to be initialized, there is the property containerDefaultToNull. Isn't that enough?
Hi @jpfinne, I like your proposal! Also, thanks for preparing a PR already.
I am wondering if anyone needs different initialization behaviours for array vs set vs map? If so, please let us know here. Otherwise I'd propose to not make this distinction.
Since in the proposed changes the option is still named containerDefaultToNull, it should not be deprecated. But the old valid values - "true" and "false" - should still work the same as before.
Since containerDefaultToNull is available only in the Java based generators, I think SET_CONTAINER_TO_NULLABLE should not be deprecated. So other generators can still use that option.
I think your proposal is an excelent compromise. @jorgerod , @jpfinne . Thanks for moving the issue ahead.
In our use-case there is no need to differentiate between different container types, @martin-mfg .
Hopefully we can get the PR merged soon.
Reacted by Martin, Slawomir Messner and Dmitry Lebedko
Hi @martin-mfg, I noticed you received one approval for the PR addressing this issue. It’s likely to be merged soon? If there’s anything I can assist with, I’d be happy to help.
Hi @martin-mfg, I noticed you received one approval for the PR addressing this issue. It’s likely to be merged soon? If there’s anything I can assist with, I’d be happy to help.
Hi, I am waiting for @wing328 to merge the PR. I think there's nothing else to be done.
Reacted by Julio Gómez Díaz, Bodo Graumann and Dmitry Lebedko
I believe there are conceptual issues with the current approach to handling default values.
Reference from Standards:
OAS 3.0.1: "The default value represents what would be assumed by the consumer if a value is not provided."
JSON Schema 2020-12: "This keyword can supply a default JSON value associated with a schema."
Understanding JSON Schema: "default is used to express that if a value is missing, it is the same as if it was present with the default value."
Current Issues:
Defaults During Field Initialization:
Assigning default values during field initialization affects JSON serialization, which is not the intended purpose. Defaults should handle missing data during deserialization, not change serialization behavior.
Inconsistent Implicit Defaults:
Lists and maps are defaulted to empty collections, while strings are not (e.g., not set to empty strings). This inconsistency can cause confusion and unexpected behavior.
Suggestions for Improvement:
Avoid Implicit Defaults:
Ensure consistency by explicitly defining all defaults, with no implicit defaults for any type.
Remove containerDefaultToNull:
If no explicit default is defined, null should be used by default, removing the need for containerDefaultToNull and simplifying behavior.
Apply Defaults Only During JSON Deserialization:
Assign defaults only during deserialization using an all-args factory method annotated with @JsonCreator. This ensures defaulting is limited to deserialization and doesn't impact serialization.
Thanks for all the feedback, suggestions, PRs, etc.
I've been thinking about this more and more and I think we all agree ideally we need to solution that works in all generators instead of just Java-related generators.
I'll try to come up with something later this week or next so that you guys can try it out via a PR.
Best regards,
William
Reacted by Ryan Eberhard, Vlad Ionuț Cananău, jbarrios-twilio and Kevin Nammour
Thanks for all the feedback, suggestions, PRs, etc.
I've been thinking about this more and more and I think we all agree ideally we need to solution that works in all generators instead of just Java-related generators.
I'll try to come up with something later this week or next so that you guys can try it out via a PR.
Best regards, William
Hi @wing328 , do you need some help in this? This is also blocking from our side, and there's no temporary workaround as we have thousands of specs, and many of them we don't own/we can't change. We have an openapi generator wrapper in which we'd like to rollback to the 7.4.0 behavior of initializing containers while using the 7.13.0 version.
Activity
Fix for OpenAPITools#18735 optional collections are initialized
jpfinne commentedon May 22, 2024
@mosesonline
The changes was done in #15891 to "fix" #18080 (23 comments!)
So you ask to revert the behaviour. IMHO you're right.
The use of !required was intially introduced by @wing328 in #14961 to fix #14875 and #14833.
Maybe the main issue is with the handling of containerDefaultToNull that could have a wrong implementation for required collections.
As you say: "I could use the 'containerDefaultToNull' property, but than all collections are null, also the required"
See #14310
I have difficulties finding the "correct" implementation for required and nullable yes/no (not to mention openapiNullable and useOptional for the spring generator). Look for example at the crazy discussion in #14765 (47 comments!)
Finally it was implemented and merged in #17202. That produces unmaintanable mustache templates, multiple bugs and regressions (for example there are NullPointerExceptions when using optional in spring!)
It seems people agree to disagree.
To end the debate we could define a matrix defining the behaviour of nullable/required in the contracts + configOptions in the generator. And potentially adding new configOptions to satisfy everyone's opinion.
rjeberhard commentedon Jun 12, 2024
@mosesonline, what's the status of this fix? I saw that you created a PR, but that it had at least one failing suite. Is there a way that I or my team can help? This same issue is causing a downstream problem in the Java client. Thanks.
jpfinne commentedon Jun 12, 2024
@mosesonline I understand your frustation.
I could work on that issue. But currently I have the feeling that several PR are not reviewed nor merged.
It's quite discouraging.
rjeberhard commentedon Jun 12, 2024
Thanks @jpfinne. I see that the project has a huge number of open PR's. Is there a way that I can help shepherd this one or encourage it to get attention? At least for the Java client, it is making current releases of the client unusable because the client generates empty content "{}" for fields that should be unset.
wing328 commentedon Jun 12, 2024
@jpfinne sorry to hear that. if you can keep the PR small, definitely it will be much easier to review.
i know contributors like to have one (big) PRs with several enhancements/bug fixes to make their life easier but a PR changing several hundred files (including samples update) is just not easy to review/get it merged.
@rjeberhard if you can help review other PRs and ideally test these locally, that would be great (you can mark the PR as "approved" to draw our attentions).
mosesonline commentedon Jun 17, 2024
Sorry, for being silent. I would be happy for any help to fix theis issue.
Fix for OpenAPITools#18735 optional collections are initialized
Fix for OpenAPITools#18735 optional collections are initialized
jorgerod commentedon Jun 26, 2024
Hello @wing328 @mosesonline @jpfinne
This is again a breaking change. We cannot absorb these changes in minor versions.
Also, in my opinion it seems wrong. By default, nullable is false, so if you don't set that property, collections must be initialised.
mosesonline commentedon Jun 26, 2024
The initial change to initialize the collections was also a breaking change. It broke our code.
jorgerod commentedon Jun 26, 2024
The same thing happened to me...
But the solution I think is to add to the API
nullable: true(remember that by default nullable is false).What do you think?
mosesonline commentedon Jun 26, 2024
Yes, often I could modify the code or modify the api. That's what you have to do in case of breaking change. But in our case the API definition is not in our hands and we would like to prevent to modificate the definition on every new release.
I understand why you decided to change the behavior. I don't agree to do the change in 7.x. But if you don't want to change it back it's ok. I can imagine that most people don't care or changed the implementation already.
jorgerod commentedon Jun 26, 2024
To clarify, I have not changed the behaviour. I in version 7.5.0 found that behaviour change and I have adapted to it (I have hundreds of apis...) because I think it is correct that if the nullable field is not defined, the collection must be initialized.
Also, correct me if I'm wrong, but for collections not to be initialized, there is the property
containerDefaultToNull. Isn't that enough?15 remaining items
jorgerod commentedon Jul 25, 2024
I think this is a very important change. It should be endorsed by a project maintainer (I just contribute from time to time and give my opinion).
@wing328 @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg
IMHO, it looks good.
martin-mfg commentedon Jul 25, 2024
Hi @jpfinne, I like your proposal! Also, thanks for preparing a PR already.
I am wondering if anyone needs different initialization behaviours for array vs set vs map? If so, please let us know here. Otherwise I'd propose to not make this distinction.
Since in the proposed changes the option is still named
containerDefaultToNull, it should not be deprecated. But the old valid values - "true" and "false" - should still work the same as before.Since
containerDefaultToNullis available only in the Java based generators, I thinkSET_CONTAINER_TO_NULLABLEshould not be deprecated. So other generators can still use that option.bodograumann commentedon Aug 8, 2024
I think your proposal is an excelent compromise. @jorgerod , @jpfinne . Thanks for moving the issue ahead.
In our use-case there is no need to differentiate between different container types, @martin-mfg .
Hopefully we can get the PR merged soon.
randyhbh commentedon Nov 11, 2024
Hi @martin-mfg, I noticed you received one approval for the PR addressing this issue. It’s likely to be merged soon? If there’s anything I can assist with, I’d be happy to help.
martin-mfg commentedon Nov 13, 2024
Hi, I am waiting for @wing328 to merge the PR. I think there's nothing else to be done.
andrey-fomin commentedon Dec 1, 2024
I believe there are conceptual issues with the current approach to handling default values.
Reference from Standards:
Current Issues:
Defaults During Field Initialization:
Inconsistent Implicit Defaults:
Suggestions for Improvement:
Avoid Implicit Defaults:
Remove
containerDefaultToNull:nullshould be used by default, removing the need forcontainerDefaultToNulland simplifying behavior.Apply Defaults Only During JSON Deserialization:
@JsonCreator. This ensures defaulting is limited to deserialization and doesn't impact serialization.@martin-mfg @jpfinne please can you check this comment?
wing328 commentedon Dec 2, 2024
Hi all,
Thanks for all the feedback, suggestions, PRs, etc.
I've been thinking about this more and more and I think we all agree ideally we need to solution that works in all generators instead of just Java-related generators.
I'll try to come up with something later this week or next so that you guys can try it out via a PR.
Best regards,
William
mrohlof-protofy commentedon Jan 10, 2025
Hey guys, what's the status on this issue?
frankjkelly commentedon Jan 30, 2025
Looking forward to resolution of this. Thank you!
mix4242 commentedon Apr 4, 2025
Has there been any progress or a decision made? I see this has gone a bit quiet 😿
kevinnammour commentedon May 11, 2025
Hi @wing328 , do you need some help in this? This is also blocking from our side, and there's no temporary workaround as we have thousands of specs, and many of them we don't own/we can't change. We have an openapi generator wrapper in which we'd like to rollback to the 7.4.0 behavior of initializing containers while using the 7.13.0 version.
wing328 commentedon May 13, 2025
sorry for the delay. too busy these days...
i just filed #21269 with instruction on how to test it
would be great it you test it with your use cases.
please reply to the PR with your test results.
thank you 🙏
wing328 commentedon May 26, 2025
Can others please help the PR #21269 when you've time?
wing328 commentedon May 27, 2025
Just merged #21269
Please give it a try with the snapshot version: https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/7.14.0-SNAPSHOT/
and reply to the PR if you find any issues