serializer
serializer copied to clipboard
Serialize using interface metadata
Q | A |
---|---|
Bug report? | IDK |
Feature request? | IDK |
BC Break report? | no |
RFC? | IDK |
I don't know if this is a missing feature or a bug.
I'm trying to define serialize metadata through interface implementation, passing the interface fqcn as the third parameter of ArrayTransformerInterface::toArray.
I have an interface PersonInterface, declaring two virtual properties (id and rating). I have another interface called UserProfileInterface extending PersonInterface and declaring one virtual property (name). I have an entity User implementing UserProfileInterface and declaring a virtual property (age).
When calling ArrayTransformerInterface::toArray, giving an instance of User, null as context and UserProfileInterface::class as type, the output contains only virtual property age, all declared in interfaces are missing
Ok, found out the piece of code blocking usage of interface metadata only. In SerializationGraphNavigator line 180, if the data implements the interface, the considered type is the class of the data. So I tried removing "implements UserProfileInterface" from User class. I've got virtual property name from UserProfileInterface but nothing from PersonInterface (should I open another issue for this case ?) Now issue is that serializer shouldn't change processed type when type is passed on invocation (or we should have some attribute on context to define if we want to force the type)
Ok, found out why parent interface of UserProfileInterface (PersonInterface) is ignored, it's related to "includeInterfaces". Could we explain why it is set to false ?
Thanks for reporting the issue, but jms/serializer metadata are per class. There is no per interface metadata.
Would this feature break some things ? Because almost all code is ready for this behaviour. As I reported, giving possibility to set includeInterfaces to true (that I should ask for in the bundle repository) and giving possibility to strictly keep the type (in fact if the provided type is a class extended by the data, the swap is done too), all the magic works.
Serializat
Serialization might work, but if you specify as @Type
something that is an interface, there is no way to unserialize it