From 91d59c7963a35d77e176fee4e5be3d8a18a33cf9 Mon Sep 17 00:00:00 2001 From: Jakub Pastuszek Date: Tue, 6 Feb 2024 10:18:10 +0100 Subject: [PATCH] Fix serialization of BackedEnum - annotation without name/value --- doc/reference/annotations.rst | 6 ++++-- src/Handler/EnumHandler.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/doc/reference/annotations.rst b/doc/reference/annotations.rst index c2f4b1123..0cb5e385a 100644 --- a/doc/reference/annotations.rst +++ b/doc/reference/annotations.rst @@ -387,8 +387,10 @@ Available Types: | | Examples: array, | | | array, etc. | +------------------------------------------------------------+--------------------------------------------------+ -| enum<'Color'> | Enum of type Color, use its case names | -| | for serialization and deserialization. | +| enum<'Color'> | Enum of type Color, use its case values | +| | for serialization and deserialization | +| | if the enum is a backed enum, | +| | use its case names if it is not a backed enum. | +------------------------------------------------------------+--------------------------------------------------+ | enum<'Color', 'name'> | Enum of type Color, use its case names | | | (as string) for serialization | diff --git a/src/Handler/EnumHandler.php b/src/Handler/EnumHandler.php index 1adbc051a..916f3b8d7 100644 --- a/src/Handler/EnumHandler.php +++ b/src/Handler/EnumHandler.php @@ -44,7 +44,7 @@ public function serializeEnum( array $type, SerializationContext $context ) { - if (isset($type['params'][1]) && 'value' === $type['params'][1]) { + if ((isset($type['params'][1]) && 'value' === $type['params'][1]) || (!isset($type['params'][1]) && is_a($enum, \BackedEnum::class, true))) { if (!$enum instanceof \BackedEnum) { throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', get_class($enum))); }