Skip to content

Commit 86ab77c

Browse files
committed
[Serializer] Catch \Throwable in getCacheKey()
1 parent 5da141b commit 86ab77c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,20 @@ protected function createChildContext(array $parentContext, $attribute/*, string
401401
private function getCacheKey($format, array $context)
402402
{
403403
unset($context['cache_key']); // avoid artificially different keys
404+
405+
if (interface_exists(\Throwable::class)) {
406+
try {
407+
return md5($format.serialize([
408+
'context' => $context,
409+
'ignored' => $this->ignoredAttributes,
410+
'camelized' => $this->camelizedAttributes,
411+
]));
412+
} catch (\Throwable $exception) {
413+
// The context cannot be serialized, skip the cache
414+
return false;
415+
}
416+
}
417+
404418
try {
405419
return md5($format.serialize([
406420
'context' => $context,

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,7 @@ public function testNormalizeNotSerializableContext()
565565
'bar' => null,
566566
];
567567

568-
$this->assertEquals($expected, $this->normalizer->normalize($objectDummy, null, ['not_serializable' => function () {
569-
}]));
568+
$this->assertEquals($expected, $this->normalizer->normalize($objectDummy, null, ['not_serializable' => new NotSerializable()]));
570569
}
571570

572571
public function testMaxDepth()
@@ -1102,3 +1101,16 @@ public function getFoo()
11021101
return $this->Foo;
11031102
}
11041103
}
1104+
1105+
1106+
class NotSerializable
1107+
{
1108+
public function __sleep()
1109+
{
1110+
if (class_exists(\Error::class)) {
1111+
throw new \Error('not serializable');
1112+
}
1113+
1114+
throw new \Exception('not serializable');
1115+
}
1116+
}

0 commit comments

Comments
 (0)