Open antonkomarev opened 6 years ago
I successfully created a Serializer class for all my models: https://github.com/piotrgolasz/koseven/blob/skanstull/modules/skanstull/classes/ModelSerializer.php
Thanks for sharing, @piotrgolasz. But it's a bit different. Because you have only one model type. And I have serializer for each endpoint, which completely describes how it should be represented in API.
Keep in mind that in your implementation once you change model fields - your API will be changed too. Your resource is tightly coupled with the model. This is not bad for private APIs which are fully under your own control and you are free to change them at your will and no client issues will be raised in this case. But if you are building public API, then such changes will lead you to issues on client side, because each change of the fields will require all clients to be updated as well.
By the way, I've implemented polymorphism in my project. I want to test it in production in next couple of months. I'll make a PR with this changes if everything will work as expected.
Hello, guys! I'm thinking about possibility to add Polymorphic Serializers in my application.
Preface
Imagine situation when you have
activity
resource withactor
relationship which polymorphic and could be represented asuser
ororg
. Additionallyactivity
resource has polymorphic collectionsubjects
which could be represented asarticle
orcomment
resources alltogether in one collection.I've chosen only 2 resource types in each relationship only for the sake of example simplicity.
Hadcode Solution
It's not great way, but it works... for resource, but not for
subjects
polymorphic collection. You don't have a way to analyze resources inside collection on the fly and choose what serializer should be used for the concrete resource.Possible Solutions
1. Serializers Array
Benefits:
Drawbacks:
2. Serializers Array with Closures
This point fixes 2nd one by using closures, but code becomes clumsy, especially if you need to pass some data to instantiate serializers.
Benefits:
Drawbacks:
3. Serializer Registry Class
Create some kind of serializer registry (let's say
SubjectSerializerRegistry
) which will be called for each resource while iterating over the collection toresolveSerializer
based on$model
class.SubjectSerializerRegistry
could looks like something similar to this:AbstractSerializerRegistry
could be pretty easy too, but there could be issues with serializers instantiation. I don't have cases when different serializers has different arguments in constructors, but if there will be need of it - code will be bigger and will require each serializer instantiation process described in separate methods insideSubjectSerializerRegistry
.If each model have only one serializer inside an application - this registry could include all possible models and their serializers.
Benefits:
Drawbacks:
Conclusion
These are first sketches. I want to think about this issue a bit more in nearest future... but first of all it would be great to receive a feedback before starting PR implementation. Personally I like 3rd solution most of all.
What do you think about this requirement and possible ways to solve it?