bug #30903 [Messenger] Uses the SerializerStamp
when deserializing the envelope (sroze)
This PR was merged into the 4.2 branch.
Discussion
----------
[Messenger] Uses the `SerializerStamp` when deserializing the envelope
| Q | A
| ------------- | ---
| Branch? | 4.2
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #29970
| License | MIT
| Doc PR | ø
Uses the `SerializerStamp` when decoding the message.
Commits
-------
ab55e8e7ef
Uses the SerializerStamp when deserializing the envelope
This commit is contained in:
commit
f88a79b550
@ -19,6 +19,7 @@ use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
|
|||||||
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
|
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
|
||||||
use Symfony\Component\Serializer as SerializerComponent;
|
use Symfony\Component\Serializer as SerializerComponent;
|
||||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||||
|
use Symfony\Component\Serializer\SerializerInterface as SerializerComponentInterface;
|
||||||
|
|
||||||
class SerializerTest extends TestCase
|
class SerializerTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -74,11 +75,23 @@ class SerializerTest extends TestCase
|
|||||||
|
|
||||||
public function testEncodedWithSymfonySerializerForStamps()
|
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($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);
|
$encoded = $serializer->encode($envelope);
|
||||||
@ -88,10 +101,40 @@ class SerializerTest extends TestCase
|
|||||||
$this->assertArrayHasKey('type', $encoded['headers']);
|
$this->assertArrayHasKey('type', $encoded['headers']);
|
||||||
$this->assertArrayHasKey('X-Message-Stamp-'.SerializerStamp::class, $encoded['headers']);
|
$this->assertArrayHasKey('X-Message-Stamp-'.SerializerStamp::class, $encoded['headers']);
|
||||||
$this->assertArrayHasKey('X-Message-Stamp-'.ValidationStamp::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)
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertEquals($serializerStamp, $decoded->last(SerializerStamp::class));
|
$symfonySerializer
|
||||||
$this->assertEquals($validationStamp, $decoded->last(ValidationStamp::class));
|
->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'))
|
||||||
|
;
|
||||||
|
|
||||||
|
$serializer->decode([
|
||||||
|
'body' => '{}',
|
||||||
|
'headers' => [
|
||||||
|
'type' => DummyMessage::class,
|
||||||
|
'X-Message-Stamp-'.SerializerStamp::class => '[{"context":{"groups":["foo"]}}]',
|
||||||
|
],
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Messenger\Envelope;
|
|||||||
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
|
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\Messenger\Exception\LogicException;
|
use Symfony\Component\Messenger\Exception\LogicException;
|
||||||
use Symfony\Component\Messenger\Stamp\SerializerStamp;
|
use Symfony\Component\Messenger\Stamp\SerializerStamp;
|
||||||
|
use Symfony\Component\Messenger\Stamp\StampInterface;
|
||||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||||
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
|
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
|
||||||
@ -69,10 +70,11 @@ class Serializer implements SerializerInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
$stamps = $this->decodeStamps($encodedEnvelope);
|
$stamps = $this->decodeStamps($encodedEnvelope);
|
||||||
|
$serializerStamp = $this->findFirstSerializerStamp($stamps);
|
||||||
|
|
||||||
$context = $this->context;
|
$context = $this->context;
|
||||||
if (isset($stamps[SerializerStamp::class])) {
|
if (null !== $serializerStamp) {
|
||||||
$context = end($stamps[SerializerStamp::class])->getContext() + $context;
|
$context = $serializerStamp->getContext() + $context;
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context);
|
$message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context);
|
||||||
@ -129,4 +131,18 @@ class Serializer implements SerializerInterface
|
|||||||
|
|
||||||
return $headers;
|
return $headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param StampInterface[] $stamps
|
||||||
|
*/
|
||||||
|
private function findFirstSerializerStamp(array $stamps): ?SerializerStamp
|
||||||
|
{
|
||||||
|
foreach ($stamps as $stamp) {
|
||||||
|
if ($stamp instanceof SerializerStamp) {
|
||||||
|
return $stamp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user