Skip to content

[BUG][JAVA] optional collection fields are initialized since 7.5.0 #18735

Closed

Description

@mosesonline

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)

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?

Regards

openapi-generator version

7.5.0+

OpenAPI declaration file content or url

f360c69#diff-4b50afe9f2863efdbde293ab01bf3775b7e99a32d330260569cb2c63db4d7f59

Generation Details

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.

Steps to reproduce

Run the test in the fork and branch https://github.com/mosesonline/openapi-generator/tree/required-collection-issue.
You can see the behavior before in the other branch https://github.com/mosesonline/openapi-generator/tree/required-collection-issue-on74 I implemented the same test there and it is green.

Related issues/PRs

Maybe the new behavior was introduced with #18104?

PR: #18738

Suggest a fix

If it's an issue and not expected behavior, I can work on a fix.

Activity

  1. added a commit that references this issue on May 22, 2024
  2. jpfinne commented on May 22, 2024

    @jpfinne
    Contributor

    @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.

  3. rjeberhard commented on Jun 12, 2024

    @rjeberhard

    @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.

  4. jpfinne commented on Jun 12, 2024

    @jpfinne
    Contributor

    @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.

  5. rjeberhard commented on Jun 12, 2024

    @rjeberhard

    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.

  6. wing328 commented on Jun 12, 2024

    @wing328
    Member

    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).

  7. mosesonline commented on Jun 17, 2024

    @mosesonline
    ContributorAuthor

    Sorry, for being silent. I would be happy for any help to fix theis issue.

  8. added 2 commits that reference this issue on Jun 17, 2024
  9. jorgerod commented on Jun 26, 2024

    @jorgerod
    Contributor

    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.

  10. mosesonline commented on Jun 26, 2024

    @mosesonline
    ContributorAuthor

    The initial change to initialize the collections was also a breaking change. It broke our code.

  11. jorgerod commented on Jun 26, 2024

    @jorgerod
    Contributor

    The initial change to initialize the collections was also a breaking change. It broke our code.

    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?

  12. mosesonline commented on Jun 26, 2024

    @mosesonline
    ContributorAuthor

    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.

  13. jorgerod commented on Jun 26, 2024

    @jorgerod
    Contributor

    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?

  14. 15 remaining items

  15. jorgerod commented on Jul 25, 2024

    @jorgerod
    Contributor

    @jorgerod does my PR fit your needs?

    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.

  16. martin-mfg commented on Jul 25, 2024

    @martin-mfg
    Contributor

    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.

  17. bodograumann commented on Aug 8, 2024

    @bodograumann
    Contributor

    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.

  18. randyhbh commented on Nov 11, 2024

    @randyhbh

    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.

  19. martin-mfg commented on Nov 13, 2024

    @martin-mfg
    Contributor

    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.

  20. andrey-fomin commented on Dec 1, 2024

    @andrey-fomin

    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:

    1. 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.
    2. 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:

    1. Avoid Implicit Defaults:

      • Ensure consistency by explicitly defining all defaults, with no implicit defaults for any type.
    2. Remove containerDefaultToNull:

      • If no explicit default is defined, null should be used by default, removing the need for containerDefaultToNull and simplifying behavior.
    3. 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.

    @martin-mfg @jpfinne please can you check this comment?

  21. wing328 commented on Dec 2, 2024

    @wing328
    Member

    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

  22. mrohlof-protofy commented on Jan 10, 2025

    @mrohlof-protofy

    Hey guys, what's the status on this issue?

  23. frankjkelly commented on Jan 30, 2025

    @frankjkelly

    Looking forward to resolution of this. Thank you!

  24. mix4242 commented on Apr 4, 2025

    @mix4242

    Has there been any progress or a decision made? I see this has gone a bit quiet 😿

  25. kevinnammour commented on May 11, 2025

    @kevinnammour

    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

    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.

  26. wing328 commented on May 13, 2025

    @wing328
    Member

    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 🙏

  27. wing328 commented on May 26, 2025

    @wing328
    Member

    Can others please help the PR #21269 when you've time?

  28. wing328 commented on May 27, 2025

    @wing328
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions