From b01f6235cd3f3d15396f0ca1c91ad810729c04de Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 22 Nov 2024 11:07:14 +0100 Subject: [PATCH] fix(symfony): unset item_uri_template when serializing an error fixes #6718 --- src/Symfony/EventListener/ErrorListener.php | 4 ++ .../ApiResource/Issue6718/Organization.php | 56 +++++++++++++++++++ tests/Fixtures/app/AppKernel.php | 4 +- tests/Functional/ItemUriTemplateTest.php | 28 ++++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 tests/Fixtures/TestBundle/ApiResource/Issue6718/Organization.php create mode 100644 tests/Functional/ItemUriTemplateTest.php diff --git a/src/Symfony/EventListener/ErrorListener.php b/src/Symfony/EventListener/ErrorListener.php index cc37939e898..9a4e5a31ce9 100644 --- a/src/Symfony/EventListener/ErrorListener.php +++ b/src/Symfony/EventListener/ErrorListener.php @@ -115,6 +115,10 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re $normalizationContext += ['api_error_resource' => true]; } + if (isset($normalizationContext['item_uri_template'])) { + unset($normalizationContext['item_uri_template']); + } + if (!isset($normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES])) { $normalizationContext[AbstractObjectNormalizer::IGNORED_ATTRIBUTES] = ['trace', 'file', 'line', 'code', 'message', 'traceAsString']; } diff --git a/tests/Fixtures/TestBundle/ApiResource/Issue6718/Organization.php b/tests/Fixtures/TestBundle/ApiResource/Issue6718/Organization.php new file mode 100644 index 00000000000..9acd267bfb2 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/Issue6718/Organization.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6718; + +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\Operation; + +#[ApiResource( + shortName: 'OrganisationIssue6718', + extraProperties: ['rfc_7807_compliant_errors' => true], + operations: [ + new Get( + uriTemplate: '/6718_organisations/{id}', + provider: [self::class, 'itemProvider'], + ), + new Get( + uriTemplate: '/6718_users/{userId}/organisation', + uriVariables: [ + 'userId', + ], + normalizationContext: [ + 'item_uri_template' => '/6718_organisations/{id}', + 'hydra_prefix' => false, + ], + provider: [self::class, 'userOrganizationItemProvider'] + ), + ], +)] +class Organization +{ + public function __construct(public readonly string $id) + { + } + + public static function itemProvider(Operation $operation, array $uriVariables = []): ?self + { + return new self($uriVariables['id']); + } + + public static function userOrganizationItemProvider(): null + { + return null; + } +} diff --git a/tests/Fixtures/app/AppKernel.php b/tests/Fixtures/app/AppKernel.php index b842bbc8c47..a6e7a932b35 100644 --- a/tests/Fixtures/app/AppKernel.php +++ b/tests/Fixtures/app/AppKernel.php @@ -119,7 +119,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load 'session' => class_exists(SessionFactory::class) ? ['storage_factory_id' => 'session.storage.factory.mock_file'] + $cookie : ['storage_id' => 'session.storage.mock_file'] + $cookie, 'profiler' => [ 'enabled' => true, - 'collect' => false, + 'collect' => true, ], 'php_errors' => ['log' => true], 'messenger' => $messengerConfig, @@ -138,7 +138,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load 'session' => class_exists(SessionFactory::class) ? ['storage_factory_id' => 'session.storage.factory.mock_file'] : ['storage_id' => 'session.storage.mock_file'], 'profiler' => [ 'enabled' => true, - 'collect' => false, + 'collect' => true, ], 'messenger' => $messengerConfig, 'router' => ['utf8' => true], diff --git a/tests/Functional/ItemUriTemplateTest.php b/tests/Functional/ItemUriTemplateTest.php new file mode 100644 index 00000000000..f1f1812445b --- /dev/null +++ b/tests/Functional/ItemUriTemplateTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Tests\Functional; + +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; + +class ItemUriTemplateTest extends ApiTestCase +{ + public function testIssue6718(): void + { + self::createClient()->request('GET', '/6718_users/1/organisation', [ + 'headers' => ['accept' => 'application/ld+json'], + ]); + $this->assertResponseStatusCodeSame(404); + $this->assertJsonContains(['description' => 'Not Found']); + } +}