Skip to content

Commit

Permalink
allow de serialization of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Feb 3, 2023
1 parent cdc876a commit 2c26914
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/JMSSerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private function loadInternal(array $config, ScopedContainer $container, array $

if ($config['enum_support']) {
$container->getDefinition('jms_serializer.metadata.enum_driver')
->setDecoratedService('jms_serializer.metadata_driver')
->setDecoratedService('jms_serializer.metadata_driver', null, 50)
->setPublic(false);
} else {
$container->removeDefinition('jms_serializer.metadata.enum_driver');
Expand Down
34 changes: 34 additions & 0 deletions Tests/DependencyInjection/Fixture/ObjectUsingEnumDeserialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace JMS\SerializerBundle\Tests\DependencyInjection\Fixture;

use JMS\Serializer\Annotation\Type;

class ObjectUsingEnumDeserialize
{
private ObjectUsingEnumDeserializeCard $one;

/**
* @Type("enum<'JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard'>")
*/
private $two;
/**
* @Type("array<enum<'JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard'>>")
*/
private array $three;

public function __construct()
{
$this->one = ObjectUsingEnumDeserializeCard::Black;
$this->two = ObjectUsingEnumDeserializeCard::Red;
$this->three = [ObjectUsingEnumDeserializeCard::Red, ObjectUsingEnumDeserializeCard::Black];
}
}

enum ObjectUsingEnumDeserializeCard
{
case Black;
case Red;
}
19 changes: 12 additions & 7 deletions Tests/DependencyInjection/JMSSerializerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces\AnInterfaceImplementation;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\IncludeInterfaces\AnObject;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnum;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserialize;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionLanguage;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingExpressionProperties;
use JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SimpleObject;
Expand Down Expand Up @@ -47,16 +48,17 @@ private function clearTempDir()
// clear temporary directory
$dir = sys_get_temp_dir() . '/serializer';
if (is_dir($dir)) {
foreach (new \RecursiveDirectoryIterator($dir) as $file) {
$filename = $file->getFileName();
if ('.' === $filename || '..' === $filename) {
continue;
}
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);

@unlink($file->getPathName());
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}

@rmdir($dir);
rmdir($dir);
}
}

Expand Down Expand Up @@ -593,6 +595,9 @@ public function testEnumSupportCanBeEnabled()

$object = new ObjectUsingEnum();
$this->assertEquals('{"one":"Black","two":"Red","three":["Red","Black"]}', $serializer->serialize($object, 'json'));

$object = new ObjectUsingEnumDeserialize();
$this->assertEquals($object, $serializer->deserialize('{"one":"Black","two":"Red","three":["Red","Black"]}', ObjectUsingEnumDeserialize::class, 'json'));
}

public function testEnumSupportIsDisabledByDefault()
Expand Down

0 comments on commit 2c26914

Please sign in to comment.