-
-
Notifications
You must be signed in to change notification settings - Fork 903
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
soyuka
merged 2 commits into
api-platform:4.1
from
MeronNagy:backed-enum-filter-support-integer-backed-enums-4.1
May 9, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
tests/Fixtures/TestBundle/Entity/Issue7126/DummyForBackedEnumFilter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
tests/Fixtures/TestBundle/Entity/Issue7126/IntegerBackedEnum.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
tests/Fixtures/TestBundle/Entity/Issue7126/StringBackedEnum.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.