Skip to content

Commit

Permalink
bug #54315 [Serializer] Fixed BackedEnumNormalizer priority for trans…
Browse files Browse the repository at this point in the history
…latable enum (IndraGunawan)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[Serializer] Fixed BackedEnumNormalizer priority for translatable enum

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

```php
enum MyEnum: string implements TranslatableInterface
{
    case Get = 'GET';
    case Post = 'POST';
    case Update = 'UPDATE';

    public function trans(TranslatorInterface $translator, ?string $locale = null): string
    {
        return match ($this) {
            self::Get  => 'custom_get',
            self::Post  => 'custom_post',
            self::Update => 'custom_update'
        };
    }
}
```

```php
class MyController {
    public function __invoke(SerializerInterface $serializer) {
        dd($serializer->serialize(MyEnum::Get)); // ""custom_get"" , Expected result: ""GET""
    }
}
```

serialize a BackedEnum that implements `TranslatableInterface` will return the translation value instead of the enum item value. this is because `TranslatableNormalizer` ([ref](https://github.com/symfony/symfony/blob/7.1/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php#L116-L118)) has higher priority than `BackedEnumNormalizer`

this PR changes the `BackedEnumNormalizer` priority higher than  `TranslatableNormalizer` priority

Commits
-------

42fde94 [Serializer] Fixed BackedEnumNormalizer priority for translatable enum
  • Loading branch information
chalasr committed Mar 23, 2024
2 parents 5e4f67e + 42fde94 commit c87e4a1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,6 @@
])

->set('serializer.normalizer.backed_enum', BackedEnumNormalizer::class)
->tag('serializer.normalizer', ['priority' => -915])
->tag('serializer.normalizer', ['priority' => -880])
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

enum TranslatableBackedEnum: string implements TranslatableInterface
{
case Get = 'GET';

public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return match ($this) {
self::Get => 'custom_get_string',
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\TranslatableBackedEnum;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
Expand Down Expand Up @@ -67,6 +69,15 @@ public static function provideNormalizersAndEncodersWithDefaultContextOption():
['serializer.encoder.csv.alias'],
];
}

public function testSerializeTranslatableBackedEnum()
{
static::bootKernel(['test_case' => 'Serializer']);

$serializer = static::getContainer()->get('serializer.alias');

$this->assertEquals('GET', $serializer->serialize(TranslatableBackedEnum::Get, 'yaml'));
}
}

class Foo
Expand Down

0 comments on commit c87e4a1

Please sign in to comment.