Skip to content

[Messenger] set amqp content_type based on serialization format #31790

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
Jun 4, 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 @@ -69,6 +69,39 @@ public function testItSendsTheEncodedMessageWithoutHeaders()
$sender->send($envelope);
}

public function testContentTypeHeaderIsMovedToAttribute()
{
$envelope = new Envelope(new DummyMessage('Oy'));
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class, 'Content-Type' => 'application/json']];

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
$serializer->method('encode')->with($envelope)->willReturnOnConsecutiveCalls($encoded);

$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
unset($encoded['headers']['Content-Type']);
$stamp = new AmqpStamp(null, AMQP_NOPARAM, ['content_type' => 'application/json']);
$connection->expects($this->once())->method('publish')->with($encoded['body'], $encoded['headers'], 0, $stamp);

$sender = new AmqpSender($connection, $serializer);
$sender->send($envelope);
}

public function testContentTypeHeaderDoesNotOverwriteAttribute()
{
$envelope = (new Envelope(new DummyMessage('Oy')))->with($stamp = new AmqpStamp('rk', AMQP_NOPARAM, ['content_type' => 'custom']));
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class, 'Content-Type' => 'application/json']];

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
$serializer->method('encode')->with($envelope)->willReturnOnConsecutiveCalls($encoded);

$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
unset($encoded['headers']['Content-Type']);
$connection->expects($this->once())->method('publish')->with($encoded['body'], $encoded['headers'], 0, $stamp);

$sender = new AmqpSender($connection, $serializer);
$sender->send($envelope);
}

/**
* @expectedException \Symfony\Component\Messenger\Exception\TransportException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function testEncodedIsHavingTheBodyAndTypeHeader()
$this->assertArrayHasKey('body', $encoded);
$this->assertArrayHasKey('headers', $encoded);
$this->assertArrayHasKey('type', $encoded['headers']);
$this->assertEquals(DummyMessage::class, $encoded['headers']['type']);
$this->assertSame(DummyMessage::class, $encoded['headers']['type']);
$this->assertSame('application/json', $encoded['headers']['Content-Type']);
}

public function testUsesTheCustomFormatAndContext()
Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,26 @@ public function send(Envelope $envelope): Envelope
$delay = $delayStamp->getDelay();
}

$amqpStamp = $envelope->last(AmqpStamp::class);
if (isset($encodedMessage['headers']['Content-Type'])) {
$contentType = $encodedMessage['headers']['Content-Type'];
unset($encodedMessage['headers']['Content-Type']);

$attributes = $amqpStamp ? $amqpStamp->getAttributes() : [];

if (!isset($attributes['content_type'])) {
$attributes['content_type'] = $contentType;

$amqpStamp = new AmqpStamp($amqpStamp ? $amqpStamp->getRoutingKey() : null, $amqpStamp ? $amqpStamp->getFlags() : AMQP_NOPARAM, $attributes);
}
}

try {
$this->connection->publish(
$encodedMessage['body'],
$encodedMessage['headers'] ?? [],
$delay,
$envelope->last(AmqpStamp::class)
$amqpStamp
);
} catch (\AMQPException $e) {
throw new TransportException($e->getMessage(), 0, $e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function encode(Envelope $envelope): array

$envelope = $envelope->withoutStampsOfType(NonSendableStampInterface::class);

$headers = ['type' => \get_class($envelope->getMessage())] + $this->encodeStamps($envelope);
$headers = ['type' => \get_class($envelope->getMessage())] + $this->encodeStamps($envelope) + $this->getContentTypeHeader();

return [
'body' => $this->serializer->serialize($envelope->getMessage(), $this->format, $context),
Expand Down Expand Up @@ -157,4 +157,28 @@ private function findFirstSerializerStamp(array $stamps): ?SerializerStamp

return null;
}

private function getContentTypeHeader(): array
{
$mimeType = $this->getMimeTypeForFormat();

return null === $mimeType ? [] : ['Content-Type' => $mimeType];
}

private function getMimeTypeForFormat(): ?string
{
switch ($this->format) {
case 'json':
return 'application/json';
case 'xml':
return 'application/xml';
case 'yml':
case 'yaml':
return 'application/x-yaml';
case 'csv':
return 'text/csv';
}

return null;
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy