Skip to content

Commit

Permalink
Throw an error when no identifier is defined (#3871)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Dec 2, 2020
1 parent 1494ae8 commit b5be5e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Api/IdentifiersExtractor.php
Expand Up @@ -57,6 +57,10 @@ public function getIdentifiersFromResourceClass(string $resourceClass): array
}
}

if (!$identifiers) {
throw new RuntimeException(sprintf('No identifier defined "%s". You should add #[\ApiPlatform\Core\Annotation\ApiProperty(identifier: true)]" on the property identifying the resource."', $resourceClass));
}

return $identifiers;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Api/IdentifiersExtractorTest.php
Expand Up @@ -202,6 +202,20 @@ public function testGetsIdentifiersFromCorrectResourceClass(): void
$this->assertSame(['foo' => 'woot'], $identifiersExtractor->getIdentifiersFromItem($item));
}

public function testNoIdentifiers(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('No identifier defined "ApiPlatform\\Core\\Tests\\Fixtures\\TestBundle\\Entity\\Dummy". You should add #[\ApiPlatform\Core\Annotation\ApiProperty(identifier: true)]" on the property identifying the resource.');
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class)->willReturn(new PropertyNameCollection(['foo']));
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'foo')->willReturn(new PropertyMetadata());
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());

$identifiersExtractor->getIdentifiersFromResourceClass(Dummy::class);
}

/**
* @group legacy
* @expectedDeprecation Not injecting ApiPlatform\Core\Api\ResourceClassResolverInterface in the IdentifiersExtractor might introduce cache issues with object identifiers.
Expand Down
Expand Up @@ -13,13 +13,19 @@

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource
*/
final class DummyForAdditionalFieldsInput
{
/**
* @ApiProperty(identifier=true)
*/
public $id;

private $dummyName;

public function __construct(string $dummyName)
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/TestBundle/Model/ResourceBarInterface.php
Expand Up @@ -13,7 +13,12 @@

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Model;

use ApiPlatform\Core\Annotation\ApiProperty;

interface ResourceBarInterface
{
/**
* @ApiProperty(identifier=true)
*/
public function getBar(): ?string;
}

0 comments on commit b5be5e0

Please sign in to comment.