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

[Serializer] Add a MaxDepth handler #26108

Closed
wants to merge 3 commits into
base: master
from

Conversation

@dunglas
Member

dunglas commented Feb 9, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets api-platform/core#1130, api-platform/core#1528, api-platform/core#1528
License MIT
Doc PR todo

Sometimes, instead of just stopping the serialization process when the configured max depth is reached, it can be interesting to let the user return something (like returning the identifier of the entity to stop manually the serialization process).

This PR also makes the max depth handling more similar to circular references handling (that already has the possibility to set a handler).

/**
* @var callable
*/
protected $maxDepthHandler;

This comment has been minimized.

@nicolas-grekas

nicolas-grekas Feb 11, 2018

Member

should be private, isn't it?

This comment has been minimized.

@dunglas

dunglas Feb 11, 2018

Member

The circular reference handler is protected, so it can be set in a subclass. I've made this one protected too for the sake of consistency.

This comment has been minimized.

@nicolas-grekas

nicolas-grekas Feb 11, 2018

Member

If it can be private, it should be. "Consistency" shouldn't win over ease of maintenance.

This comment has been minimized.

@dunglas

dunglas Feb 11, 2018

Member

$classDiscriminatorResolver is also protected (it has been introduced only in 4.1), should we also change its visibility?

This comment has been minimized.

@nicolas-grekas

nicolas-grekas Feb 11, 2018

Member

yes please (same PR to reduce process overhead if possible :) )

continue;
}
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
if ($maxDepthReached) {
$attributeValue = call_user_func($this->maxDepthHandler, $attributeValue);

This comment has been minimized.

@nicolas-grekas

nicolas-grekas Feb 11, 2018

Member

($this->maxDepthHandler)($attributeValue)

This comment has been minimized.

@nicolas-grekas

nicolas-grekas Feb 11, 2018

Member

or \call_user_func($this->maxDepthHandler, $attributeValue);
in order to remove one noisy frame in stack traces

@@ -41,6 +41,11 @@
private $attributesCache = array();
private $cache = array();
/**
* @var callable

This comment has been minimized.

@sroze

sroze Feb 11, 2018

Member

callable|null

@lyrixx

This comment has been minimized.

Member

lyrixx commented Feb 16, 2018

Could you also add a configuration entry to configure it like I have done with the circular reference handler (IIRC, I'm on my phone ATM :-))

dunglas added some commits Feb 9, 2018

@lyrixx

lyrixx approved these changes Feb 19, 2018

@fabpot

fabpot approved these changes Feb 19, 2018

@fabpot

This comment has been minimized.

Member

fabpot commented Feb 19, 2018

Thank you @dunglas.

@fabpot fabpot closed this in 3cb5619 Feb 19, 2018

@dunglas dunglas deleted the dunglas:maxlength-handler branch Feb 19, 2018

javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Mar 12, 2018

minor #9427 [Serializer] Max depth handler support (dunglas)
This PR was merged into the master branch.

Discussion
----------

[Serializer] Max depth handler support

Documents symfony/symfony#26108. Closes #9317 and #9229.

Commits
-------

ef46c8c [Serializer] Max depth handler support

@fabpot fabpot referenced this pull request May 7, 2018

Merged

Release v4.1.0-BETA1 #27181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment