Skip to content

Commit 9ac0062

Browse files
MariusJamsoyuka
andcommitted
fix(symfony): context not serializable when session (#6302)
* fix(symfony): context not serializable when session * phpstan --------- Co-authored-by: soyuka <[email protected]>
1 parent 6c3d58c commit 9ac0062

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Symfony/Messenger/ContextStamp.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Symfony\Messenger;
1515

16+
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\Messenger\Stamp\StampInterface;
1718

1819
/**
@@ -22,8 +23,15 @@
2223
*/
2324
final class ContextStamp implements StampInterface
2425
{
25-
public function __construct(private readonly array $context = [])
26+
private readonly array $context;
27+
28+
public function __construct(array $context = [])
2629
{
30+
if (($request = ($context['request'] ?? null)) && $request instanceof Request && $request->hasSession()) {
31+
unset($context['request']);
32+
}
33+
34+
$this->context = $context;
2735
}
2836

2937
/**

tests/Symfony/Messenger/ContextStampTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Symfony\Messenger\ContextStamp;
1717
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\Messenger\Stamp\StampInterface;
1920

2021
/**
@@ -32,4 +33,16 @@ public function testGetContext(): void
3233
$contextStamp = new ContextStamp();
3334
$this->assertIsArray($contextStamp->getContext());
3435
}
36+
37+
/**
38+
* @doesNotPerformAssertions
39+
*/
40+
public function testSerializable(): void
41+
{
42+
$request = new Request();
43+
$request->setSessionFactory(function (): void {}); // @phpstan-ignore-line
44+
45+
$stamp = new ContextStamp(['request' => $request]);
46+
serialize($stamp);
47+
}
3548
}

0 commit comments

Comments
 (0)