Skip to content

Commit

Permalink
fix(metadata): enum resource identifier default to value
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Apr 12, 2024
1 parent 57d06a4 commit 8acce05
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Metadata/Resource/Factory/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function createLinksFromIdentifiers(Metadata $operation): array

$link = (new Link())->withFromClass($resourceClass)->withIdentifiers($identifiers);
$parameterName = $identifiers[0];
if ('value' === $parameterName && enum_exists($resourceClass)) {
$parameterName = 'id';
}

if (1 < \count($identifiers)) {
$parameterName = 'id';
Expand Down Expand Up @@ -155,6 +158,10 @@ private function getIdentifiersFromResourceClass(string $resourceClass): array
return ['id'];
}

if (!$hasIdProperty && !$identifiers && enum_exists($resourceClass)) {
return ['value'];
}

return $identifiers;
}

Expand Down
62 changes: 62 additions & 0 deletions tests/Functional/BackedEnumResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumIntegerResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\BackedEnumStringResource;
Expand Down Expand Up @@ -43,4 +44,65 @@ public function testOnlyGetOperationsAddedWhenNonSpecified(string $resourceClass

$this->assertInstanceOf($operationClass, $operations[$operationName]);
}

public function testEnumsAreAssignedValuePropertyAsIdentifierByDefault(): void
{
$linkFactory = self::getContainer()->get('api_platform.metadata.resource.link_factory');
$result = $linkFactory->completeLink(new Link(fromClass: BackedEnumIntegerResource::class));
$identifiers = $result->getIdentifiers();

$this->assertCount(1, $identifiers);
$this->assertNotContains('id', $identifiers);
$this->assertContains('value', $identifiers);
}

public function testCollection(): void
{
self::createClient()->request('GET', '/backed_enum_integer_resources', ['headers' => ['Accept' => 'application/json']]);

$this->assertResponseIsSuccessful();
$this->assertJsonEquals([
[
'name' => 'Yes',
'value' => 1,
'description' => 'We say yes',
],
[
'name' => 'No',
'value' => 2,
'description' => 'Computer says no',
],
[
'name' => 'Maybe',
'value' => 3,
'description' => 'Let me think about it',
],
]);
}

public function testItem(): void
{
self::createClient()->request('GET', '/backed_enum_integer_resources/1', ['headers' => ['Accept' => 'application/json']]);

$this->assertResponseIsSuccessful();
$this->assertJsonEquals([
'name' => 'Yes',
'value' => 1,
'description' => 'We say yes',
]);
}

public static function provider404s(): iterable
{
yield ['/backed_enum_integer_resources/42'];
yield ['/backed_enum_integer_resources/fortytwo'];
}

/** @dataProvider provider404s */
public function testItem404(string $uri): void
{
self::createClient()->request('GET', $uri);

$this->assertResponseStatusCodeSame(404);
}
}

0 comments on commit 8acce05

Please sign in to comment.