Skip to content

Commit

Permalink
Fix serialization of BackedEnum - annotation without name/value
Browse files Browse the repository at this point in the history
  • Loading branch information
iksss-invia committed Feb 6, 2024
1 parent c952f9d commit 91d59c7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions doc/reference/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ Available Types:
| | Examples: array<string, string>, |
| | array<string, MyNamespace\MyObject>, 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 |
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/EnumHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down

0 comments on commit 91d59c7

Please sign in to comment.