-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
/** | ||
* @var callable | ||
*/ | ||
protected $maxDepthHandler; |
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.
should be private, isn't it?
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.
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.
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.
If it can be private, it should be. "Consistency" shouldn't win over ease of maintenance.
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.
$classDiscriminatorResolver
is also protected (it has been introduced only in 4.1), should we also change its visibility?
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.
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); |
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.
($this->maxDepthHandler)($attributeValue)
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.
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 |
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.
callable|null
9ad7f5b
to
9abb93b
Compare
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 :-)) |
ca13873
to
37271fb
Compare
37271fb
to
53e6edf
Compare
Thank you @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
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).