Skip to content
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

Fix Serializer encode for NonSendableStampInterface #15

Open
nyroDev opened this issue Jun 29, 2022 · 1 comment
Open

Fix Serializer encode for NonSendableStampInterface #15

nyroDev opened this issue Jun 29, 2022 · 1 comment

Comments

@nyroDev
Copy link

nyroDev commented Jun 29, 2022

On the encode function of the Serilizer, all stamps are used and sent in the header stamps.

This seems to work correctly before.
But now, there is a NonSendableStampInterface that should be used to find which stamp could be used in that case.

The comment on that classe reads:
A stamp that should not be included with the Envelope if sent to a transport.

In order to fix it, I updated the code like that:

use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;

$allStamps = [];
foreach ($envelope->all() as $stamps) {
    $allStamps = array_merge($allStamps, $stamps);
}

// Filter to keep only correct stamps.
$allStamps = array_filter($allStamps, function ($stamp) {
    return !is_object($stamp) || !($stamp instanceof NonSendableStampInterface);
});

I wanted to send a pull request, but I cannot where this code is.
It's referenced with some ID that I could not find in the repo...

@gnumoksha
Copy link

My code is like:

readonly class FooInterchangeableSerializer implements SerializerInterface
{
    public function encode(Envelope $envelope): array
    {
        $envelope = $envelope->withoutStampsOfType(NonSendableStampInterface::class);

        [...]
     }
     
     public function decode(array $encodedEnvelope): Envelope
    {
        if (empty($encodedEnvelope['body'])) {
            throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
        }

        $headers = $encodedEnvelope['headers'];
        $stamps = [];
        if (isset($headers['stamps'])) {
            /** @var array $stamps */
            $stamps = unserialize(base64_decode($headers['stamps']));
        }

        /**
         * The method all() returns a different format that the one excepted by the {@see Envelope::__construct()},
         * so normalize them.
         */
        /** @var StampInterface[] $normalizedStamps */
        $normalizedStamps = [];
        foreach ($stamps as $stamp) {
            if (!is_array($stamp)) {
                $normalizedStamps[] = $stamp;
                continue;
            }

            foreach ($stamp as $innerStamp) {
                $normalizedStamps[] = $innerStamp;
            }
        }

        $body = $encodedEnvelope['body'];
        $event = EventSerializer::decode($body);

        return new Envelope($event, $normalizedStamps);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants