Skip to content

Commit 52064fa

Browse files
authored
Merge 918a001 into b81374f
2 parents b81374f + 918a001 commit 52064fa

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/Api/IdentifiersExtractor.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
5151
public function getIdentifiersFromResourceClass(string $resourceClass): array
5252
{
5353
$identifiers = [];
54-
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
54+
foreach ($properties = $this->propertyNameCollectionFactory->create($resourceClass) as $property) {
5555
if ($this->propertyMetadataFactory->create($resourceClass, $property)->isIdentifier() ?? false) {
5656
$identifiers[] = $property;
5757
}
5858
}
5959

6060
if (!$identifiers) {
61+
if (\in_array('id', iterator_to_array($properties), true)) {
62+
return ['id'];
63+
}
64+
6165
throw new RuntimeException(sprintf('No identifier defined "%s". You should add #[\ApiPlatform\Core\Annotation\ApiProperty(identifier: true)]" on the property identifying the resource."', $resourceClass));
6266
}
6367

@@ -71,14 +75,14 @@ public function getIdentifiersFromItem($item): array
7175
{
7276
$identifiers = [];
7377
$resourceClass = $this->getResourceClass($item, true);
78+
$identifierProperties = $this->getIdentifiersFromResourceClass($resourceClass);
7479

7580
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
76-
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
77-
$identifier = $propertyMetadata->isIdentifier();
78-
if (null === $identifier || false === $identifier) {
81+
if (!\in_array($propertyName, $identifierProperties, true)) {
7982
continue;
8083
}
8184

85+
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
8286
$identifier = $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName);
8387

8488
if (!\is_object($identifier)) {

tests/Api/IdentifiersExtractorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,16 @@ private function getMetadataFactoryProphecies($class, $identifiers, array $proph
252252

253253
return [$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy];
254254
}
255+
256+
public function testDefaultIdentifierId(): void
257+
{
258+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
259+
$propertyNameCollectionFactoryProphecy->create(Dummy::class)->willReturn(new PropertyNameCollection(['id']));
260+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
261+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->willReturn(new PropertyMetadata());
262+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
263+
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
264+
265+
$this->assertSame(['id'], $identifiersExtractor->getIdentifiersFromResourceClass(Dummy::class));
266+
}
255267
}

0 commit comments

Comments
 (0)