Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serializer] Catch \Throwable in getCacheKey() #36336

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
dunglas marked this conversation as resolved.
Show resolved Hide resolved
try {
return md5($format.serialize([
'context' => $context,
'ignored' => $this->ignoredAttributes,
'camelized' => $this->camelizedAttributes,
]));
} catch (\Throwable $exception) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\Exception?
catching \Throwable is a terrible practice, it hides coding issues and helps ppl lose their time to figure out what is broken...

Copy link
Member Author

@dunglas dunglas Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here, it's a very specific edge case, see #36332 (comment)
(We were already catch \Exception)

Copy link
Member Author

@dunglas dunglas Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't do that, if any value in the context contains a Doctrine entity, an error can be thrown, leading to a 500.

// 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');
}
}