Skip to content

Enums lose type information when serialised as part of a static map #10

Description

@IceRidder

Detailed description

When serializing with a TypeMap and one of the possible elements is an Enum it will lose its type

Ex:

#[StaticTypeMap(key: 'type', map: [
    'enum' => MyEnum::class,
    'object' =>  MyType::class,
])]

enum MyEnum: int implements MyInterface
{
    case A = 1;
    case B = 2;
}

class MyType implements MyInterface {
    public function __construct(
        public int $id = 1,
    )
    {
    }
}

class Element
{
    public function __construct(
        #[SequenceField(arrayType: MyInterface::class)]
        public array $elements = [],
    )
    {
    }
}

$element = new Element([MyEnum::A]);
$serde->serialize($element, 'json');

Will result in : {"elements":[1]} and thus de-serialization will fail

Context

I am trying to create a generic collection to serialize/de-serialize list of items

Possible implementation

Add map check in EnumExporter similar to ObjectExporter and if one is present serialize as {keyName: mixed, value: mixed}. I would not consider this a BC break as the feature was not working anyway.

Your environment

Include as many relevant details about the environment you experienced the bug in and how to reproduce it.

  • Version used (e.g. PHP 5.6, HHVM 3): PHP 8.1
  • Operating system and version (e.g. Ubuntu 16.04, Windows 7): Ubuntu 22.10

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions