schmittjoh / JMSSerializerBundle

Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
http://jmsyst.com/bundles/JMSSerializerBundle
MIT License
1.8k stars 311 forks source link

EntityNotFoundException when we deserialize XML in Symfony 4 #687

Open GregoireTenor opened 5 years ago

GregoireTenor commented 5 years ago

Hi,

We have this exception when we deserialize XML in Symfony 4 :

Doctrine\ORM\EntityNotFoundException: "Entity of type 'xxx' for IDs id() was not found"

This is our code :

$xml = '\<country\>\<code\>fr\</code\>\</country\>';
        $country = $this->serializer->deserialize($xml, Country::class, 'xml');
        var_dump($country->getCode());die();

The same code works in Symfony 2 project.

This is my configuration :

"php": "^7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "doctrine/doctrine-migrations-bundle": "^1.3",
        "friendsofsymfony/rest-bundle": "^2.4",
        "jms/serializer-bundle": "^2.4",
        "lexik/jwt-authentication-bundle": "^2.5",
        "nelmio/cors-bundle": "^1.5",
        "sensio/framework-extra-bundle": "^5.2",
        "symfony/console": "*",
        "symfony/flex": "^1.1",
        "symfony/framework-bundle": "^4.0",
        "symfony/orm-pack": "^1.0",
        "symfony/serializer-pack": "^1.0",
        "symfony/templating": "^4.0",
        "symfony/translation": "^4.0",
        "symfony/twig-bundle": "^4.0",
        "symfony/yaml": "*"

Thanks for your help

goetas commented 5 years ago

How are the metadata for the Country class?

Both doctrine and jms

GregoireTenor commented 5 years ago
/**
 * @ORM\Entity()
 */
class Country
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=2)
     * @Serializer\Type("string")
     */
    private $code;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     * @Serializer\Type("string")
     */
    private $name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getCode(): ?string
    {
        return $this->code;
    }

    public function setCode(string $code): self
    {
        $this->code = $code;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }
}
GregoireTenor commented 5 years ago

Why did you add the "question" label ? It's a bug ! Did you reproduce it ?

goetas commented 5 years ago

The primary key in your entity is "id", but you are sending the "code" in the xml data

goetas commented 5 years ago

JMS serializer detects it as doctrine entity and tries to load it

GregoireTenor commented 5 years ago

JMS should work without the id for new entities to persist... It works in Symfony 2... But you think it's normal ?

goetas commented 5 years ago

could be related to https://github.com/schmittjoh/serializer/pull/951

Can you check if adding jms/serializer: 1.11.* works?

GregoireTenor commented 5 years ago

I added jms/serializer: 1.11.*, but it doesn't work either