serializer: [3.20.0] The provided class "MyEnum" is an enum, and cannot be instantiated

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

I’m super grateful for the work on enums in https://github.com/schmittjoh/serializer/pull/1448. I’m just testing it out with jms/serializer v3.20.0 together with jms/serializer-bundle dev-enable-enum and noticed something wrong.

Steps required to reproduce the problem

enum SocialPlatform : string
{
    case Spotify = 'spotify';
    case Instagram = 'instagram';
}

class MyEvent {
    public function __construct(
        private int $userId,
        private SocialPlatform $socialPlatform,
    ) {
    }
}

$object = new MyEvent(1, SocialPlatform::instagram);

// This is in a functional test, using Symfony Bundle, with `enum_support` enabled.
$this->serializer = self::getContainer()->get('jms_serializer');

$this->context = SerializationContext::create()
    ->enableMaxDepthChecks()
    ->setSerializeNull(true);

$serialized = $this->serializer->serialize($object, 'json', $this->context);
// serializes to: {"user_id":1,"social_platform":"instagram"}

$deserialized = $this->serializer->deserialize($serialized, MyEvent::class, 'json');
// fails Doctrine\Instantiator\Exception\InvalidArgumentException : The provided class "SocialPlatform" is an enum, and cannot be instantiated

Expected Result

No error, deserialized back to an object.

Actual Result

Doctrine\Instantiator\Exception\InvalidArgumentException : The provided class "SocialPlatform" is an enum, and cannot be instantiated
 /Volumes/CS/www/app/vendor/jms/serializer/src/Construction/UnserializeObjectConstructor.php:22
 /Volumes/CS/www/app/vendor/jms/serializer/src/Construction/DoctrineObjectConstructor.php:72
 /Volumes/CS/www/app/vendor/jms/serializer/src/GraphNavigator/DeserializationGraphNavigator.php:189
 /Volumes/CS/www/app/vendor/jms/serializer/src/JsonDeserializationVisitor.php:188
 /Volumes/CS/www/app/vendor/jms/serializer/src/GraphNavigator/DeserializationGraphNavigator.php:214
 /Volumes/CS/www/app/vendor/jms/serializer/src/JsonDeserializationVisitor.php:112
 /Volumes/CS/www/app/vendor/jms/serializer/src/GraphNavigator/DeserializationGraphNavigator.php:140
 /Volumes/CS/www/app/vendor/jms/serializer/src/JsonDeserializationVisitor.php:188
 /Volumes/CS/www/app/vendor/jms/serializer/src/GraphNavigator/DeserializationGraphNavigator.php:214
 /Volumes/CS/www/app/vendor/jms/serializer/src/Serializer.php:252
 /Volumes/CS/www/app/vendor/jms/serializer/src/Serializer.php:180

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 24 (16 by maintainers)

Commits related to this issue

Most upvoted comments

Tried it again today, and it seems to be working fine. Thank you! 💙