Skip to content

Commit 8f552bb

Browse files
committed
fix(symfony): unset item_uri_template when serializing an error
fixes #6718
1 parent d64ba84 commit 8f552bb

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

src/Symfony/EventListener/ErrorListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
115115
$normalizationContext += ['api_error_resource' => true];
116116
}
117117

118+
if (isset($normalizationContext['item_uri_template'])) {
119+
unset($normalizationContext['item_uri_template']);
120+
}
121+
118122
if (!isset($normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES])) {
119123
$normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES] = ['trace', 'file', 'line', 'code', 'message', 'traceAsString'];
120124
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Issue6718;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\Operation;
19+
20+
#[ApiResource(
21+
shortName: 'OrganisationIssue6718',
22+
extraProperties: ['rfc_7807_compliant_errors' => true],
23+
operations: [
24+
new Get(
25+
uriTemplate: '/6718_organisations/{id}',
26+
provider: [self::class, 'itemProvider'],
27+
),
28+
new Get(
29+
uriTemplate: '/6718_users/{userId}/organisation',
30+
uriVariables: [
31+
'userId',
32+
],
33+
normalizationContext: [
34+
'item_uri_template' => '/6718_organisations/{id}',
35+
'hydra_prefix' => false,
36+
],
37+
provider: [self::class, 'userOrganizationItemProvider']
38+
),
39+
],
40+
)]
41+
class Organization
42+
{
43+
public function __construct(public readonly string $id)
44+
{
45+
}
46+
47+
public static function itemProvider(Operation $operation, array $uriVariables = []): ?self
48+
{
49+
return new self($uriVariables['id']);
50+
}
51+
52+
public static function userOrganizationItemProvider(): null
53+
{
54+
return null;
55+
}
56+
}

tests/Fixtures/app/AppKernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
119119
'session' => class_exists(SessionFactory::class) ? ['storage_factory_id' => 'session.storage.factory.mock_file'] + $cookie : ['storage_id' => 'session.storage.mock_file'] + $cookie,
120120
'profiler' => [
121121
'enabled' => true,
122-
'collect' => false,
122+
'collect' => true,
123123
],
124124
'php_errors' => ['log' => true],
125125
'messenger' => $messengerConfig,
@@ -138,7 +138,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
138138
'session' => class_exists(SessionFactory::class) ? ['storage_factory_id' => 'session.storage.factory.mock_file'] : ['storage_id' => 'session.storage.mock_file'],
139139
'profiler' => [
140140
'enabled' => true,
141-
'collect' => false,
141+
'collect' => true,
142142
],
143143
'messenger' => $messengerConfig,
144144
'router' => ['utf8' => true],

tests/Functional/JsonLd.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Doctrine\ORM\EntityManagerInterface;
2020
use Doctrine\ORM\Tools\SchemaTool;
2121

22-
class JsonLd extends ApiTestCase
22+
class JsonLdTest extends ApiTestCase
2323
{
2424
/**
2525
* The input DTO denormalizes an existing Doctrine entity.
@@ -88,4 +88,13 @@ protected function tearDown(): void
8888
@$schemaTool->dropSchema($classes);
8989
parent::tearDown();
9090
}
91+
92+
public function testIssue6718(): void
93+
{
94+
self::createClient()->request('GET', '/6718_users/1/organisation', [
95+
'headers' => ['accept' => 'application/ld+json'],
96+
]);
97+
$this->assertResponseStatusCodeSame(404);
98+
$this->assertJsonContains(['description' => 'Not Found']);
99+
}
91100
}

0 commit comments

Comments
 (0)