Skip to content
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

[Laravel] Model property casting to Enum is not working #6906

Open
cay89 opened this issue Jan 10, 2025 · 1 comment
Open

[Laravel] Model property casting to Enum is not working #6906

cay89 opened this issue Jan 10, 2025 · 1 comment

Comments

@cay89
Copy link
Contributor

cay89 commented Jan 10, 2025

API Platform version(s) affected: 4.0.12

Description
I have a model with a status property what I'm trying to cast to Enum:

enum TenantStatus: string
{
    case PENDING = 'pending';
    case ACTIVATION_IN_PROGRESS = 'activation_in_progress';
    case ACTIVE = 'active';
    case RESTRICTED = 'restricted';
    case DELETION_IN_PROGRESS = 'deletion_in_progress';
}
#[ApiResource(...)]
class Tenant extends AbstractModel
{

    // ...

    protected $casts = [
        'status' => TenantStatus::class,
    ];
}

but in the response the status will be []:

image

If I remove the casting from the model, the status display properly:

image

Additional Context

I traced the error back to AbstractItemNormalizer. It seems like something is going wrong here somewhere.

ApiPlatform\Serializer\AbstractItemNormalizer::getAttributeValue()
image

@toitzi
Copy link
Contributor

toitzi commented Jan 10, 2025

When you cast something as an enum, Laravel does the transformation for you, but in the database it is still a string, hence it is working when you remove the cast since it is only string.

When you have the cast enabled, the string is automatically converted back to you enum by laravel on read operations. Now API Platform tries to normalize it, but does not know how since there is no backed enum normalizer, see here.

Since Symfony now also comes with a BackedEnumNormalizer, i think it is fine to add it. I will open a PR and we will see.

But in the meantime you can archive this yourself by putting this into your AppServiceProvider:

use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;

public function register(): void
{
    $this->app->extend('api_platform_normalizer_list', function (\SplPriorityQueue $list, Application $app) {
        $list->insert($app->make(BackedEnumNormalizer::class), -905);
        return $list;
    });
}

toitzi added a commit to toitzi/core that referenced this issue Jan 10, 2025
toitzi added a commit to toitzi/core that referenced this issue Jan 13, 2025
toitzi added a commit to toitzi/core that referenced this issue Jan 13, 2025
toitzi added a commit to toitzi/core that referenced this issue Jan 13, 2025
toitzi added a commit to toitzi/core that referenced this issue Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants