-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] XML deserialization with MetadataAwareNameConverter and optional xml attributes fails if attributes are not present #32114
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
Comments
I can confirm this exact issue |
I have create an issue to cover both cases without using the Can this be useful for you? |
I think this is related, but perhaps not. I'm trying to deserialize XML into a Property entity with a name and value, e.g. <property name="length">4110</property> serialization.yaml is configured so that the attribute and value are generated properly. App\Entity\Property:
attributes:
name:
serialized_name: '@name'
groups: ['xml']
value:
serialized_name: '#'
groups: ['xml'] And works as expected. $property = (new Property())->setName('length')->setValue(4110);
$context = [
'xml_root_node_name' => 'property',
'groups' => ['xml']
];
$xml = $serializer->serialize($property, 'xml', $context);
// $xml is now '<property name="length">4110</property>' as expected
$xml = '<property name="length">4110</property>';
// but serializing to Property doesn't work.
$data = $serializer->deserialize($xml, Property::class, 'xml');
// neither name or value is set.
dd($data); Does the serialized_name property (or @SerializedName annotation) work in the deserialization process? Or should I be calling this differently? |
Help wanted. |
Hey, thanks for your report! |
My workaround was to use a regex to rewrite the attributes as node elements, then rewrite my parsing accordingly. Of course, my solution is a classic definition of "hack" -- functional, but certainly not the "right" way. |
Did something related here: #35087 |
Hey, thanks for your report! |
Could I get a reply or should I close this? |
@nicolas-grekas Do you know if any tests have been added for this? |
I don't remember sorry. Please have a look if you can. |
I can confirm that I still have the issue. I've create a repo that shows example, trivial (2 entities, serialization.yaml, and a Command).
The serializations works, but not the deserialization: $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);
// works!
/** @var Product $product2 */
$product2 = $this->serializer->deserialize($xml, Product::class, 'xml');
dump($product2); // nothing is set :-( |
Hey, thanks for your report! |
Could I get a reply or should I close this? |
I'll try again. |
I bumped the example repo to Symfony 6.1 and it still fails. git clone git@github.com:tacman/xml-serializer-demo.git
cd xml-serializer-demo && composer install
bin/console app:bug |
Hey, thanks for your report! |
Could I get a reply or should I close this? |
I guess not many people use XML deserialization. It continues to be an issue. |
Hey, thanks for your report! |
Could I get a reply or should I close this? |
I think this PR will fix it: b70032c but shouldn't it be on all Symfony releases, not just 5.4? |
@tacman We regularly merge the |
…rld) This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix XML scalar to object denormalization | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #32114 #34579 | License | MIT Another approach to #34825 Commits ------- 6fe892f [Serializer] Fix XML scalar to object denormalization
Symfony version(s) affected: ^4.3
Description
I am using Symfony Serializer to deserialize complex XML documents into custom objects. Some XML nodes have optional attributes that may be present or not.
For example, the "price" node can have a "currency" attribute or not:
<price currency="EUR">66</price>
or
<price>55</price>
are both valid.
I am using MetadataAwareNameConverter to map our class attributes to xml attributes.
In the first case, the Price object is correctly created and both attributes are filled in, but in the second case the Price object is created by both attributes are left NULL.
How to reproduce
Our Price class is this one:
This test case shows the correct behaviour when "currency" attribute is present:
but this test case fails (Amount is also NULL!):
Possible Solution
I have found that when denormalizing the attributes, the name converter receives an array with key '0' as property name. I have implemented a CustomMetadataAwareNameConverter that replaces the property name '0' by '#', and it works now..
This is the CustomMetadataAwareNameConverter class:
and the test that previously failed works now:
The text was updated successfully, but these errors were encountered: