Skip to content

Changing to MessageDecodingFailedException so that invalid messages are rejected #30756

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function testEncodedIsDecodable()
$this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope)));
}

public function testDecodingFailsWithMissingBodyKey()
{
$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessage('Encoded envelope should have at least a "body".');

$serializer = new PhpSerializer();

$serializer->decode([]);
}

public function testDecodingFailsWithBadFormat()
{
$this->expectException(MessageDecodingFailedException::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ public function testDecodingFailsWithBadFormat()
]);
}

/**
* @dataProvider getMissingKeyTests
*/
public function testDecodingFailsWithMissingKeys(array $data, string $expectedMessage)
{
$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessage($expectedMessage);

$serializer = new Serializer();

$serializer->decode($data);
}

public function getMissingKeyTests()
{
yield 'no_body' => [
['headers' => ['type' => 'bar']],
'Encoded envelope should have at least a "body" and some "headers".',
];

yield 'no_headers' => [
['body' => '{}'],
'Encoded envelope should have at least a "body" and some "headers".',
];

yield 'no_headers_type' => [
['body' => '{}', 'headers' => ['foo' => 'bar']],
'Encoded envelope does not have a "type" header.',
];
}

public function testDecodingFailsWithBadClass()
{
$this->expectException(MessageDecodingFailedException::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Messenger\Transport\Serialization;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;

/**
Expand All @@ -28,7 +27,7 @@ class PhpSerializer implements SerializerInterface
public function decode(array $encodedEnvelope): Envelope
{
if (empty($encodedEnvelope['body'])) {
throw new InvalidArgumentException('Encoded envelope should have at least a "body".');
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
}

return $this->safelyUnserialize($encodedEnvelope['body']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Messenger\Transport\Serialization;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Stamp\SerializerStamp;
Expand Down Expand Up @@ -63,11 +62,11 @@ public static function create(): self
public function decode(array $encodedEnvelope): Envelope
{
if (empty($encodedEnvelope['body']) || empty($encodedEnvelope['headers'])) {
throw new InvalidArgumentException('Encoded envelope should have at least a "body" and some "headers".');
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body" and some "headers".');
}

if (empty($encodedEnvelope['headers']['type'])) {
throw new InvalidArgumentException('Encoded envelope does not have a "type" header.');
throw new MessageDecodingFailedException('Encoded envelope does not have a "type" header.');
}

$stamps = $this->decodeStamps($encodedEnvelope);
Expand Down