From 5997eae18486e3ba0b010a6fc20e7b6a2185b43a Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Fri, 11 Oct 2024 14:19:15 +0200 Subject: [PATCH] Do not use single quotes in enum type definition This makes its behaviour similar to the other typed types, such as array and iterable. --- UPGRADING.md | 1 + doc/reference/annotations.rst | 8 ++++---- src/Handler/EnumHandler.php | 6 ++++++ src/Metadata/Driver/EnumPropertiesDriver.php | 8 +++++++- tests/Fixtures/ObjectWithEnums.php | 12 +++++++----- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/UPGRADING.md b/UPGRADING.md index 93ae1ced1..a1ed23549 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -104,6 +104,7 @@ If you are on version `1.x`, it is suggested to migrate directly to `3.0.0` (sin - `JsonSerializationVisitor::hasData` will be removed - `VisitorInterface` is internal, use `SerializationVisitorInterface` and `DeserializationVisitorInterface` instead - `GraphNavigator` is internal, use `GraphNavigatorInterface` instead +- `enum<'Type'>` and similar are deprecated, use `enum` instead **Other** - Elements (as classes, interfaces, methods, properties...) diff --git a/doc/reference/annotations.rst b/doc/reference/annotations.rst index a6728af27..765deaf5f 100644 --- a/doc/reference/annotations.rst +++ b/doc/reference/annotations.rst @@ -454,19 +454,19 @@ Available Types: | | Examples: array, | | | array, etc. | +------------------------------------------------------------+--------------------------------------------------+ -| enum<'Color'> | Enum of type Color, use its case values | +| enum | 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 | +| enum | Enum of type Color, use its case names | | | (as string) for serialization | | | and deserialization. | +------------------------------------------------------------+--------------------------------------------------+ -| enum<'Color', 'value'> | Backed Enum of type Color, use its case value | +| enum | Backed Enum of type Color, use its case value | | | for serialization and deserialization. | +------------------------------------------------------------+--------------------------------------------------+ -| enum<'Color', 'value', 'integer'> | Backed Enum of type Color, use its case value | +| enum | Backed Enum of type Color, use its case value | | | (forced as integer) for serialization | | | and deserialization. | +------------------------------------------------------------+--------------------------------------------------+ diff --git a/src/Handler/EnumHandler.php b/src/Handler/EnumHandler.php index 44e223bed..e3e82baef 100644 --- a/src/Handler/EnumHandler.php +++ b/src/Handler/EnumHandler.php @@ -64,6 +64,12 @@ public function serializeEnum( public function deserializeEnum(DeserializationVisitorInterface $visitor, $data, array $type): ?\UnitEnum { $enumType = $type['params'][0]; + if (isset($enumType['name'])) { + $enumType = $enumType['name']; + } else { + trigger_deprecation('jms/serializer', '3.31', "Using enum<'Type'> or similar is deprecated, use enum instead."); + } + $caseValue = (string) $data; $ref = new \ReflectionEnum($enumType); diff --git a/src/Metadata/Driver/EnumPropertiesDriver.php b/src/Metadata/Driver/EnumPropertiesDriver.php index 514e3ee8c..3e1e85f10 100644 --- a/src/Metadata/Driver/EnumPropertiesDriver.php +++ b/src/Metadata/Driver/EnumPropertiesDriver.php @@ -48,7 +48,13 @@ public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata try { $propertyReflection = $this->getReflection($propertyMetadata); if ($enum = $this->getEnumReflection($propertyReflection)) { - $serializerType = ['name' => 'enum', 'params' => [$enum->getName(), $enum->isBacked() ? 'value' : 'name']]; + $serializerType = [ + 'name' => 'enum', + 'params' => [ + ['name' => $enum->getName(), 'params' => []], + $enum->isBacked() ? 'value' : 'name', + ], + ]; $propertyMetadata->setType($serializerType); } } catch (ReflectionException $e) { diff --git a/tests/Fixtures/ObjectWithEnums.php b/tests/Fixtures/ObjectWithEnums.php index 84f84d2c8..c3c6fee79 100644 --- a/tests/Fixtures/ObjectWithEnums.php +++ b/tests/Fixtures/ObjectWithEnums.php @@ -12,32 +12,34 @@ class ObjectWithEnums { /** - * @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\Suit', 'name'>") + * @Serializer\Type("enum") */ public Suit $ordinary; /** - * @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit', 'value'>") + * @Serializer\Type("enum") */ public BackedSuit $backedValue; /** + * Deprecated, remove single quote around type with 4.0. + * * @Serializer\Type("enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit'>") */ public BackedSuit $backedWithoutParam; /** - * @Serializer\Type("array>") + * @Serializer\Type("array>") */ public array $ordinaryArray; /** - * @Serializer\Type("array>") + * @Serializer\Type("array>") */ public array $backedArray; /** - * @Serializer\Type("array>") + * @Serializer\Type("array>") */ public array $backedArrayWithoutParam;