Skip to content

Commit 43350d0

Browse files
committed
fix(symfony): set normalization context in request attributes
1 parent f63fd81 commit 43350d0

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/State/Provider/ReadProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
7171

7272
if ($this->serializerContextBuilder && $request) {
7373
// Builtin data providers are able to use the serialization context to automatically add join clauses
74-
$context += $this->serializerContextBuilder->createFromRequest($request, true, [
74+
$context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, [
7575
'resource_class' => $operation->getClass(),
7676
'operation' => $operation,
7777
]);
78+
$request->attributes->set('_api_normalization_context', $normalizationContext);
7879
}
7980

8081
try {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\State\Tests\Provider;
15+
16+
use ApiPlatform\Metadata\Get;
17+
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
18+
use ApiPlatform\State\Provider\ReadProvider;
19+
use ApiPlatform\State\ProviderInterface;
20+
use PHPUnit\Framework\TestCase;
21+
use Symfony\Component\HttpFoundation\Request;
22+
23+
class ReadProviderTest extends TestCase
24+
{
25+
public function testSetsSerializerContext(): void
26+
{
27+
$data = new \stdClass();
28+
$operation = new Get(read: true);
29+
$decorated = $this->createStub(ProviderInterface::class);
30+
$decorated->method('provide')->willReturn($data);
31+
$serializerContextBuilder = $this->createMock(SerializerContextBuilderInterface::class);
32+
$serializerContextBuilder->expects($this->once())->method('createFromRequest')->willReturn(['a']);
33+
$provider = new ReadProvider($decorated, $serializerContextBuilder);
34+
$request = new Request();
35+
$provider->provide($operation, ['id' => 1], ['request' => $request]);
36+
$this->assertEquals(['a'], $request->attributes->get('_api_normalization_context'));
37+
}
38+
}

src/State/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"symfony/web-link": "^6.4 || ^7.0",
3737
"symfony/http-foundation": "^6.4 || 7.0",
3838
"willdurand/negotiation": "^3.1",
39-
"api-platform/validator": "*@dev || ^3.1"
39+
"api-platform/validator": "*@dev || ^3.1",
40+
"api-platform/serializer": "*@dev || ^3.1"
4041
},
4142
"autoload": {
4243
"psr-4": {

0 commit comments

Comments
 (0)