symfonyjmsserializerbundle

JMS Serializer / Symfony class does not exist


I have an entity with some OneToOne relations:

/**
 * @OA\Property(type="integer")
 * @Serializer\Groups({"list", "detail"})
 * @Serializer\Type("File::class")
 * @ORM\OneToOne(targetEntity="File")
 */
private ?File $cv = null;

And here is the File entity:

/**
 * @OA\Schema()
 * @ORM\Entity(repositoryClass=FileRepository::class)
 */
class File
{

    /**
     * @OA\Property(type="integer")
     * @Serializer\Groups({"list", "detail"})
     * @Serializer\Type("integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @OA\Property(type="string")
     * @Serializer\Groups({"list", "detail"})
     * @Serializer\Type("string")
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    private $filename;

    /**
     * @OA\Property(type="string")
     * @Serializer\Groups({"list", "detail"})
     * @Serializer\Type("string")
     * @ORM\Column(type="guid", nullable=false)
     */
    private $uuid;

    /**
     * @OA\Property(type="string")
     * @Serializer\Groups({"list", "detail"})
     * @Serializer\Type("string")
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    private $contentType;

    /**
     * @OA\Property(type="string", format="date-time")
     * @Serializer\Groups({"list", "detail"})
     * @Serializer\Type("string")
     * @ORM\Column(type="date", length=255, nullable=false)
     */
    private $creationDate;

When I try to serialize with JMS my first entity, with the OneToOne relation:

        $data = $this->serializer->serialize($candidate, 'json', SerializationContext::create()->setGroups(array('detail'))->setSerializeNull(true));

I always got the error: Class File does not exist

Obviously, it's my @Type annotation that is wrong, but why? What should I do.


Solution

  • Try changing this:
    @Serializer\Type("File::class")

    To this (no quotes):
    @Serializer\Type(File::class)

    I suspect it may be looking for the literal string "File::class" instead of parsing the class name.