Skip to content

Commit 4247162

Browse files
Merge pull request #4880 from vincentchalamon/fix/id-detection
Do not auto-set id as identifier if it is set as identifier=false
2 parents 4ad55f6 + 86b03ce commit 4247162

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/Metadata/Resource/Factory/LinkFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,13 @@ private function getIdentifiersFromResourceClass(string $resourceClass): array
133133
$hasIdProperty = false;
134134
$identifiers = [];
135135
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
136-
if (!$hasIdProperty) {
136+
$isIdentifier = $this->propertyMetadataFactory->create($resourceClass, $property)->isIdentifier();
137+
138+
if (!$hasIdProperty && null === $isIdentifier) {
137139
$hasIdProperty = 'id' === $property;
138140
}
139141

140-
if ($this->propertyMetadataFactory->create($resourceClass, $property)->isIdentifier() ?? false) {
142+
if ($isIdentifier) {
141143
$identifiers[] = $property;
142144
}
143145
}

tests/Metadata/Resource/Factory/LinkFactoryTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ final class LinkFactoryTest extends TestCase
3939
/**
4040
* @dataProvider provideCreateLinksFromIdentifiersCases
4141
*/
42-
public function testCreateLinksFromIdentifiers(array $propertyNames, bool $compositeIdentifier, array $expectedLinks): void
42+
public function testCreateLinksFromIdentifiers(array $propertyNames, bool $compositeIdentifier, array $expectedLinks, ?bool $idAsIdentifier = null): void
4343
{
4444
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
4545
$propertyNameCollectionFactoryProphecy->create(Argument::cetera())->willReturn(new PropertyNameCollection($propertyNames));
4646
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
47-
$propertyMetadataFactoryProphecy->create(AttributeResource::class, 'id')->willReturn(new ApiProperty());
47+
$propertyMetadataFactoryProphecy->create(AttributeResource::class, 'id')->willReturn(null === $idAsIdentifier ? new ApiProperty() : new ApiProperty(identifier: $idAsIdentifier));
4848
$propertyMetadataFactoryProphecy->create(AttributeResource::class, 'name')->willReturn((new ApiProperty())->withIdentifier(true));
4949
$propertyMetadataFactoryProphecy->create(AttributeResource::class, 'slug')->willReturn(new ApiProperty());
5050
$propertyMetadataFactoryProphecy->create(AttributeResource::class, 'composite1')->willReturn((new ApiProperty())->withIdentifier(true));
@@ -65,11 +65,23 @@ public function provideCreateLinksFromIdentifiersCases(): \Generator
6565
'compositeIdentifier' => false,
6666
[],
6767
];
68-
yield 'no identifiers' => [
68+
yield 'id detected as identifier' => [
6969
['id'],
7070
'compositeIdentifier' => false,
7171
[(new Link())->withFromClass(AttributeResource::class)->withParameterName('id')->withIdentifiers(['id'])],
7272
];
73+
yield 'id forced as identifier' => [
74+
['id'],
75+
'compositeIdentifier' => false,
76+
[(new Link())->withFromClass(AttributeResource::class)->withParameterName('id')->withIdentifiers(['id'])],
77+
true,
78+
];
79+
yield 'id forced as no identifier' => [
80+
['id'],
81+
'compositeIdentifier' => false,
82+
[],
83+
false,
84+
];
7385
yield 'name identifier' => [
7486
['id', 'name'],
7587
'compositeIdentifier' => false,

0 commit comments

Comments
 (0)