-
-
Notifications
You must be signed in to change notification settings - Fork 841
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
feat: BackedEnum resources #6309
Open
GwendolenLynch
wants to merge
5
commits into
api-platform:main
Choose a base branch
from
GwendolenLynch:feat/enum-resources
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d0f1f4a
fix(metadata): Only add GET operations for enums when ApiResource doe…
GwendolenLynch 995b6b6
feat(state): backed enum provider
GwendolenLynch 8771988
fix(metadata): enum resource identifier default to value
GwendolenLynch cf128d2
fix(metadata): get method metadata for BackedEnums
GwendolenLynch a069f68
test: resource with enum properties schema
GwendolenLynch File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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\State\Provider; | ||
|
||
use ApiPlatform\Metadata\CollectionOperationInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
final class BackedEnumProvider implements ProviderInterface | ||
{ | ||
public function __construct(private ProviderInterface $decorated) | ||
{ | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
$resourceClass = $operation->getClass(); | ||
if (!$resourceClass || !is_a($resourceClass, \BackedEnum::class, true)) { | ||
return $this->decorated->provide($operation, $uriVariables, $context); | ||
} | ||
|
||
if ($operation instanceof CollectionOperationInterface) { | ||
return $resourceClass::cases(); | ||
} | ||
|
||
$id = $uriVariables['id'] ?? null; | ||
if (null === $id) { | ||
throw new NotFoundHttpException('Not Found'); | ||
} | ||
|
||
if ($enum = $this->resolveEnum($resourceClass, $id)) { | ||
return $enum; | ||
} | ||
|
||
throw new NotFoundHttpException('Not Found'); | ||
} | ||
|
||
/** | ||
* @param class-string $resourceClass | ||
*/ | ||
private function resolveEnum(string $resourceClass, string|int $id): ?\BackedEnum | ||
{ | ||
$reflectEnum = new \ReflectionEnum($resourceClass); | ||
$type = (string) $reflectEnum->getBackingType(); | ||
|
||
if ('int' === $type) { | ||
if (!is_numeric($id)) { | ||
return null; | ||
} | ||
$enum = $resourceClass::tryFrom((int) $id); | ||
} else { | ||
$enum = $resourceClass::tryFrom($id); | ||
} | ||
|
||
// @deprecated enums will be indexable only by value in 4.0 | ||
$enum ??= array_reduce($resourceClass::cases(), static fn ($c, \BackedEnum $case) => $id === $case->name ? $case : $c, null); | ||
|
||
return $enum; | ||
} | ||
} |
This file contains 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
This file contains 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
33 changes: 33 additions & 0 deletions
33
tests/Fixtures/TestBundle/ApiResource/BackedEnumIntegerResource.php
This file contains 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,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
#[ApiResource] | ||
enum BackedEnumIntegerResource: int | ||
{ | ||
case Yes = 1; | ||
case No = 2; | ||
case Maybe = 3; | ||
|
||
public function getDescription(): string | ||
{ | ||
return match ($this) { | ||
self::Yes => 'We say yes', | ||
self::No => 'Computer says no', | ||
self::Maybe => 'Let me think about it', | ||
}; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Fixtures/TestBundle/ApiResource/BackedEnumStringResource.php
This file contains 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,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
#[ApiResource] | ||
enum BackedEnumStringResource: string | ||
{ | ||
case Yes = 'yes'; | ||
case No = 'no'; | ||
case Maybe = 'maybe'; | ||
|
||
public function getDescription(): string | ||
{ | ||
return match ($this) { | ||
self::Yes => 'We say yes', | ||
self::No => 'Computer says no', | ||
self::Maybe => 'Let me think about it', | ||
}; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/Fixtures/TestBundle/ApiResource/Issue6317/Issue6317.php
This file contains 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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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\ApiResource\Issue6317; | ||
|
||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
#[ApiResource] | ||
enum Issue6317: int | ||
{ | ||
case First = 1; | ||
case Second = 2; | ||
|
||
#[ApiProperty(identifier: true, example: 'An example of an ID')] | ||
public function getId(): int | ||
{ | ||
return $this->value; | ||
} | ||
|
||
#[ApiProperty(jsonSchemaContext: ['example' => '/lisa/mary'])] | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
#[ApiProperty(jsonldContext: ['example' => '24'])] | ||
public function getOrdinal(): string | ||
{ | ||
return 1 === $this->value ? '1st' : '2nd'; | ||
} | ||
|
||
#[ApiProperty(openapiContext: ['example' => '42'])] | ||
public function getCardinal(): int | ||
{ | ||
return $this->value; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/Fixtures/TestBundle/ApiResource/ResourceWithEnumProperty.php
This file contains 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,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* 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\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Enum\GenderTypeEnum; | ||
|
||
#[ApiResource()] | ||
#[Get( | ||
provider: self::class.'::providerItem', | ||
)] | ||
#[GetCollection( | ||
provider: self::class.'::providerCollection', | ||
)] | ||
class ResourceWithEnumProperty | ||
{ | ||
public int $id = 1; | ||
|
||
public ?BackedEnumIntegerResource $intEnum = null; | ||
|
||
/** @var BackedEnumStringResource[] */ | ||
public array $stringEnum = []; | ||
|
||
public ?GenderTypeEnum $gender = null; | ||
|
||
/** @var GenderTypeEnum[] */ | ||
public array $genders = []; | ||
|
||
public static function providerItem(Operation $operation, array $uriVariables): self | ||
{ | ||
$self = new self(); | ||
$self->intEnum = BackedEnumIntegerResource::Yes; | ||
$self->stringEnum = [BackedEnumStringResource::Maybe, BackedEnumStringResource::No]; | ||
$self->gender = GenderTypeEnum::FEMALE; | ||
$self->genders = [GenderTypeEnum::FEMALE, GenderTypeEnum::MALE]; | ||
|
||
return $self; | ||
} | ||
|
||
public static function providerCollection(Operation $operation, array $uriVariables): array | ||
{ | ||
return [self::providerItem($operation, $uriVariables)]; | ||
} | ||
} |
Oops, something went wrong.
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.
Does this match what you had in mind @soyuka?
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.
Uri variable should be as followed:
in the metadata directly so that we don't need any condition here right?
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.
Apologies, but I'm confused again 😇 … I'm probably missing something obvious, but the
Link
is constructed directly above, and theparameterName
is set at return:$link->withParameterName($parameterName)
… So is your suggestion how that return array should be shaped? If so what about non-enums?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.
Which raises another thing about thecreateLinksFromIdentifiers
method … If the$identifier
array is empty there is an early return (lines 56-58), and then there is theif (1 < \count($identifiers)) {
(lines 66-69) test that looks like dead code, correct?Scrap this one, I was reading the comparison backwards in my head … confusion reigns today 😟
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 need to check this into the details, will try to find some time!