Skip to content

Commit

Permalink
[Serializer] Fix unitialized properties (from PHP 7.4.2) when seriali…
Browse files Browse the repository at this point in the history
…zing context for the cache key
  • Loading branch information
alanpoulain authored and fabpot committed Apr 4, 2020
1 parent 2b5a266 commit a4d9b0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
20 changes: 20 additions & 0 deletions Tests/Normalizer/AbstractObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}
}

0 comments on commit a4d9b0c

Please sign in to comment.