Skip to content

Commit 2f33e6e

Browse files
bug #46297 [Serializer] Fix JsonSerializableNormalizer ignores circular reference handler in $context (BreyndotEchse)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Serializer] Fix JsonSerializableNormalizer ignores circular reference handler in $context | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT **How to reproduce this bug:** Normalize an object using the default "_serializer_" service (will use "_serializer.normalizer.object_" normalizer / _ObjectNormalizer_) ```php $target = new class { public function getMe() { return $this; } }; $targetData = $serializer->normalize($target, null, [ AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => static fn($v) => is_object($v) ? get_class($v) : gettype($v) ]); // Normalized data as expected: // $targetData === ["me" => "class@anonymous\x00 ...."]; ``` Normalize an object implementing \JsonSerializable using the default "_serializer_" service (will use "_serializer.normalizer.json_serializable_" normalizer / JsonSerializableNormalizer) ```php $target = new class implements \JsonSerializable { public function jsonSerialize() { return ['me' => $this]; } }; $targetData = $serializer->normalize($target, null, [ AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => static fn($v) => is_object($v) ? get_class($v) : gettype($v) ]); // Same result expected as above, but an exception will be thrown instead: // Symfony\Component\Serializer\Exception\CircularReferenceException("A circular reference has been detected when serializing the object of class "JsonSerializable@anonymous" (configured limit: 1).") ``` Commits ------- 793a264 [Serializer] Fix JsonSerializableNormalizer ignores circular reference handler in $context
2 parents 0437e3a + 793a264 commit 2f33e6e

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/Symfony/Component/Serializer/Normalizer/JsonSerializableNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class JsonSerializableNormalizer extends AbstractNormalizer
2727
public function normalize($object, $format = null, array $context = [])
2828
{
2929
if ($this->isCircularReference($object, $context)) {
30-
return $this->handleCircularReference($object);
30+
return $this->handleCircularReference($object, $format, $context);
3131
}
3232

3333
if (!$object instanceof \JsonSerializable) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
/**
15+
* @author Marvin Feldmann <breyndot.echse@gmail.com>
16+
*/
17+
class JsonSerializableCircularReferenceDummy implements \JsonSerializable
18+
{
19+
public function jsonSerialize(): array
20+
{
21+
return [
22+
'me' => $this,
23+
];
24+
}
25+
}

src/Symfony/Component/Serializer/Tests/Normalizer/JsonSerializableNormalizerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1818
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
1919
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20+
use Symfony\Component\Serializer\Serializer;
2021
use Symfony\Component\Serializer\SerializerInterface;
22+
use Symfony\Component\Serializer\Tests\Fixtures\JsonSerializableCircularReferenceDummy;
2123
use Symfony\Component\Serializer\Tests\Fixtures\JsonSerializableDummy;
24+
use Symfony\Component\Serializer\Tests\Normalizer\Features\CircularReferenceTestTrait;
2225

2326
/**
2427
* @author Fred Cox <mcfedr@gmail.com>
2528
*/
2629
class JsonSerializableNormalizerTest extends TestCase
2730
{
31+
use CircularReferenceTestTrait;
32+
2833
/**
2934
* @var JsonSerializableNormalizer
3035
*/
@@ -96,6 +101,19 @@ private function doTestCircularNormalize(bool $legacy = false)
96101
$this->assertEquals('string_object', $this->normalizer->normalize(new JsonSerializableDummy()));
97102
}
98103

104+
protected function getNormalizerForCircularReference(array $defaultContext): JsonSerializableNormalizer
105+
{
106+
$normalizer = new JsonSerializableNormalizer(null, null, $defaultContext);
107+
new Serializer([$normalizer]);
108+
109+
return $normalizer;
110+
}
111+
112+
protected function getSelfReferencingModel()
113+
{
114+
return new JsonSerializableCircularReferenceDummy();
115+
}
116+
99117
public function testInvalidDataThrowException()
100118
{
101119
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy