[Serializer] Fix unitialized properties (from PHP 7.4.2) when serializing context for the cache key

This commit is contained in:
Alan Poulain 2020-04-03 12:40:07 +02:00 committed by Fabien Potencier
parent 5da141b8d0
commit 1fafff7c10
2 changed files with 21 additions and 0 deletions

View File

@ -400,6 +400,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
*/
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([

View File

@ -163,6 +163,14 @@ class AbstractObjectNormalizerTest extends TestCase
$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 @@ class ArrayDenormalizerDummy implements DenormalizerInterface, SerializerAwareIn
$this->serializer = $serializer;
}
}
class NotSerializable
{
public function __sleep()
{
if (class_exists(\Error::class)) {
throw new \Error('not serializable');
}
throw new \Exception('not serializable');
}
}