-
Notifications
You must be signed in to change notification settings - Fork 119
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
[FEATURE] copyWith extension methods that allow setting a field to null #438
Comments
@vipw try new version :) |
|
@nguyenloki258 could you please share your swagger file? Because my 50 swaggers has been generated well |
I get the same issue as @nguyenloki258. The generated code compiles, but since both extension classes have a method with the same name, the compiler can't infer which An easy solution is to change the Here it is in a PR: |
There's one other thing that comes to my mind about this feature. Do you think it's good to allow setting required fields to null? Maybe there should only be one copyWith method that takes raw types for required members and Wrapped for the optional members. That would correspond more closely to the swagger file definition, but it would break backward compatibility and make code using the generated class more verbose. I'm curious what you think about it. |
Good evening @vipw! I think it will make models more flexible. Because who knows how swagger file is configures. That's correct your PR fix this issue? |
Hello! I'm receiving the same issue as @nguyenloki258 The issue is that I have multiple swagger files, which each create two different versions of the Later, I have a barrel file that exports the generated models, and there's a conflict because both model files have their own As a stopgap fix, I downgraded my version of the package. Technically, you can also work around it by doing Perhaps a better solution would be to copy how freezed does It keeps a private placeholder object, and |
Hi @ko16a46 , good idea. Previously, we copy-pasted |
Getting the same error:
For anyone looking for a workaround, this solves it for me. Wrap your API response in one of the extension classes like so, and then use copyWith:
|
@dJani97 that would be really helpfull if you share you swagger file |
@Vovanella95 it's not an open-source project, can I share it with you without making it public? |
I'm sending it by email |
Is your feature request related to a problem? Please describe.
I have an API that allows unsetting a value by POSTing a JSON document with a field set to NULL.
For example a person's age can be unset with by POSTing a Person object to /person/personId
The autogenerated Person class has
This is usually very helpful but doesn't allow setting either field to null.
This is a simplified example, I actually have dozens of entities, each with many members. Otherwise I would just do the obvious thing:
Describe the solution you'd like
I'm not sure it's the best solution, but I built my own extension method based on this post: https://stackoverflow.com/a/71732563/759140
It seems bloated to make copyWith and copyWrapped methods. Maybe it could be a configuration flag whether the copyWith uses wrapping or not.
I also had a thought to add a copyButNullSomeFields method that would take bools, and null the specified fields:
But I haven't tried this one out yet. It might be a better for autogenerated code because it would be easy to chain it with copyWith().
Swagger specification link
N/A
The text was updated successfully, but these errors were encountered: