Skip to content
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

Merged
merged 21 commits into from
Dec 1, 2020
Merged

Conversation

SychO9
Copy link
Member

@SychO9 SychO9 commented Nov 5, 2020

Part A of #1624
Part of #1891

Changes proposed in this pull request:

  • Deprecate the Serializing event.
  • Deprecate the GetApiRelationshipEvent event.
  • Add attributes extenders.
  • Add relationships extenders.
  • Add Tests for all the new extenders.

Reviewers should focus on:

  • Are the tests on point.

Confirmed

  • Backend changes: tests are green (run composer test).

Bundled extensions to update:

@SychO9 SychO9 changed the title WIP: Add api serializer attributes extender & tests WIP: ApiSerializer Extender Nov 5, 2020
@SychO9
Copy link
Member Author

SychO9 commented Nov 5, 2020

I think I'll split this into two PRs, one for adding the ApiSerializer Extender (this one) and one for the ApiController Extender.

@SychO9 SychO9 force-pushed the sm/api-serializer-extender branch 2 times, most recently from 3e3850b to 22a73d8 Compare November 8, 2020 15:05
@SychO9 SychO9 changed the title WIP: ApiSerializer Extender ApiSerializer Extender Nov 8, 2020
@SychO9 SychO9 marked this pull request as ready for review November 8, 2020 16:46
src/Api/Serializer/AbstractSerializer.php Outdated Show resolved Hide resolved
src/Extend/ApiSerializer.php Outdated Show resolved Hide resolved
src/Extend/ApiSerializer.php Outdated Show resolved Hide resolved
src/Extend/ApiSerializer.php Outdated Show resolved Hide resolved
tests/integration/extenders/ApiSerializerTest.php Outdated Show resolved Hide resolved
src/Extend/ApiSerializer.php Show resolved Hide resolved
Copy link
Member

@clarkwinkelmann clarkwinkelmann left a 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...

@SychO9
Copy link
Member Author

SychO9 commented Nov 16, 2020

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 $serializer->getActor() just like in the event listeners.

@askvortsov1
Copy link
Sponsor Member

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

@clarkwinkelmann
Copy link
Member

seems like you'd still be able to do $serializer->getActor() just like in the event listeners.

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.

are there arguments you're expecting to need to pass in, in the near future?

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.

@SychO9
Copy link
Member Author

SychO9 commented Nov 16, 2020

Ah I see, that's a good point yeah 🤔

@askvortsov1
Copy link
Sponsor Member

We'll merge this a bit later in case @franzliedke has any comments / suggestions

Copy link
Member Author

@SychO9 SychO9 left a 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/Api/Serializer/AbstractSerializer.php Outdated Show resolved Hide resolved
foreach ($this->attributes as $attributeName => $callback) {
$callback = ContainerUtil::wrapCallback($callback, $container);

$attributes[$attributeName] = $callback($attributes, $serializer, $model);
Copy link
Member Author

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 ?

Copy link
Sponsor Member

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
@askvortsov1 askvortsov1 merged commit 8c813bc into flarum:master Dec 1, 2020
@SychO9 SychO9 deleted the sm/api-serializer-extender branch December 1, 2020 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants