-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG][JAVA] optional collection fields are initialized since 7.5.0 #18735
Comments
@mosesonline 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. 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!) It seems people agree to disagree. |
@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. |
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. |
@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). |
Sorry, for being silent. I would be happy for any help to fix theis issue. |
check required collections are not null update samples and tests
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. |
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 What do you think? |
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 |
Before version 7.5.0 the collections are not initialized and with they are. So, the behavior, or the result, is an other. I admit, I don't know when the old behavior was introduced, but it changed from 7.4 to 7.5. Even if it fixes to a more standard compliant behavior it's changing it. Unfortunately the containerDefaultToNull sets all containers to null also that containers that where initialized before 7.5.0. I have not found any configuration in the generator that doesn't force me to change code and/or api. And yes, for me it's only one API I cannot change myself not hundreds or thousands... If the consensus is to leave it like it behaves now. I'm totally fine. |
Yeah, I totally understand. Maybe the PR should be focused on the fact that if the property What do you think? |
Is there an important distinction between required = false and nullable = true? As context, I'm coming to this bug because of the impact on Kubernetes clients. For instance, this field is optional in the schema of a Pod, but with the earlier change clients are now initializing the field to an empty container which results in the transmission of I'm very happy if we are able to find a solution that resolves this problem while also not breaking @jorgerod's use case; however, I suspect it won't be reasonable to get all fields that are using required to align nullable. |
Hi @rjeberhard @mosesonline @martin-mfg @wing328 The behaviour to be followed does not have to be the one that each one creates or is convenient, but the one that the OpenApi3 specification says. And the specification is very clear: by default nullable is false (see here and here )
If by default nullable is false, to ensure that the specification is met the collections must be initialized. On the other hand, in Openapi Generator there is the I add this matrix to make it clearer: # ...
components:
schemas:
User:
type: object
properties:
phones:
type: array
items:
type: string
# ...
|
I think @jorgerod explained the alternatives. Facts:
Interpretation: Making the change at (1) in a MINOR version seemed wrong. I agree. It broke some of our clients but it's an opportunity to better define our API specs. But once accepted this breaking change in the OpenAPI generator release and released, I think reverting it (remember that it's the right thing to do) it's completely nonsense. What is done, it's done. Conclusion: Change my mind |
Hi everyone, Here's a comparison of the different behaviours:
✅ = container is initialized OpenAPI Generator 7.6.0 has the same behaviour as 7.5.0. Several versions before 7.4.0 have the same behaviour as 7.4.0. I think most people who face problems with the switch from 7.4.0 to 7.5.0 are affected only by the change for the case "not required, nullable not specified". If that's the case, you can use the new [...]
<generatorName>java</generatorName>
<openapiNormalizer>SET_CONTAINER_TO_NULLABLE=array|set|map</openapiNormalizer>
[...] But if you need to restore the 7.4.0 behaviour for all rows in the table, there is currently no simple option to do this. I think it would make sense to introduce an optional setting to fully restore the 7.4.0 behaviour, and it would be great if someone could create a PR for this. (I would not advise to change the behaviour of the existing option Do you have any additional thoughts, @wing328? |
Chiming in here, the change to initialize collections to an empty collection broke multiple tests for one of our projects. Admittedly, some specs are woefully terrible at my company, but my team has no control over another's teams' specification. So for some, we disable schema validation to generate the models/POJOs to consume their API from our Spring Boot applications. For our use case specifically, we have the code generation in a separate dedicated project which is published as a Java library internally. The project has a plethora of integration tests with WireMock to mock the actual backend for the specifications. We upgraded the Gradle plugin from 7.3.0 to 7.7.0 and observed the failures. After some investigation, I came across various issues and finally landing on this one. Configuring |
@ciscoo the discussion here is about correctness, not resource consumption. Anyway an empty collection should not consume so many memory resources. |
👋 Chiming in here as another consumer who was broken by this change. We had an optional property with schema The change might be a correct reading of the OAS spec (not sure, although it seems wrong to me), but... in the future could we ensure that changes like that go into new major versions of the generator? Breaking changes without a major version are unpredictable and confusing to consume. |
@tomdeering-wf You are right, IMHO OpenAPI generator project has a history of introducing breaking changes in minor versions. This is wrong and needs to be improved. Breaking changes should be introduced in major versions only. But once made the wrongdoing (breaking change in a minor version), considering that the change is a fix, reverting it would do more harm than good, in my opinion. |
I disagree with this interpretation and point 2 in particular. I think several things are being mixed up here. In my understanding both
So these are two completely separate conditions. Most importantly, a patch-semantic of "update all fields that are set, while skipping missing fields", can now unknowingly delete all that data that should have been left alone. This is a very dangerous breaking change! Luckily in our case it was caught in a test early enough. Another problem with the current solution, is that the current intepretation of a missing field is not stable. As discussed above, So I think the behaviour should be reverted to how it was in 7.4 asap. |
It seems there is no consensus about the correct behaviour. Instead of I just can't find a good syntax for the expression used in The maxtrix with 6 rows can be used to specify exactly the generated code. Using the row number (from 1 to 6): To replicate the 7.5.0 behaviour: containerDefaultToNull=24 (2 meaning second row, i.e. not required, nullable and 4 meaning 4th row i.e. not required nullable not specified. This is really precise but very obscure. So a better syntax is needed. Using exclamation mark for not, question mark for not specified, pipe for To specify the container type: Shortcuts for the expression could be added: Implementation wise: |
Hi @wing328 @mosesonline @bodograumann @martin-mfg @juliojgd @jpfinne I think this is a good option. Right now there is a solution that is not complete and is quite controversial. What would seem important to me is that this change comes after a full check and approval by the openapi-generator maintainers. Also, ideally, this is a behaviour that should apply to all languages. |
@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. |
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 Since |
I think your proposal is an excelent compromise. @jorgerod , @jpfinne . Thanks for moving the issue ahead. |
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. |
I believe there are conceptual issues with the current approach to handling default values. Reference from Standards:
Current Issues:
Suggestions for Improvement:
@martin-mfg @jpfinne please can you check this comment? |
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, |
Bug Report Checklist
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.
The text was updated successfully, but these errors were encountered: