Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Metadata/IdentifiersExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/Metadata/Tests/IdentifiersExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading