Skip to content
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

Fix serialization of BackedEnum - annotation without name/value #1536

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can is_a($enum, \BackedEnum::class, true) be replace with $enum instanceof \BackedEnum? seems much more readable to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for reviewing the code...
yes, instanceof is better here 👍

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
17 changes: 15 additions & 2 deletions tests/Fixtures/ObjectWithEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<enum<'JMS\Serializer\Tests\Fixtures\Enum\Suit'>>")
Expand All @@ -30,6 +36,11 @@ class ObjectWithEnums
*/
public array $backedArray;

/**
* @Serializer\Type("array<enum<'JMS\Serializer\Tests\Fixtures\Enum\BackedSuit'>>")
*/
public array $backedArrayWithoutParam;

public Suit $ordinaryAutoDetect;

public BackedSuit $backedAutoDetect;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]}';
}
Expand Down
7 changes: 6 additions & 1 deletion tests/Serializer/xml/object_with_enums.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<ordinary><![CDATA[Clubs]]></ordinary>
<backed><![CDATA[C]]></backed>
<backed_value><![CDATA[C]]></backed_value>
<backed_without_param><![CDATA[C]]></backed_without_param>
<ordinary_array>
<entry><![CDATA[Clubs]]></entry>
<entry><![CDATA[Spades]]></entry>
Expand All @@ -10,6 +11,10 @@
<entry><![CDATA[C]]></entry>
<entry><![CDATA[H]]></entry>
</backed_array>
<backed_array_without_param>
<entry><![CDATA[C]]></entry>
<entry><![CDATA[H]]></entry>
</backed_array_without_param>
<ordinary_auto_detect><![CDATA[Clubs]]></ordinary_auto_detect>
<backed_auto_detect><![CDATA[C]]></backed_auto_detect>
<backed_int_auto_detect>3</backed_int_auto_detect>
Expand Down
Loading