Skip to content

Commit 21fffca

Browse files
Patrick BerenschotTobion
Patrick Berenschot
authored andcommitted
[Messenger] Check for all serialization exceptions during message dec…
1 parent 5f87c7b commit 21fffca

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,40 @@ public function testEncodedSkipsNonEncodeableStamps()
207207
$encoded = $serializer->encode($envelope);
208208
$this->assertStringNotContainsString('DummySymfonySerializerNonSendableStamp', print_r($encoded['headers'], true));
209209
}
210+
211+
public function testDecodingFailedConstructorDeserialization()
212+
{
213+
$serializer = new Serializer();
214+
215+
$this->expectException(MessageDecodingFailedException::class);
216+
217+
$serializer->decode([
218+
'body' => '{}',
219+
'headers' => ['type' => DummySymfonySerializerInvalidConstructor::class],
220+
]);
221+
}
222+
223+
public function testDecodingStampFailedDeserialization()
224+
{
225+
$serializer = new Serializer();
226+
227+
$this->expectException(MessageDecodingFailedException::class);
228+
229+
$serializer->decode([
230+
'body' => '{"message":"hello"}',
231+
'headers' => [
232+
'type' => DummyMessage::class,
233+
'X-Message-Stamp-'.SerializerStamp::class => '[{}]',
234+
],
235+
]);
236+
}
210237
}
211238
class DummySymfonySerializerNonSendableStamp implements NonSendableStampInterface
212239
{
213240
}
241+
class DummySymfonySerializerInvalidConstructor
242+
{
243+
public function __construct(string $missingArgument)
244+
{
245+
}
246+
}

src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Symfony\Component\Messenger\Stamp\StampInterface;
2020
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2121
use Symfony\Component\Serializer\Encoder\XmlEncoder;
22-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
22+
use Symfony\Component\Serializer\Exception\ExceptionInterface;
2323
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2424
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2525
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
@@ -81,7 +81,7 @@ public function decode(array $encodedEnvelope): Envelope
8181

8282
try {
8383
$message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context);
84-
} catch (UnexpectedValueException $e) {
84+
} catch (ExceptionInterface $e) {
8585
throw new MessageDecodingFailedException(sprintf('Could not decode message: %s.', $e->getMessage()), $e->getCode(), $e);
8686
}
8787

@@ -119,7 +119,7 @@ private function decodeStamps(array $encodedEnvelope): array
119119

120120
try {
121121
$stamps[] = $this->serializer->deserialize($value, substr($name, \strlen(self::STAMP_HEADER_PREFIX)).'[]', $this->format, $this->context);
122-
} catch (UnexpectedValueException $e) {
122+
} catch (ExceptionInterface $e) {
123123
throw new MessageDecodingFailedException(sprintf('Could not decode stamp: %s.', $e->getMessage()), $e->getCode(), $e);
124124
}
125125
}

0 commit comments

Comments
 (0)