Skip to content

Can't make the bundle to deserialize enums #145

@lucianull

Description

@lucianull

Context:
I have a base class object:


namespace App\Model\Event;

use App\Enum\NotificationTypeTagsEnum;
use App\Model\SuppliersPortal\RequestBody\ActivateAccountModel;
use App\Model\SuppliersPortal\RequestBody\UnknownUnitOfMeasureModel;
use Symfony\Component\Serializer\Attribute\DiscriminatorMap;
use Symfony\Component\Validator\Constraints as Assert;

#[DiscriminatorMap(
    typeProperty: 'notificationTypeTag',
    mapping: [
        NotificationTypeTagsEnum::SUPPLIERS_PORTAL_ACTIVATE_ACCOUNT->value => ActivateAccountModel::class,
        NotificationTypeTagsEnum::SUPPLIERS_PORTAL_UNKNOWN_UNIT_OF_MEASURE->value => UnknownUnitOfMeasureModel::class,
    ]
)]
class EventBodyModel
{
    public function __construct(
        #[Assert\NotBlank(message: "Notification type should not be blank.")]
        public ?NotificationTypeTagsEnum $notificationTypeTag
    ) {}

    public function getNotificationTypeTag(): ?NotificationTypeTagsEnum
    {
        return $this->notificationTypeTag;
    }

    public function setNotificationTypeTag(?NotificationTypeTagsEnum $notificationTypeTag): self
    {
        $this->notificationTypeTag = $notificationTypeTag;
        return $this;
    }
}

A class that extends it:

<?php

namespace App\Model\SuppliersPortal\RequestBody;

use App\Model\Event\EventBodyModel;
use App\Utils\Interfaces\ArrayableInterface;
use Symfony\Component\Validator\Constraints as Assert;


class ActivateAccountModel extends EventBodyModel
{
    public function __construct(
        #[Assert\NotNull(message: "First name should not be null.")]
        private ?string $firstName = null,

        #[Assert\NotNull(message: "Last name should not be null.")]
        private ?string $lastName = null,

        #[Assert\Url(message: "Activation link should be a valid URL.")]
        private ?string $activationLink = null,
    ) {
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(?string $firstName): self
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(?string $lastName): self
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getActivationLink(): ?string
    {
        return $this->activationLink;
    }

    public function setActivationLink(?string $activationLink): self
    {
        $this->activationLink = $activationLink;

        return $this;
    }
}

And i try to store this object in the db, in an entity field:

...
class Event
...
    #[ORM\Column(type: 'json_document', nullable: true, options: ['jsonb' => true])]
    private ?EventBodyModel $data = null;
...

And when i try to get an event entity from the db I am getting the following error:
Failed to denormalize attribute "notificationTypeTag" value for class "App\Model\SuppliersPortal\RequestBody\ActivateAccountModel": Expected argument of type "?App\Enum\NotificationTypeTagsEnum

Am I doing something wrong or why notificationTypeTag is not deserialized back from string to NotificationTypeTagsEnum?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions