Skip to content

Commit

Permalink
fix(symfony): context not serializable when session (#6302)
Browse files Browse the repository at this point in the history
* fix(symfony): context not serializable when session

* phpstan

---------

Co-authored-by: soyuka <soyuka@users.noreply.github.com>
  • Loading branch information
MariusJam and soyuka committed Apr 13, 2024
1 parent 6fd38c0 commit 2c25293
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Symfony/Messenger/ContextStamp.php
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Symfony\Messenger;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\Stamp\StampInterface;

/**
Expand All @@ -22,8 +23,15 @@
*/
final class ContextStamp implements StampInterface
{
public function __construct(private readonly array $context = [])
private readonly array $context;

public function __construct(array $context = [])
{
if (($request = ($context['request'] ?? null)) && $request instanceof Request && $request->hasSession()) {
unset($context['request']);
}

$this->context = $context;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/Symfony/Messenger/ContextStampTest.php
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Symfony\Messenger\ContextStamp;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Messenger\Stamp\StampInterface;

/**
Expand All @@ -32,4 +33,16 @@ public function testGetContext(): void
$contextStamp = new ContextStamp();
$this->assertIsArray($contextStamp->getContext());
}

/**
* @doesNotPerformAssertions
*/
public function testSerializable(): void
{
$request = new Request();
$request->setSessionFactory(function (): void {}); // @phpstan-ignore-line

$stamp = new ContextStamp(['request' => $request]);
serialize($stamp);
}
}

0 comments on commit 2c25293

Please sign in to comment.