From a4d9b0c9cd7684bff81d2dee3ceab4961b3014e7 Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Fri, 3 Apr 2020 12:40:07 +0200 Subject: [PATCH] [Serializer] Fix unitialized properties (from PHP 7.4.2) when serializing context for the cache key --- Normalizer/AbstractObjectNormalizer.php | 1 + .../AbstractObjectNormalizerTest.php | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Normalizer/AbstractObjectNormalizer.php b/Normalizer/AbstractObjectNormalizer.php index 56dc3c590..1eba26343 100644 --- a/Normalizer/AbstractObjectNormalizer.php +++ b/Normalizer/AbstractObjectNormalizer.php @@ -400,6 +400,7 @@ protected function createChildContext(array $parentContext, $attribute/*, string */ private function getCacheKey($format, array $context) { + unset($context[self::OBJECT_TO_POPULATE]); unset($context['cache_key']); // avoid artificially different keys try { return md5($format.serialize([ diff --git a/Tests/Normalizer/AbstractObjectNormalizerTest.php b/Tests/Normalizer/AbstractObjectNormalizerTest.php index d5d7cace3..d269da27e 100644 --- a/Tests/Normalizer/AbstractObjectNormalizerTest.php +++ b/Tests/Normalizer/AbstractObjectNormalizerTest.php @@ -163,6 +163,14 @@ public function testDenormalizeStringCollectionDecodedFromXmlWithTwoChildren() $this->assertEquals('bar', $stringCollection->children[1]); } + public function testDenormalizeNotSerializableObjectToPopulate() + { + $normalizer = new AbstractObjectNormalizerDummy(); + $normalizedData = $normalizer->denormalize(['foo' => 'foo'], Dummy::class, null, [AbstractObjectNormalizer::OBJECT_TO_POPULATE => new NotSerializable()]); + + $this->assertSame('foo', $normalizedData->foo); + } + private function getDenormalizerForStringCollection() { $extractor = $this->getMockBuilder(PhpDocExtractor::class)->getMock(); @@ -379,3 +387,15 @@ public function setSerializer(SerializerInterface $serializer) $this->serializer = $serializer; } } + +class NotSerializable +{ + public function __sleep() + { + if (class_exists(\Error::class)) { + throw new \Error('not serializable'); + } + + throw new \Exception('not serializable'); + } +}