Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed May 16, 2024
1 parent 5be4490 commit 48b3af7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testDecodingFailsWithMissingBodyKey()
$serializer = new AmazonSqsSerializer();

$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessage('Encoded envelope should have at least a "body", or maybe you should implement your own serializer');
$this->expectExceptionMessage('Encoded envelope should have at least a "body"');

$serializer->decode([]);
}
Expand All @@ -53,18 +53,6 @@ public function testDecodingFailsWithBadFormat()
]);
}

public function testDecodingFailsWithBadBase64Body()
{
$serializer = new AmazonSqsSerializer();

$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessageMatches('/Could not decode/');

$serializer->decode([
'body' => 'x',
]);
}

public function testDecodingFailsWithBadClass()
{
$serializer = new AmazonSqsSerializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Transport;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;

class AmazonSqsSerializer extends PhpSerializer
{
protected function shouldBeEncoded(string $body): bool
public function encode(Envelope $envelope): array
{
return parent::shouldBeEncoded($body)
|| preg_match('/[^\x20-\x{D7FF}\xA\xD\x9\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u', $body);
$encoded = parent::encode($envelope);
if (preg_match('/[^\x20-\x{D7FF}\xA\xD\x9\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]/u', $encoded['body'])) {
$encoded['body'] = base64_encode($encoded['body']);
}

return $encoded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.2.5",
"async-aws/core": "^1.5",
"async-aws/sqs": "^1.0|^2.0",
"symfony/messenger": "^5.4.40|^6.4.8",
"symfony/messenger": "^4.4.19|^5.0|^6.0",
"symfony/service-contracts": "^1.1|^2|^3",
"psr/log": "^1|^2|^3"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function encode(Envelope $envelope): array

$body = addslashes(serialize($envelope));

if ($this->shouldBeEncoded($body)) {
if (!preg_match('//u', $body)) {
$body = base64_encode($body);
}

Expand All @@ -50,14 +50,6 @@ public function encode(Envelope $envelope): array
];
}

/**
* Entry point to use stricter condition for base64 encoding.
*/
protected function shouldBeEncoded(string $body): bool
{
return !preg_match('//u', $body);
}

private function safelyUnserialize(string $contents)
{
if ('' === $contents) {
Expand Down

0 comments on commit 48b3af7

Please sign in to comment.