-
-
Notifications
You must be signed in to change notification settings - Fork 826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ApiSerializer Extender #2438
ApiSerializer Extender #2438
Conversation
54b2cd3
to
38d34ed
Compare
I think I'll split this into two PRs, one for adding the ApiSerializer Extender (this one) and one for the ApiController Extender. |
3e3850b
to
22a73d8
Compare
a79d7e6
to
5bf9402
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just looking at the code looks good.
I just find it annoying sometimes to not have access to the actor in some contexts, but that was already from before this PR.
My only worry is that by using callbacks instead of event class, it makes it harder to add properties like $actor
or $request
later on without introducing a bunch of breaking changes...
wouldn't you still be able to do that if you used invokable classes ? EDIT: nvm, I don't think I fully understand what the difference is between using an event class and a callable when it comes to using the actor, seems like you'd still be able to do |
I think Clark is referring to the old approach of bundling all properties passed to the callable under a single POPO event object. That's a fair point, but are there arguments you're expecting to need to pass in, in the near future? The way I see it, the only thing that could prompt a change like that is if we switched to https://github.com/tobyzerner/json-api-server, but that's a long way away and would likely break other things too |
True, those additional parameters could be accessed from objects that are passed to the callback sometimes, so there wouldn't be an issue with adding more parameters to the callback here.
Except from actor/request for some contexts where none of the arguments allow to access them, I'm not seeing anything particular. I'm just seeing this as a potential issue for the future. Adding properties to an event class is easy, removing one only affects those that were using it. Removing properties from a callback will break every implementation that uses one of the arguments that follows. Luckily PHP allows callback to accept less arguments than passed. Otherwise this would be a guaranteed breaking change anytime we add a new parameter, even if nobody wanted to use it. |
Ah I see, that's a good point yeah 🤔 |
We'll merge this a bit later in case @franzliedke has any comments / suggestions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a couple of things I thought about while updating bundled extensions.
src/Extend/ApiSerializer.php
Outdated
foreach ($this->attributes as $attributeName => $callback) { | ||
$callback = ContainerUtil::wrapCallback($callback, $container); | ||
|
||
$attributes[$attributeName] = $callback($attributes, $serializer, $model); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that most of the time when adding a single attribute through the attribute
method of the extender, the first parameter being $attributes is rarely the one needed, so it's useless most of the time, do we want to push it to the end, or would we rather keep it consistent with the callback signature of the mutate
method ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think consistency is better here; that being said, we could push it to the end for both mutate
and attribute
.
- serializer - model - attributes
Part A of #1624
Part of #1891
Changes proposed in this pull request:
Serializing
event.GetApiRelationshipEvent
event.Reviewers should focus on:
Confirmed
composer test
).Bundled extensions to update: