Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't make the bundle to deserialize enums #145

Open
lucianull opened this issue Jan 27, 2025 · 0 comments
Open

Can't make the bundle to deserialize enums #145

lucianull opened this issue Jan 27, 2025 · 0 comments

Comments

@lucianull
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant