Skip to content

Commit c87e4a1

Browse files
committed
bug #54315 [Serializer] Fixed BackedEnumNormalizer priority for translatable 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
2 parents 5e4f67e + 42fde94 commit c87e4a1

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,6 @@
219219
])
220220

221221
->set('serializer.normalizer.backed_enum', BackedEnumNormalizer::class)
222-
->tag('serializer.normalizer', ['priority' => -915])
222+
->tag('serializer.normalizer', ['priority' => -880])
223223
;
224224
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures;
13+
14+
use Symfony\Contracts\Translation\TranslatableInterface;
15+
use Symfony\Contracts\Translation\TranslatorInterface;
16+
17+
enum TranslatableBackedEnum: string implements TranslatableInterface
18+
{
19+
case Get = 'GET';
20+
21+
public function trans(TranslatorInterface $translator, ?string $locale = null): string
22+
{
23+
return match ($this) {
24+
self::Get => 'custom_get_string',
25+
};
26+
}
27+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SerializerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
1313

14+
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\TranslatableBackedEnum;
15+
1416
/**
1517
* @author Kévin Dunglas <dunglas@gmail.com>
1618
*/
@@ -67,6 +69,15 @@ public static function provideNormalizersAndEncodersWithDefaultContextOption():
6769
['serializer.encoder.csv.alias'],
6870
];
6971
}
72+
73+
public function testSerializeTranslatableBackedEnum()
74+
{
75+
static::bootKernel(['test_case' => 'Serializer']);
76+
77+
$serializer = static::getContainer()->get('serializer.alias');
78+
79+
$this->assertEquals('GET', $serializer->serialize(TranslatableBackedEnum::Get, 'yaml'));
80+
}
7081
}
7182

7283
class Foo

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy