serializer icon indicating copy to clipboard operation
serializer copied to clipboard

Serialize using interface metadata

Open DGCarramona opened this issue 4 years ago • 6 comments

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

DGCarramona avatar Mar 12 '20 20:03 DGCarramona

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)

DGCarramona avatar Mar 12 '20 21:03 DGCarramona

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 ?

DGCarramona avatar Mar 12 '20 21:03 DGCarramona

Thanks for reporting the issue, but jms/serializer metadata are per class. There is no per interface metadata.

goetas avatar Mar 14 '20 19:03 goetas

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.

DGCarramona avatar Mar 15 '20 02:03 DGCarramona

Serializat

goetas avatar Mar 24 '20 07:03 goetas

Serialization might work, but if you specify as @Type something that is an interface, there is no way to unserialize it

goetas avatar Mar 24 '20 07:03 goetas