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 f4cd9a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,7 @@ public function testNormalizeNotSerializableContext()
'bar' => null,
];

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

public function testMaxDepth()
Expand Down Expand Up @@ -1102,3 +1101,15 @@ public function getFoo()
return $this->Foo;
}
}

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

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

0 comments on commit f4cd9a5

Please sign in to comment.