Skip to content

Commit 5c7e0d2

Browse files
authored
fix(symfony): error wrongly inherit normalization context (#6939)
1 parent d5b48b1 commit 5c7e0d2

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

src/Symfony/EventListener/ErrorListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
9494
$operation = $operation->withProvider('api_platform.state.error_provider');
9595
}
9696

97-
$normalizationContext = ($operation->getNormalizationContext() ?? []) + ($apiOperation?->getNormalizationContext() ?? []);
97+
$normalizationContext = $operation->getNormalizationContext() ?? [];
9898
if (!($normalizationContext['api_error_resource'] ?? false)) {
9999
$normalizationContext += ['api_error_resource' => true];
100100
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Tests\Fixtures\TestBundle\ApiResource\Issue6926;
15+
16+
use ApiPlatform\Metadata\ErrorResource;
17+
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
18+
19+
#[ErrorResource]
20+
class Error extends \Exception implements ProblemExceptionInterface
21+
{
22+
public function getType(): string
23+
{
24+
return '/errors/demo-error';
25+
}
26+
27+
public function getTitle(): ?string
28+
{
29+
return 'Demo error';
30+
}
31+
32+
public function getStatus(): ?int
33+
{
34+
return 400;
35+
}
36+
37+
public function getDetail(): ?string
38+
{
39+
return 'This should be returned in the response.';
40+
}
41+
42+
public function getInstance(): ?string
43+
{
44+
return null;
45+
}
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Tests\Fixtures\TestBundle\ApiResource\Issue6926;
15+
16+
use ApiPlatform\Metadata\Post;
17+
use Symfony\Component\Serializer\Attribute\Groups;
18+
19+
#[Post(
20+
uriTemplate: '/issue6926',
21+
processor: [self::class, 'process'],
22+
denormalizationContext: ['groups' => ['login_request:write']],
23+
normalizationContext: ['groups' => ['login_request:read']],
24+
errors: [Error::class]
25+
)]
26+
class ThrowsAnExceptionWithGroup
27+
{
28+
#[Groups('login_request:read')]
29+
private ?string $id = null;
30+
31+
public function getId(): ?string
32+
{
33+
return $this->id;
34+
}
35+
36+
public static function process(): void
37+
{
38+
throw new Error();
39+
}
40+
}
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\Tests\Functional\Issues;
15+
16+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6926\Error;
18+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6926\ThrowsAnExceptionWithGroup;
19+
use ApiPlatform\Tests\SetupClassResourcesTrait;
20+
21+
class Issue6926Test extends ApiTestCase
22+
{
23+
use SetupClassResourcesTrait;
24+
25+
/**
26+
* @return class-string[]
27+
*/
28+
public static function getResources(): array
29+
{
30+
return [ThrowsAnExceptionWithGroup::class, Error::class];
31+
}
32+
33+
public function testWithGroupFilter(): void
34+
{
35+
$response = self::createClient()->request('POST', '/issue6926', ['json' => []]);
36+
$this->assertEquals('This should be returned in the response.', $response->toArray(false)['detail'] ?? false);
37+
}
38+
}

0 commit comments

Comments
 (0)