Skip to content

Commit 964b7d2

Browse files
authored
fix(metadata): guard toProperty read in IdentifiersExtractor (#8919)
1 parent bdf1cab commit 964b7d2

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎src/Metadata/IdentifiersExtractor.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
2323
use ApiPlatform\Metadata\Util\TypeHelper;
2424
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
25+
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
2526
use Symfony\Component\PropertyAccess\PropertyAccess;
2627
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2728

@@ -122,7 +123,11 @@ private function getIdentifierValue(object $item, string $class, string $propert
122123
}
123124

124125
if ($toProperty) {
125-
return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
126+
try {
127+
return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
128+
} catch (NoSuchPropertyException|UnexpectedTypeException $e) {
129+
throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
130+
}
126131
}
127132

128133
$resourceClass = $this->getResourceClass($item, true);
@@ -144,7 +149,7 @@ private function getIdentifierValue(object $item, string $class, string $propert
144149
if ($type->isIdentifiedBy($class)) {
145150
return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
146151
}
147-
} catch (NoSuchPropertyException $e) {
152+
} catch (NoSuchPropertyException|UnexpectedTypeException $e) {
148153
throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
149154
}
150155
}

‎src/Metadata/Tests/IdentifiersExtractorTest.php‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Metadata\Tests;
1515

16+
use ApiPlatform\Metadata\Exception\RuntimeException;
1617
use ApiPlatform\Metadata\Get;
1718
use ApiPlatform\Metadata\HttpOperation;
1819
use ApiPlatform\Metadata\IdentifiersExtractor;
@@ -24,6 +25,7 @@
2425
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Dummy;
2526
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\DummyWithEnumIdentifier;
2627
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\RelationMultiple;
28+
use PHPUnit\Framework\Attributes\DataProvider;
2729
use PHPUnit\Framework\TestCase;
2830
use Prophecy\Argument;
2931
use Prophecy\PhpUnit\ProphecyTrait;
@@ -174,6 +176,46 @@ public function testGetIdentifiersFromItemWithToProperty(): void
174176
);
175177
}
176178

179+
public static function unreadableToPropertyProvider(): iterable
180+
{
181+
yield 'missing property' => ['missing'];
182+
yield 'null relation' => ['first'];
183+
}
184+
185+
#[DataProvider('unreadableToPropertyProvider')]
186+
public function testGetIdentifiersFromItemWithUnreadableToPropertyThrowsRuntimeException(string $toProperty): void
187+
{
188+
$resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class);
189+
$resourceClassResolver->method('isResourceClass')->willReturn(true);
190+
$resourceClassResolver->method('getResourceClass')->willReturn(RelationMultiple::class);
191+
192+
$identifiersExtractor = new IdentifiersExtractor(
193+
$this->createStub(ResourceMetadataCollectionFactoryInterface::class),
194+
$resourceClassResolver,
195+
$this->createStub(PropertyNameCollectionFactoryInterface::class),
196+
$this->createStub(PropertyMetadataFactoryInterface::class)
197+
);
198+
199+
$operation = (new Get())
200+
->withUriTemplate('/resources/{firstId}')
201+
->withUriVariables([
202+
'firstId' => (new Link())
203+
->withFromClass(Dummy::class)
204+
->withToProperty($toProperty)
205+
->withIdentifiers(['id'])
206+
->withParameterName('firstId'),
207+
])
208+
->withClass(RelationMultiple::class);
209+
210+
$item = new RelationMultiple();
211+
$item->id = 1;
212+
213+
$this->expectException(RuntimeException::class);
214+
$this->expectExceptionMessage('Not able to retrieve identifiers.');
215+
216+
$identifiersExtractor->getIdentifiersFromItem($item, $operation);
217+
}
218+
177219
public function testGetIdentifierWithEnumValues(): void
178220
{
179221
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);

0 commit comments

Comments
 (0)