Skip to content

Commit 4c32c1a

Browse files
committed
minor #17281 [Serializer] Unset object_to_populate after using it (dunglas)
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #17281). Discussion ---------- [Serializer] Unset object_to_populate after using it | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a The `object_to_populate` key must be unset after using it to avoid problems when normalizing sub objects. Needed for #17193. Commits ------- ff18b68 [Serializer] Unset object_to_populate after using it
2 parents da655a9 + ff18b68 commit 4c32c1a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ protected function prepareForDenormalization($data)
279279
* Instantiates an object using constructor parameters when needed.
280280
*
281281
* This method also allows to denormalize data into an existing object if
282-
* it is present in the context with the object_to_populate key.
282+
* it is present in the context with the object_to_populate. This object
283+
* is removed from the context before being returned to avoid side effects
284+
* when recursively normalizing an object graph.
283285
*
284286
* @param array $data
285287
* @param string $class
@@ -298,7 +300,10 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
298300
is_object($context['object_to_populate']) &&
299301
$class === get_class($context['object_to_populate'])
300302
) {
301-
return $context['object_to_populate'];
303+
$object = $context['object_to_populate'];
304+
unset($context['object_to_populate']);
305+
306+
return $object;
302307
}
303308

304309
$constructor = $reflectionClass->getConstructor();

0 commit comments

Comments
 (0)