You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
Hello, guys! I'm thinking about possibility to add Polymorphic Serializers in my application.
Preface
Imagine situation when you have activity resource with actor relationship which polymorphic and could be represented as user or org. Additionally activity resource has polymorphic collection subjects which could be represented as article or comment resources alltogether in one collection.
I've chosen only 2 resource types in each relationship only for the sake of example simplicity.
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.
instantiates a lot of classes simultaneously even if they wouldn't be in use;
not reusable.
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.
$element = newCollection($activity->subjects, [
Article::class => function () use ($request) {
returnnewArticleSerializer($request),
},
Comment::class => function () use ($request) {
returnnewCommentSerializer($request),
},
]);
Benefits:
not so greedy as solution №1.
Drawbacks:
clumsy code (IMHO);
not reusable.
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 to resolveSerializer based on $model class.
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 inside SubjectSerializerRegistry.
If each model have only one serializer inside an application - this registry could include all possible models and their serializers.
Benefits:
reusable.
Drawbacks:
more classes;
more complicated logic if serializers has complex instantiation process.
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?
The text was updated successfully, but these errors were encountered:
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?
The text was updated successfully, but these errors were encountered: