Skip to content

Commit

Permalink
Merge pull request #1536 from iKsSs/enum-test
Browse files Browse the repository at this point in the history
Fix serialization of BackedEnum - annotation without name/value
  • Loading branch information
scyzoryck authored Feb 21, 2024
2 parents 9e15e63 + 3a3518a commit 1235c79
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
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]) && $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)));
}
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

0 comments on commit 1235c79

Please sign in to comment.