schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.32k stars 585 forks source link

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

Open gremo opened 2 years ago

gremo commented 2 years ago

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;
}