Skip to content

fix(doctrine): support integer-backed enums in BackedEnumFilter #7127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
10 changes: 9 additions & 1 deletion src/Doctrine/Common/Filter/BackedEnumFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait BackedEnumFilterTrait
use PropertyHelperTrait;

/**
* @var array<string, string>
* @var array<string, class-string>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe class-string to be more appropriate here, I can revert this if needed.

*/
private array $enumTypes;

Expand Down Expand Up @@ -80,6 +80,14 @@ abstract protected function isBackedEnumField(string $property, string $resource

private function normalizeValue($value, string $property): mixed
{
$firstCase = $this->enumTypes[$property]::cases()[0] ?? null;
if (
\is_int($firstCase?->value)
&& false !== filter_var($value, \FILTER_VALIDATE_INT)
) {
$value = (int) $value;
}

$values = array_map(fn (\BackedEnum $case) => $case->value, $this->enumTypes[$property]::cases());

if (\in_array($value, $values, true)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

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

use ApiPlatform\Doctrine\Orm\Filter\BackedEnumFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\ORM\Mapping as ORM;

#[GetCollection(
uriTemplate: 'backed_enum_filter{._format}',
)]
#[ApiFilter(BackedEnumFilter::class, properties: ['stringBackedEnum', 'integerBackedEnum'])]
#[ORM\Entity]
class DummyForBackedEnumFilter
{
/**
* @var int The id
*/
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;

#[ORM\Column(nullable: true, enumType: StringBackedEnum::class)]
private ?StringBackedEnum $stringBackedEnum = null;

#[ORM\Column(nullable: true, enumType: IntegerBackedEnum::class)]
private ?IntegerBackedEnum $integerBackedEnum = null;

public function getId(): ?int
{
return $this->id;
}

public function getStringBackedEnum(): ?StringBackedEnum
{
return $this->stringBackedEnum;
}

public function setStringBackedEnum(StringBackedEnum $stringBackedEnum): void
{
$this->stringBackedEnum = $stringBackedEnum;
}

public function getIntegerBackedEnum(): ?IntegerBackedEnum
{
return $this->integerBackedEnum;
}

public function setIntegerBackedEnum(IntegerBackedEnum $IntegerBackedEnum): void
{
$this->integerBackedEnum = $IntegerBackedEnum;
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Issue7126/IntegerBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

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

enum IntegerBackedEnum: int
{
case One = 1;
case Two = 2;
}
20 changes: 20 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Issue7126/StringBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

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

enum StringBackedEnum: string
{
case One = 'one';
case Two = 'two';
}
86 changes: 86 additions & 0 deletions tests/Functional/BackedEnumFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7126\DummyForBackedEnumFilter;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7126\IntegerBackedEnum;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7126\StringBackedEnum;
use ApiPlatform\Tests\RecreateSchemaTrait;
use ApiPlatform\Tests\SetupClassResourcesTrait;

final class BackedEnumFilterTest extends ApiTestCase
{
use RecreateSchemaTrait;
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [DummyForBackedEnumFilter::class];
}

public function testFilterStringBackedEnum(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped();
}

$this->recreateSchema($this->getResources());
$this->loadFixtures();
$route = 'backed_enum_filter';
$response = self::createClient()->request('GET', $route.'?stringBackedEnum='.StringBackedEnum::One->value);
$a = $response->toArray();
$this->assertCount(1, $a['hydra:member']);
$this->assertEquals(StringBackedEnum::One->value, $a['hydra:member'][0]['stringBackedEnum']);
}

public function testFilterIntegerBackedEnum(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped();
}

$this->recreateSchema($this->getResources());
$this->loadFixtures();
$route = 'backed_enum_filter';
$response = self::createClient()->request('GET', $route.'?integerBackedEnum='.IntegerBackedEnum::Two->value);
$a = $response->toArray();
$this->assertCount(1, $a['hydra:member']);
$this->assertEquals(IntegerBackedEnum::Two->value, $a['hydra:member'][0]['integerBackedEnum']);
}

public function loadFixtures(): void
{
$container = static::$kernel->getContainer();
$registry = $container->get('doctrine');
$manager = $registry->getManager();

$dummyOne = new DummyForBackedEnumFilter();
$dummyOne->setStringBackedEnum(StringBackedEnum::One);
$dummyOne->setIntegerBackedEnum(IntegerBackedEnum::One);
$manager->persist($dummyOne);

$dummyTwo = new DummyForBackedEnumFilter();
$dummyTwo->setStringBackedEnum(StringBackedEnum::Two);
$dummyTwo->setIntegerBackedEnum(IntegerBackedEnum::Two);
$manager->persist($dummyTwo);

$manager->flush();
}
}
Loading