| Q | A
| ---------------- | -----
| Bug report? | no
| Feature request? | yes
| BC Break report? | no
| RFC? | no
| Symfony version | 3.3.0
It would be really helpful if there was a way to recursively deseriailize objects.
Given an object like this:
class User
{
/**
* Set Name.
*
* @param Name $name
*/
public function setName(Name $name) : self
{
$this->name = $name;
return $this;
}
}
The serializer throws a Symfony\Component\Serializer\Exception\UnexpectedValueException
with the message "Expected argument of type 'Name', 'array' given" because it passes an array into setName()
rather than running the denormilization on the Name
class and then passing the result into setName()
. I think having this feature would be really helpful. I could of course allow this method to accept Name and array, but then it will bypass:
1) Populating the existing name object
2) Any groups that may be used for access.
Since the method already has a typehint of the only thing it will accept (a class) I think it makes sense that if a non-scalar type hint is encountered in a setter, it should:
1) Run the denormalizer with the type hinted class as the type.
2) Pass the same groups from the parent to the child
3) Attempt to get the value of the property, if a value is returned, it should use that value in object_to_populate
(assuming object_to_populate
was set on the parent)
Well this wasn't immediately obvious, but I found this:
https://github.com/symfony/symfony/blob/3.2/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml#L25
which lead me to install the ProperyInfo compoment and enable it in my config.yml
:
framework:
property_info:
enabled: true
This _mostly_ fixes my problem(s). It does support recursion, and it does support groups, however, it does not support object_to_populate
. I'm going to change this issue to be about that rather than recursion itself which Symfony clearly supports (albeit poorly documented).
There is a lack in the documentation about how you need to activate PropertyInfo in order to have embedded object deserialized, I've already posted an issue about that: https://github.com/symfony/symfony-docs/issues/7387
About the third problem (now only problem), object_to_populate is used to update/use an already existing object instead of creating a new one. You say that if the parent use this option, the embedded objects should used it too. I can see the need to use an existing object as value of a property, but:
@davidbarratt's comment helped me figure-out that I needed to install the PropertyInfoComponent
and add the framework.property_info.enabled
config for the Serializer to be able to deserialize objects with \DateTime
properties. This is not necessary for the serialization and I couldn't find anything in the doc that mentions it.
Without this, serializing an object with a \DateTime
property works, but deserializing it just afterwards fails. It (serializing _and deserializing_ objects with \DateTime
properties) seems like a likely scenario so I would propose mentionning it in the documentation to save other developers the frustration and frantic googling.
Most helpful comment
Well this wasn't immediately obvious, but I found this:
https://github.com/symfony/symfony/blob/3.2/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml#L25
which lead me to install the ProperyInfo compoment and enable it in my
config.yml
:This _mostly_ fixes my problem(s). It does support recursion, and it does support groups, however, it does not support
object_to_populate
. I'm going to change this issue to be about that rather than recursion itself which Symfony clearly supports (albeit poorly documented).