Skip to content

Commit

Permalink
[Messenger] Check for all serialization exceptions during message dec…
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Berenschot authored and Tobion committed Jan 29, 2020
1 parent 551d579 commit c226393
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
33 changes: 33 additions & 0 deletions Tests/Transport/Serialization/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,40 @@ public function testEncodedSkipsNonEncodeableStamps()
$encoded = $serializer->encode($envelope);
$this->assertStringNotContainsString('DummySymfonySerializerNonSendableStamp', print_r($encoded['headers'], true));
}

public function testDecodingFailedConstructorDeserialization()
{
$serializer = new Serializer();

$this->expectException(MessageDecodingFailedException::class);

$serializer->decode([
'body' => '{}',
'headers' => ['type' => DummySymfonySerializerInvalidConstructor::class],
]);
}

public function testDecodingStampFailedDeserialization()
{
$serializer = new Serializer();

$this->expectException(MessageDecodingFailedException::class);

$serializer->decode([
'body' => '{"message":"hello"}',
'headers' => [
'type' => DummyMessage::class,
'X-Message-Stamp-'.SerializerStamp::class => '[{}]',
],
]);
}
}
class DummySymfonySerializerNonSendableStamp implements NonSendableStampInterface
{
}
class DummySymfonySerializerInvalidConstructor
{
public function __construct(string $missingArgument)
{
}
}
6 changes: 3 additions & 3 deletions Transport/Serialization/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer as SymfonySerializer;
Expand Down Expand Up @@ -81,7 +81,7 @@ public function decode(array $encodedEnvelope): Envelope

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

Expand Down Expand Up @@ -119,7 +119,7 @@ private function decodeStamps(array $encodedEnvelope): array

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

0 comments on commit c226393

Please sign in to comment.