Skip to content

Navigation Menu

Sign in
Appearance settings
Sign up
Appearance settings

Commit 3a040d0

Browse filesBrowse the repository at this point in the historyBrowse files
oliinykdmnicolas-grekas
authored andcommitted
[Serializer] Fix max depth counting for subclasses that inherit MaxDepth metadata
1 parent 1e5f7b7 commit 3a040d0
Copy full SHA for 3a040d0

4 files changed

+87-1Lines changed: 87 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,18 @@ private function isMaxDepthReached(array $attributesMetadata, string $class, str
762762
return false;
763763
}
764764

765-
$key = \sprintf(self::DEPTH_KEY_PATTERN, $class, $attribute);
765+
$keyClass = $class;
766+
if ($this->classMetadataFactory) {
767+
while (false !== $parent = get_parent_class($keyClass)) {
768+
$parentAttributes = $this->classMetadataFactory->getMetadataFor($parent)->getAttributesMetadata();
769+
if (($parentAttributes[$attribute] ?? null)?->getMaxDepth() !== $maxDepth) {
770+
break;
771+
}
772+
$keyClass = $parent;
773+
}
774+
}
775+
776+
$key = \sprintf(self::DEPTH_KEY_PATTERN, $keyClass, $attribute);
766777
if (!isset($context[$key])) {
767778
$context[$key] = 1;
768779

Collapse file
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Attributes;
13+
14+
use Symfony\Component\Serializer\Attribute\MaxDepth;
15+
16+
class MaxDepthRecursiveDummy
17+
{
18+
public $name;
19+
20+
#[MaxDepth(1)]
21+
public $linked;
22+
23+
public function getName()
24+
{
25+
return $this->name;
26+
}
27+
28+
public function getLinked()
29+
{
30+
return $this->linked;
31+
}
32+
}
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Attributes;
13+
14+
class MaxDepthRecursiveDummyProxy extends MaxDepthRecursiveDummy
15+
{
16+
}
Collapse file

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/MaxDepthTestTrait.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/MaxDepthTestTrait.php
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1515
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\MaxDepthDummy;
16+
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\MaxDepthRecursiveDummy;
17+
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\MaxDepthRecursiveDummyProxy;
1618

1719
/**
1820
* Covers AbstractObjectNormalizer::ENABLE_MAX_DEPTH and AbstractObjectNormalizer::MAX_DEPTH_HANDLER.
@@ -60,6 +62,31 @@ public function testMaxDepth()
6062
$this->assertEquals($expected, $result);
6163
}
6264

65+
public function testMaxDepthWithSubclassInheritingMetadata()
66+
{
67+
$normalizer = $this->getNormalizerForMaxDepth();
68+
69+
$level1 = new MaxDepthRecursiveDummy();
70+
$level1->name = 'level1';
71+
72+
$level2 = new MaxDepthRecursiveDummyProxy();
73+
$level2->name = 'level2';
74+
$level1->linked = $level2;
75+
76+
$level3 = new MaxDepthRecursiveDummy();
77+
$level3->name = 'level3';
78+
$level2->linked = $level3;
79+
80+
$result = $normalizer->normalize($level1, null, ['enable_max_depth' => true]);
81+
82+
$this->assertEquals([
83+
'name' => 'level1',
84+
'linked' => [
85+
'name' => 'level2',
86+
],
87+
], $result);
88+
}
89+
6390
public function testMaxDepthHandler()
6491
{
6592
$level1 = new MaxDepthDummy();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.