Skip to content

[Messenger] Uses Symfony Serializer by default for envelope items #28270

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 @@ -80,24 +80,30 @@ public function testUsesTheCustomFormatAndContext()
$this->assertSame($message, $decoded->getMessage());
}

public function testEncodedWithSerializationConfiguration()
public function testEncodedWithSymfonySerializerForItems()
{
$serializer = new Serializer(
new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder()))
new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())),
'json',
array()
);

$envelope = Envelope::wrap(new DummyMessage('Hello'))
->with(new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo'))))
->with(new ValidationConfiguration(array('foo', 'bar')))
->with($serializerConfiguration = new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo'))))
->with($validationConfiguration = new ValidationConfiguration(array('foo', 'bar')))
;

$encoded = $serializer->encode($envelope);

$this->assertArrayHasKey('body', $encoded);
$this->assertArrayHasKey('headers', $encoded);
$this->assertArrayHasKey('type', $encoded['headers']);
$this->assertEquals(DummyMessage::class, $encoded['headers']['type']);
$this->assertArrayHasKey('X-Message-Envelope-Items', $encoded['headers']);
$this->assertSame('a:2:{s:75:"Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration";O:75:"Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration":1:{s:84:"'."\0".'Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration'."\0".'context";a:1:{s:6:"groups";a:1:{i:0;s:3:"foo";}}}s:76:"Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration";O:76:"Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration":1:{s:84:"'."\0".'Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration'."\0".'groups";a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}}}', $encoded['headers']['X-Message-Envelope-Items']);
$this->assertArrayHasKey('X-Message-Envelope-Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration', $encoded['headers']);
$this->assertArrayHasKey('X-Message-Envelope-Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration', $encoded['headers']);

$decoded = $serializer->decode($encoded);

$this->assertEquals($serializerConfiguration, $decoded->get(SerializerConfiguration::class));
$this->assertEquals($validationConfiguration, $decoded->get(ValidationConfiguration::class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function decode(array $encodedEnvelope): Envelope
throw new \InvalidArgumentException('Encoded envelope does not have a `type` header.');
}

$envelopeItems = isset($encodedEnvelope['headers']['X-Message-Envelope-Items']) ? unserialize($encodedEnvelope['headers']['X-Message-Envelope-Items']) : array();
$envelopeItems = $this->decodeEnvelopeItems($encodedEnvelope);

$context = $this->context;
/** @var SerializerConfiguration|null $serializerConfig */
Expand All @@ -67,14 +67,39 @@ public function encode(Envelope $envelope): array
$context = $serializerConfig->getContext() + $context;
}

$headers = array('type' => \get_class($envelope->getMessage()));
if ($configurations = $envelope->all()) {
$headers['X-Message-Envelope-Items'] = serialize($configurations);
}
$headers = array('type' => \get_class($envelope->getMessage())) + $this->encodeEnvelopeItems($envelope);

return array(
'body' => $this->serializer->serialize($envelope->getMessage(), $this->format, $context),
'headers' => $headers,
);
}

private function decodeEnvelopeItems($encodedEnvelope)
{
$items = array();
foreach ($encodedEnvelope['headers'] as $name => $value) {
if (0 !== strpos($name, $prefix = 'X-Message-Envelope-')) {
continue;
}

$items[] = $this->serializer->deserialize($value, substr($name, \strlen($prefix)), $this->format, $this->context);
}

return $items;
}

private function encodeEnvelopeItems(Envelope $envelope)
{
if (!$configurations = $envelope->all()) {
return array();
}

$headers = array();
foreach ($configurations as $configuration) {
$headers['X-Message-Envelope-'.\get_class($configuration)] = $this->serializer->serialize($configuration, $this->format, $this->context);
}

return $headers;
}
}