Skip to content

[Messenger] Uses the SerializerStamp when deserializing the envelope #30903

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 1 commit into from
Apr 6, 2019
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 @@ -19,6 +19,7 @@
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
use Symfony\Component\Serializer as SerializerComponent;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface as SerializerComponentInterface;

class SerializerTest extends TestCase
{
Expand Down Expand Up @@ -74,11 +75,23 @@ public function testUsesTheCustomFormatAndContext()

public function testEncodedWithSymfonySerializerForStamps()
{
$serializer = new Serializer();
$serializer = new Serializer(
$symfonySerializer = $this->createMock(SerializerComponentInterface::class)
);

$envelope = (new Envelope(new DummyMessage('Hello')))
$envelope = (new Envelope($message = new DummyMessage('test')))
->with($serializerStamp = new SerializerStamp([ObjectNormalizer::GROUPS => ['foo']]))
->with($validationStamp = new ValidationStamp(['foo', 'bar']))
->with($validationStamp = new ValidationStamp(['foo', 'bar']));

$symfonySerializer
->expects($this->at(2))
->method('serialize')->with(
$message,
'json',
[
ObjectNormalizer::GROUPS => ['foo'],
]
)
;

$encoded = $serializer->encode($envelope);
Expand All @@ -88,10 +101,40 @@ public function testEncodedWithSymfonySerializerForStamps()
$this->assertArrayHasKey('type', $encoded['headers']);
$this->assertArrayHasKey('X-Message-Stamp-'.SerializerStamp::class, $encoded['headers']);
$this->assertArrayHasKey('X-Message-Stamp-'.ValidationStamp::class, $encoded['headers']);
}

$decoded = $serializer->decode($encoded);
public function testDecodeWithSymfonySerializerStamp()
{
$serializer = new Serializer(
$symfonySerializer = $this->createMock(SerializerComponentInterface::class)
);

$symfonySerializer
->expects($this->at(0))
->method('deserialize')
->with('[{"context":{"groups":["foo"]}}]', SerializerStamp::class.'[]', 'json', [])
->willReturn([new SerializerStamp(['groups' => ['foo']])])
;

$symfonySerializer
->expects($this->at(1))
->method('deserialize')->with(
'{}',
DummyMessage::class,
'json',
[
ObjectNormalizer::GROUPS => ['foo'],
]
)
->willReturn(new DummyMessage('test'))
;

$this->assertEquals($serializerStamp, $decoded->last(SerializerStamp::class));
$this->assertEquals($validationStamp, $decoded->last(ValidationStamp::class));
$serializer->decode([
'body' => '{}',
'headers' => [
'type' => DummyMessage::class,
'X-Message-Stamp-'.SerializerStamp::class => '[{"context":{"groups":["foo"]}}]',
],
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Stamp\SerializerStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
Expand Down Expand Up @@ -69,10 +70,11 @@ public function decode(array $encodedEnvelope): Envelope
}

$stamps = $this->decodeStamps($encodedEnvelope);
$serializerStamp = $this->findFirstSerializerStamp($stamps);

$context = $this->context;
if (isset($stamps[SerializerStamp::class])) {
$context = end($stamps[SerializerStamp::class])->getContext() + $context;
if (null !== $serializerStamp) {
$context = $serializerStamp->getContext() + $context;
}

$message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context);
Expand Down Expand Up @@ -129,4 +131,18 @@ private function encodeStamps(Envelope $envelope): array

return $headers;
}

/**
* @param StampInterface[] $stamps
*/
private function findFirstSerializerStamp(array $stamps): ?SerializerStamp
{
foreach ($stamps as $stamp) {
if ($stamp instanceof SerializerStamp) {
return $stamp;
}
}

return null;
}
}