Open antonkomarev opened 5 years ago
@tobscure this code is working in 2 of my projects on production for a one year. It's not fully covered with tests, but I've added some major ones. I will be glad to have a feedback about it and ready for discussion about methods and class naming.
Implements #139
Problem
Sometimes resources require to have polymorphic relations.
Garage
has manyvehicles
in it. Vehicle could has it's own typeCar
orBike
.Garage
has oneowner
. Owner could be of typePerson
orOrganization
.Solution
Solution is pretty simple. We are providing
SerializerRegistry
instead of concreteSerializer
to polymorphic collection or resource and it will try to find serializer which mapped to this serializable object.Collections
VehicleSerializerRegistry
which will describe mappings between serializable object & serializer.GarageSerializer
usePolymorphicCollection
instead ofCollection
and passVehicleSerializerRegistry
to it.Resource
OwnerSerializerRegistry
which will describe mappings between serializable object & serializer.GarageSerializer
usePolymorphicResource
instead ofResource
and passOwnerSerializerRegistry
to it.In closing
Be careful with polymorphism, because there are some edge cases which were revealed in a discussion with Michael Hibay on discuss.