From c952f9d95bbd249c5e0aef9c397918383d5a2cfb Mon Sep 17 00:00:00 2001 From: Jakub Pastuszek Date: Tue, 6 Feb 2024 08:37:40 +0100 Subject: [PATCH 1/3] Add Backed Enum without name/value specification to test --- doc/reference/annotations.rst | 6 ++---- tests/Fixtures/ObjectWithEnums.php | 17 +++++++++++++++-- tests/Serializer/JsonSerializationTest.php | 2 +- tests/Serializer/xml/object_with_enums.xml | 7 ++++++- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/doc/reference/annotations.rst b/doc/reference/annotations.rst index 0cb5e385a..c2f4b1123 100644 --- a/doc/reference/annotations.rst +++ b/doc/reference/annotations.rst @@ -387,10 +387,8 @@ Available Types: | | Examples: array, | | | array, etc. | +------------------------------------------------------------+--------------------------------------------------+ -| 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'> | Enum of type Color, use its case names | +| | for serialization and deserialization. | +------------------------------------------------------------+--------------------------------------------------+ | enum<'Color', 'name'> | Enum of type Color, use its case names | | | (as string) for serialization | diff --git a/tests/Fixtures/ObjectWithEnums.php b/tests/Fixtures/ObjectWithEnums.php index f7761f086..84f84d2c8 100644 --- a/tests/Fixtures/ObjectWithEnums.php +++ b/tests/Fixtures/ObjectWithEnums.php @@ -15,10 +15,16 @@ class ObjectWithEnums * @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\Suit', 'name'>") */ public Suit $ordinary; + /** * @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit', 'value'>") */ - public BackedSuit $backed; + public BackedSuit $backedValue; + + /** + * @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit'>") + */ + public BackedSuit $backedWithoutParam; /** * @Serializer\Type("array>") @@ -30,6 +36,11 @@ class ObjectWithEnums */ public array $backedArray; + /** + * @Serializer\Type("array>") + */ + public array $backedArrayWithoutParam; + public Suit $ordinaryAutoDetect; public BackedSuit $backedAutoDetect; @@ -46,9 +57,11 @@ public function __construct() { $this->ordinary = Suit::Clubs; - $this->backed = BackedSuit::Clubs; + $this->backedValue = BackedSuit::Clubs; + $this->backedWithoutParam = BackedSuit::Clubs; $this->backedArray = [BackedSuit::Clubs, BackedSuit::Hearts]; + $this->backedArrayWithoutParam = [BackedSuit::Clubs, BackedSuit::Hearts]; $this->ordinaryArray = [Suit::Clubs, Suit::Spades]; $this->ordinaryAutoDetect = Suit::Clubs; diff --git a/tests/Serializer/JsonSerializationTest.php b/tests/Serializer/JsonSerializationTest.php index 0c6800e89..4c7bb0302 100644 --- a/tests/Serializer/JsonSerializationTest.php +++ b/tests/Serializer/JsonSerializationTest.php @@ -141,7 +141,7 @@ protected static function getContent($key) $outputs['custom_datetimeinterface'] = '{"custom":"2021-09-07"}'; $outputs['data_integer'] = '{"data":10000}'; $outputs['uid'] = '"66b3177c-e03b-4a22-9dee-ddd7d37a04d5"'; - $outputs['object_with_enums'] = '{"ordinary":"Clubs","backed":"C","ordinary_array":["Clubs","Spades"],"backed_array":["C","H"],"ordinary_auto_detect":"Clubs","backed_auto_detect":"C","backed_int_auto_detect":3,"backed_int":3,"backed_name":"C","backed_int_forced_str":3}'; + $outputs['object_with_enums'] = '{"ordinary":"Clubs","backed_value":"C","backed_without_param":"C","ordinary_array":["Clubs","Spades"],"backed_array":["C","H"],"backed_array_without_param":["C","H"],"ordinary_auto_detect":"Clubs","backed_auto_detect":"C","backed_int_auto_detect":3,"backed_int":3,"backed_name":"C","backed_int_forced_str":3}'; $outputs['object_with_autodetect_enums'] = '{"ordinary_array_auto_detect":["Clubs","Spades"],"backed_array_auto_detect":["C","H"],"mixed_array_auto_detect":["Clubs","H"]}'; $outputs['object_with_enums_disabled'] = '{"ordinary_array_auto_detect":[{"name":"Clubs"},{"name":"Spades"}],"backed_array_auto_detect":[{"name":"Clubs","value":"C"},{"name":"Hearts","value":"H"}],"mixed_array_auto_detect":[{"name":"Clubs"},{"name":"Hearts","value":"H"}]}'; } diff --git a/tests/Serializer/xml/object_with_enums.xml b/tests/Serializer/xml/object_with_enums.xml index bba1a2b00..ab0017713 100644 --- a/tests/Serializer/xml/object_with_enums.xml +++ b/tests/Serializer/xml/object_with_enums.xml @@ -1,7 +1,8 @@ - + + @@ -10,6 +11,10 @@ + + + + 3 From 91d59c7963a35d77e176fee4e5be3d8a18a33cf9 Mon Sep 17 00:00:00 2001 From: Jakub Pastuszek Date: Tue, 6 Feb 2024 10:18:10 +0100 Subject: [PATCH 2/3] 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))); } From 3a3518a5b568cd2919ed95b3d749ba823ad52140 Mon Sep 17 00:00:00 2001 From: Jakub Pastuszek Date: Tue, 20 Feb 2024 16:31:44 +0100 Subject: [PATCH 3/3] Use instanceof instead of is_a() --- src/Handler/EnumHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Handler/EnumHandler.php b/src/Handler/EnumHandler.php index 916f3b8d7..44e223bed 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]) || (!isset($type['params'][1]) && is_a($enum, \BackedEnum::class, true))) { + if ((isset($type['params'][1]) && 'value' === $type['params'][1]) || (!isset($type['params'][1]) && $enum instanceof \BackedEnum)) { 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))); }