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

Having only a deserialization handler with a "generic" type throws... why? #1435

Open
gremo opened this issue Oct 20, 2022 · 0 comments
Open
Labels

Comments

@gremo
Copy link

gremo commented Oct 20, 2022

I want to use an handler only for deserialization. Upon serialization, the default handler should be used.

If I try to define a custom handler, registering it like this way:

app.serializer.relation_deserialization_handler:
    class: App\Serializer\RelationsHandler
    arguments: ['@doctrine.orm.entity_manager']
    tags:
        - { name: jms_serializer.handler, type: Relation, direction: deserialization, format: json, method: deserialize }
<?php

declare(strict_types=1);

namespace App\Serializer;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use JMS\Serializer\Context;
use JMS\Serializer\Visitor\DeserializationVisitorInterface;

class RelationsHandler
{
    private $manager;

    public function __construct(EntityManagerInterface $manager)
    {
        $this->manager = $manager;
    }

    public function deserialize(
        DeserializationVisitorInterface $visitor,
        $relation,
        array $type,
        Context $context
    ) {
        $className = isset($type['params'][0]['name']) ? $type['params'][0]['name'] : null;
    }
}

Class Relation does not exist in jms\metadata\src\MetadataFactory.php line 175.

I'm using it on a regular PHP property:

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;

/**
 * @ORM\Entity()
 * @ORM\Table("customer")
 */
class Customer
{
    /**
     * @ORM\ManyToOne(targetEntity="Agent")
     * @ORM\JoinColumn()
     *
     * @Serializer\Type("Relation<App\Entity\Agent>")
     * @Serializer\Groups({"customers:get_all", "customers:get_one", "customers:create"})
     */
    public ?Agent $agent = null;
}
@goetas goetas added the question label Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants