8000 [Serializer] Catch \Throwable in getCacheKey() by dunglas · Pull Request #36336 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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

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.
Loading
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Serializer] Catch \Throwable in getCacheKey()
  • Loading branch information
dunglas committed Apr 3, 2020
commit f4cd9a59393e714f7c324dda8e6f3b32e54a5601
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) {
Copy link
Member
@nicolas-grekas nicolas-grekas Apr 3, 2020

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 testNormalizeNotSerializab 8067 leContext()
'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