Description
The serializer has grown a lot especially since the 2.7. However it suffers (IMO) from one problem: its whole design is based on inheritance:
AbstractNormalizer
AbstractObjectNormalizer
ObjectNormalizer
GetSetMethodNormalizer
AbstractNormalizer
has a lot about circular references handling and how to instantiate objectsAbstractObjectNormalizer
extendsAbstractNormalizer
to specify how to normalize/denormalize objectsObjectNormalizer
implements how to extract attributes (via reflection) from an object and delegates the read/write attributes to the property accessor
Now this is working ok as it is. However, there is a big assumption here: the objects you are handling are POPO. In the case of Eloquent ORM for example, the entities are very different beast which requires their own Serializer and PropertyAccessor. As you can see in the repository, there is not that much required to get it works. But you loose one thing: all the children serializer. Indeed if you relied on ObjectNormalizer
in your library for all your serializers (like done in ApiPlatform for example), you cannot just switch of base serializer like that very easily. Another implication is that with the current design, it's very encouraged to extend serializers, where sometimes you could just decorate it to make use of composition instead.
The Serializer have seen itself a lot of features added via inheritance and IMO this is causing problem. It feels like it was quite simple at first, and one feature after another has been added but still with the mindset of "keeping things simple", having them a bit hidden. So instead of introducing new interfaces to provide new extension points, everything is done with inheritance and protected
. It is a bit limiting and complexify a lot the serializer.
Now the Serializer works well right now, so this whole post could be dismissed as "it's ok, it could have been designed better but it's a lot of work especially because of the BC promise". But given the rapid growth of the Serializer, it would also be fair to take a step back and reconsider deeper changes too (I am not really talking in terms of adding new features, but rather about how the implementation of the extension points).
Note: edited a bit the post to reflect on the discussion below.