-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Closed
Description
Symfony version(s) affected
all
Description
When serializing an entity with related entities, and those entities are configured to have attributes, deserializing does not work.
<?xml version="1.0"?>
<product description="Wool sweater">
<property name="color">blue</property>
<property name="size">small</property>
</product>
This XML can be generated from the entity but not deserialized.
How to reproduce
$product = (new Product())
->setDescription("Wool sweater");
$product
->addProperty((new Property())->setName('color')->setValue('blue'))
->addProperty((new Property())->setName('size')->setValue('small'))
;
$context = [
'xml_root_node_name' => 'product',
'groups' => ['xml'],
'xml_standalone' => false,
'xml_format_output' => true,
];
$xml = $this->serializer->serialize($product, 'xml', $context);
This works as expected, the $xml string is now:
<?xml version="1.0"?>
<product description="Wool sweater">
<property name="color">blue</property>
<property name="size">small</property>
</product>
But deserializing that XML doesn't work:
/** @var Product $product2 */
$product2 = $this->serializer->deserialize($xml, Product::class, 'xml');
dump($product2); // nothing is set :-(
The Serializer is configured so that Product description and Property name are attributes.
# config/serialization.yaml
App\Entity\Product:
attributes:
description:
serialized_name: '@description'
groups: ['xml']
properties:
serialized_name: 'property'
groups: ['xml']
App\Entity\Property:
attributes:
name:
serialized_name: '@name'
groups: ['xml']
value:
serialized_name: '#'
groups: ['xml']
The entities are simple and were created with bin/console make:entity. Setters and getters are generated and so not shown.
#[ORM\Entity(repositoryClass: ProductRepository::class)]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $description;
#[ORM\OneToMany(mappedBy: 'product', targetEntity: Property::class, orphanRemoval: true)]
private $properties;
public function __construct()
{
$this->properties = new ArrayCollection();
}
#[ORM\Entity(repositoryClass: PropertyRepository::class)]
class Property
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
private $name;
#[ORM\Column(type: 'string', length: 255)]
private $value;
#[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'properties')]
#[ORM\JoinColumn(nullable: false)]
private $product;
Possible Solution
I'm guessing that the issue is that the serialization.yaml file isn't configured correctly, but since serializing works, I'm not sure what the error is.
Additional Context
I thought this might be related to #51594 or #51701, but the issue persists.