-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer][PropertyAccess] Support immutable objects #19291
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
Comments
Looks like your link got somehow broken, here it is once again: On the core subject: I personally think that Something like GeneratedHydrator would fit here, but what the |
Fixed
I couple of thoughts on the fly: There is two problems at hand: instantiating the object and populating it. And in this part (
I'm not a big fan to "hack" things to set private values. Not that it's not a valid use-case but it can create some unexpected issues as you are setting your model state in a non conventional way. It's a scenario where I would rather see developers use custom serializers or if they are fine with this behaviour, enrich the serializer behaviour in their code base (i.e. not providing that in the core).
I may have got the wrong idea on what the PropertyAccess is about and someone more knowledgeable may correct me, but to me it looks like PropertyAccess simply has two facets:
For both, part of that process is figuring how to access/write the value which includes checking if the property is public or not, if there's a getter, is using camelcase or snake case and so on.
100% agree with that. It is a convention like another which looks like widly adopted (I have no data to back it up, it is only from my personal experience) and I believe that if there is another popular convention, it should be supported as well (provided it's worth it). So for those reasons I don't feel like it's misusing the PropertyAccess component :) |
@theofidry, on second thought, the |
@kix I would say it still is. You are write, "wither" and so are actually non-static factories. However because they are non-static, they require an instantiated factory which is the instance itself. To me this would still fall within the responsibility of an hydrator: you have an object and properties to populate, and you and a populated object. That the populated object is the same or not than the input one is nothing but a detail. As said I wouldn't mind having it as a separate component or outside of Symfony, but the issue is that it relies on private methods of |
As discussed in off, there is not need for that as it can be solved by ensuring the object is properly populated when instantiating it. The only edge case that would not work would be if a property should be set with a |
The Serializer is amazing at transforming a data structure into another and
ObjectNormalizer
does a great job at figuring out how to denormalize and object. It avoids to have to rely on a lot of custom normalizers (unless performances are critical).However it does not support immutable objects which is a bit sad as it forces user to create custom normalizers each time. Or rather to be clear, it does not support "withers", i.e. methods returning a new modified instance:
As of now, the Serializer is using the property accessor:
There would be two ways to do it:
PropertyAccess
About 1) it's a bit tricky because when using
setValue(&$object, 'prop', 'val)
, you would expect$object
being modified, not having a new modified instance. However I would think it's still valid because you are passing$object
reference, meaning what you want to set is the variable value and that you don't really care if the object is immutable or not.I'm ready to work on it, but needs to hear people opinion on the topic :)
/cc @webmozart @dunglas
The text was updated successfully, but these errors were encountered: