Skip to content

Commit

Permalink
[Serializer] Catch \Throwable in getCacheKey()
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Apr 3, 2020
1 parent 5da141b commit ea24bfc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Expand Up @@ -401,6 +401,20 @@ protected function createChildContext(array $parentContext, $attribute/*, string
private function getCacheKey($format, array $context)
{
unset($context['cache_key']); // avoid artificially different keys

if (interface_exists(\Throwable::class)) {
try {
return md5($format.serialize([
'context' => $context,
'ignored' => $this->ignoredAttributes,
'camelized' => $this->camelizedAttributes,
]));
} catch (\Throwable $exception) {
// The context cannot be serialized, skip the cache
return false;
}
}

try {
return md5($format.serialize([
'context' => $context,
Expand Down
Expand Up @@ -194,6 +194,13 @@ public function testExtraAttributesException()
'allow_extra_attributes' => false,
]);
}

public function testContextNotSerializable()
{
$normalizer = new ObjectNormalizer();
$result = $normalizer->normalize(new Dummy(), null, ['not_serializable' => new NotSerializable()]);
$this->assertIsArray($result);
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down Expand Up @@ -379,3 +386,15 @@ public function setSerializer(SerializerInterface $serializer)
$this->serializer = $serializer;
}
}

class NotSerializable
{
function __sleep()
{
if (class_exists(\Error::class)) {
throw new \Error('not serializable');
}

throw new \Exception('not serializable');
}
}

0 comments on commit ea24bfc

Please sign in to comment.