Skip to content

[Serializer] Allow to access to the format and context in circular ref handler #27020

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] Allow to access to the format and context in circular re…
…f handler
  • Loading branch information
dunglas committed Jul 3, 2018
commit 99f829ec2b59e4dafca3040a9150c48c42f01cfe
5 changes: 5 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* `AbstractNormalizer::handleCircularReference` is now final, and receives two optional extra arguments: the format and the context

4.1.0
-----

Expand Down
13 changes: 10 additions & 3 deletions src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,23 @@ protected function isCircularReference($object, &$context)
* If a circular reference handler is set, it will be called. Otherwise, a
* {@class CircularReferenceException} will be thrown.
*
* @param object $object
* @final since Symfony 4.2
*
* @param object $object
* @param string|null $format
* @param array $context
*
* @return mixed
*
* @throws CircularReferenceException
*/
protected function handleCircularReference($object)
protected function handleCircularReference($object/*, string $format = null, array $context = array()*/)
{
$format = \func_num_args() > 1 ? func_get_arg(1) : null;
$context = \func_num_args() > 2 ? func_get_arg(2) : array();

if ($this->circularReferenceHandler) {
return \call_user_func($this->circularReferenceHandler, $object);
return \call_user_func($this->circularReferenceHandler, $object, $format, $context);
}

throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', \get_class($object), $this->circularReferenceLimit));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public function normalize($object, $format = null, array $context = array())
}

if ($this->isCircularReference($object, $context)) {
return $this->handleCircularReference($object);
return $this->handleCircularReference($object, $format, $context);
}

$data = array();
$stack = array();
$attributes = $this->getAttributes($object, $format, $context);
$class = get_class($object);
$class = \get_class($object);
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;

foreach ($attributes as $attribute) {
Expand Down Expand Up @@ -154,7 +154,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
*/
protected function getAttributes($object, $format = null, array $context)
{
$class = get_class($object);
$class = \get_class($object);
$key = $class.'-'.$context['cache_key'];

if (isset($this->attributesCache[$key])) {
Expand Down Expand Up @@ -248,7 +248,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
$attribute = $this->nameConverter->denormalize($attribute);
}

if ((false !== $allowedAttributes && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
if ((false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
if (isset($context[self::ALLOW_EXTRA_ATTRIBUTES]) && !$context[self::ALLOW_EXTRA_ATTRIBUTES]) {
$extraAttributes[] = $attribute;
}
Expand Down Expand Up @@ -345,7 +345,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
return (float) $data;
}

if (call_user_func('is_'.$builtinType, $data)) {
if (\call_user_func('is_'.$builtinType, $data)) {
return $data;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,15 @@ public function testCircularReferenceHandler()

$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy');
$this->assertEquals($expected, $this->normalizer->normalize($obj));

$this->normalizer->setCircularReferenceHandler(function ($obj, string $format, array $context) {
$this->assertInstanceOf(CircularReferenceDummy::class, $obj);
$this->assertSame('test', $format);
$this->arrayHasKey('foo', $context);

return \get_class($obj);
});
$this->assertEquals($expected, $this->normalizer->normalize($obj, 'test', array('foo' => 'bar')));
}

public function testDenormalizeNonExistingAttribute()
Expand Down