8000 bug #54395 [Serializer] Fixing PHP warning in the ObjectNormalizer wi… · symfony/symfony@6e12c6a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e12c6a

Browse files
bug #54395 [Serializer] Fixing PHP warning in the ObjectNormalizer with MaxDepth enabled (jaydiablo)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Serializer] Fixing PHP warning in the ObjectNormalizer with MaxDepth enabled | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #54378 | License | MIT This fixes a specific issue I ran into when testing an application in Symfony 6 that uses the rompetompe/inertia-bundle to serialize PHP data to JSON for use by Inertia.js. Added a test that is as minimal as necessary for the Warning to be thrown. I tested against Symfony 5.4 as well, but the issue doesn't seem to be present there (could have something to do with 5.4 not having the AttributeLoader). Commits ------- 31e3bde [Serializer] Fixing PHP warning in the ObjectNormalizer with MaxDepth enabled
2 parents a10cc01 + 31e3bde commit 6e12c6a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ private function updateData(array $data, string $attribute, mixed $attributeValu
707707
private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
708708
{
709709
if (!($enableMaxDepth = $context[self::ENABLE_MAX_DEPTH] ?? $this->defaultContext[self::ENABLE_MAX_DEPTH] ?? false)
710-
|| null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
710+
|| !isset($attributesMetadata[$attribute]) || null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
711711
) {
712712
return false;
713713
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,20 @@ protected function setAttributeValue(object $object, string $attribute, $value,
10531053

10541054
$this->assertSame('scalar', $normalizer->denormalize('scalar', XmlScalarDummy::class, 'xml')->value);
10551055
}
1056+
1057+
public function testNormalizationWithMaxDepthOnStdclassObjectDoesNotThrowWarning()
1058+
{
1059+
$object = new \stdClass();
1060+
$object->string = 'yes';
1061+
1062+
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
1063+
$normalizer = new ObjectNormalizer($classMetadataFactory);
1064+
$normalized = $normalizer->normalize($object, context: [
1065+
AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true,
1066+
]);
1067+
1068+
$this->assertSame(['string' => 'yes'], $normalized);
1069+
}
10561070
}
10571071

10581072
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer

0 commit comments

Comments
 (0)
0