Skip to content

Commit

Permalink
Fix error when unserializing enums
Browse files Browse the repository at this point in the history
Unserialized enums where not saved in the objectMapping property.

Signed-off-by: Maurício Meneghini Fauth <[email protected]>
  • Loading branch information
MauricioFauth committed Sep 9, 2023
1 parent 061736d commit 3a0927d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/JsonSerializer/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,9 @@ protected function unserializeObject($value)
}

if (is_subclass_of($className, \UnitEnum::class)) {
return constant("$className::{$value['name']}");
$obj = constant("$className::{$value['name']}");
$this->objectMapping[$this->objectMappingIndex++] = $obj;
return $obj;
}

if (!$this->isSplList($className)) {
Expand Down
48 changes: 48 additions & 0 deletions tests/JsonSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ public function testSerializeEnums()
$this->assertSame($expected, $this->serializer->serialize($intBackedEnum));
}

/**
* Test serialization of multiple Enums
*
* @return void
*/
public function testSerializeMultipleEnums()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}

$obj = new stdClass();
$obj->enum1 = SupportEnums\MyUnitEnum::Hearts;
$obj->enum2 = SupportEnums\MyBackedEnum::Hearts;
$obj->enum3 = SupportEnums\MyIntBackedEnum::One;
$obj->enum4 = SupportEnums\MyUnitEnum::Hearts;
$obj->enum5 = SupportEnums\MyBackedEnum::Hearts;
$obj->enum6 = SupportEnums\MyIntBackedEnum::One;

$expected = '{"@type":"stdClass","enum1":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"},"enum2":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"},"enum3":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"One","value":1},"enum4":{"@type":"@1"},"enum5":{"@type":"@2"},"enum6":{"@type":"@3"}}';
$this->assertSame($expected, $this->serializer->serialize($obj));
}

/**
* Test unserialization of Enums
*
Expand Down Expand Up @@ -301,6 +324,31 @@ public function testUnserializeEnums()
$this->assertSame(SupportEnums\MyBackedEnum::Hearts->value, $obj->value);
}

/**
* Test unserialization of multiple Enums
*
* @return void
*/
public function testUnserializeMultipleEnums()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}

$obj = new stdClass();
$obj->enum1 = SupportEnums\MyUnitEnum::Hearts;
$obj->enum2 = SupportEnums\MyBackedEnum::Hearts;
$obj->enum3 = SupportEnums\MyIntBackedEnum::One;
$obj->enum4 = SupportEnums\MyUnitEnum::Hearts;
$obj->enum5 = SupportEnums\MyBackedEnum::Hearts;
$obj->enum6 = SupportEnums\MyIntBackedEnum::One;

$serialized = '{"@type":"stdClass","enum1":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"},"enum2":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"},"enum3":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"One","value":1},"enum4":{"@type":"@1"},"enum5":{"@type":"@2"},"enum6":{"@type":"@3"}}';
$actualObj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('stdClass', $actualObj);
$this->assertEquals($obj, $actualObj);
}

/**
* Test unserialization of wrong UnitEnum
*
Expand Down

0 comments on commit 3a0927d

Please sign in to comment.