From 39c3fa9bfa25c30eac2b62c7ddb89b300c441f5a Mon Sep 17 00:00:00 2001 From: soyuka Date: Tue, 24 Nov 2026 11:04:20 +0100 Subject: [PATCH] fix(metadata): guard toProperty read in IdentifiersExtractor --- src/Metadata/IdentifiersExtractor.php | 9 +++- .../Tests/IdentifiersExtractorTest.php | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Metadata/IdentifiersExtractor.php b/src/Metadata/IdentifiersExtractor.php index 3b3015a6fc9..cf093c48a63 100644 --- a/src/Metadata/IdentifiersExtractor.php +++ b/src/Metadata/IdentifiersExtractor.php @@ -22,6 +22,7 @@ use ApiPlatform\Metadata\Util\ResourceClassInfoTrait; use ApiPlatform\Metadata\Util\TypeHelper; use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; +use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; @@ -122,7 +123,11 @@ private function getIdentifierValue(object $item, string $class, string $propert } if ($toProperty) { - return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName); + try { + return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName); + } catch (NoSuchPropertyException|UnexpectedTypeException $e) { + throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e); + } } $resourceClass = $this->getResourceClass($item, true); @@ -144,7 +149,7 @@ private function getIdentifierValue(object $item, string $class, string $propert if ($type->isIdentifiedBy($class)) { return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName); } - } catch (NoSuchPropertyException $e) { + } catch (NoSuchPropertyException|UnexpectedTypeException $e) { throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e); } } diff --git a/src/Metadata/Tests/IdentifiersExtractorTest.php b/src/Metadata/Tests/IdentifiersExtractorTest.php index 9693e3499b1..6879d57c4aa 100644 --- a/src/Metadata/Tests/IdentifiersExtractorTest.php +++ b/src/Metadata/Tests/IdentifiersExtractorTest.php @@ -13,6 +13,7 @@ namespace ApiPlatform\Metadata\Tests; +use ApiPlatform\Metadata\Exception\RuntimeException; use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\HttpOperation; use ApiPlatform\Metadata\IdentifiersExtractor; @@ -24,6 +25,7 @@ use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Dummy; use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\DummyWithEnumIdentifier; use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\RelationMultiple; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -174,6 +176,46 @@ public function testGetIdentifiersFromItemWithToProperty(): void ); } + public static function unreadableToPropertyProvider(): iterable + { + yield 'missing property' => ['missing']; + yield 'null relation' => ['first']; + } + + #[DataProvider('unreadableToPropertyProvider')] + public function testGetIdentifiersFromItemWithUnreadableToPropertyThrowsRuntimeException(string $toProperty): void + { + $resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class); + $resourceClassResolver->method('isResourceClass')->willReturn(true); + $resourceClassResolver->method('getResourceClass')->willReturn(RelationMultiple::class); + + $identifiersExtractor = new IdentifiersExtractor( + $this->createStub(ResourceMetadataCollectionFactoryInterface::class), + $resourceClassResolver, + $this->createStub(PropertyNameCollectionFactoryInterface::class), + $this->createStub(PropertyMetadataFactoryInterface::class) + ); + + $operation = (new Get()) + ->withUriTemplate('/resources/{firstId}') + ->withUriVariables([ + 'firstId' => (new Link()) + ->withFromClass(Dummy::class) + ->withToProperty($toProperty) + ->withIdentifiers(['id']) + ->withParameterName('firstId'), + ]) + ->withClass(RelationMultiple::class); + + $item = new RelationMultiple(); + $item->id = 1; + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Not able to retrieve identifiers.'); + + $identifiersExtractor->getIdentifiersFromItem($item, $operation); + } + public function testGetIdentifierWithEnumValues(): void { $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);