Skip to content

[Serializer] Add a MaxDepth handler #26108

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

Closed
wants to merge 3 commits into from

Conversation

dunglas
Copy link
Member

@dunglas 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be private, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

@nicolas-grekas nicolas-grekas Feb 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

($this->maxDepthHandler)($attributeValue)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callable|null

@lyrixx
Copy link
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 :-))

@fabpot
Copy link
Member

fabpot commented Feb 19, 2018

Thank you @dunglas.

@fabpot fabpot closed this in 3cb5619 Feb 19, 2018
@dunglas dunglas deleted the maxlength-handler branch February 19, 2018 20:48
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Mar 12, 2018
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 mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants