-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[Serializer] Support enums that implement JsonSerializable
in BackedEnumNormalizer
#57068
base: 6.4
Are you sure you want to change the base?
Conversation
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
This comment was marked as outdated.
This comment was marked as outdated.
JsonSerializable
in BackedEnumNormalizer
Thanks for the PR. This is a new feature and must target |
I do have a question. Wouldn't it possibly be preferable to add an option to enable/disable this behavior? As using the If you have a backed enum that implements Edit/addition: Using the |
@@ -78,6 +79,15 @@ public function testSerializeTranslatableBackedEnum() | |||
|
|||
$this->assertEquals('GET', $serializer->serialize(TranslatableBackedEnum::Get, 'yaml')); | |||
} | |||
|
|||
public function testSerializeJsonSerializableBackedEnum() |
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.
Seeing how the BackedEnumNormalizer has a test itself, wouldn't it be a more fitting place to test the new behavior in the normalizers BackedEnumNormalizerTest?
So. I need to close this pull request and reopen it with the change in my 7.2 branch, right? |
You are right @KevinVanSonsbeek! I forgot about deserialization. |
@Eptagone i did also just remember that symfony offers the JsonSerializableNormalizer, which can be used for any object, not just backed enums. Allthough you do need to keep in mind the normalizer ordering for it to work as expected. <?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Serializer;
enum JsonSerializableBackedDummyEnum: string implements JsonSerializable
{
case Get = 'GET';
public function jsonSerialize(): string
{
return 'json_serialized_get_value';
}
}
$normalizers = [new BackedEnumNormalizer(), new JsonSerializableNormalizer()];
$serializer = new Serializer($normalizers, [new JsonEncoder()]);
dump($serializer->serialize(JsonSerializableBackedDummyEnum::Get, 'json')); // "GET"
$normalizers = [new JsonSerializableNormalizer(), new BackedEnumNormalizer()];
$serializer = new Serializer($normalizers, [new JsonEncoder()]);
dump($serializer->serialize(JsonSerializableBackedDummyEnum::Get, 'json')); // "json_serialized_get_value" |
Ohh, if that exists then there is no need for this pull request. Thx |
The serializer itself doesn't work with priority. When you go and serialize content, it checks if normalization is needed. And if normalization is needed, it will just start looping through all the normalizers it has, till it finds one that supports the input. But in the default service definitions it does seem that the You could work around this problem by redefining the service for the normalizer, with a different priority though. (Allthough you'd need to keep an eye out that you don't break any other serialization logic) # /config/services.yaml
services:
'serializer.normalizer.backed_enum':
class: 'Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer'
tags:
- { name: 'serializer.normalizer', priority: -951 } # Set to priority -951, as the JsonSerializableNormalizer is loaded at priority -950 |
I have an enum that implement
JsonSerializable
. But looks like it is not considered when serializing.Currently, the following code doesn't work with the Serializer but it works with
json_encode
.This PR allows the
BackedEnumNormalizer
to handle enums that implement theJsonSerializable
interface.