-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] add xml name converter to use objects instead of arrays #35087
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
Conversation
src/Symfony/Component/Serializer/Tests/NameConverter/XmlAttributesConverterTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Tests/NameConverter/XmlAttributesConverterTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/NameConverter/XmlAttributesConverter.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please target master
src/Symfony/Component/Serializer/NameConverter/XmlAttributesConverter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/NameConverter/XmlAttributesConverter.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/NameConverter/XmlAttributesConverter.php
Outdated
Show resolved
Hide resolved
25d39c6
to
cdaebf6
Compare
@nicolas-grekas Already did the fixes and put the branch agaisnt master. Thanks a lot for the review! |
@dunglas Can you have a look at this one? Thank you. |
I'm not a fond of these new conventions. IMHO using a custom (de)normalizer for DTOs is cleaner, already supported and gives you more control. |
Thanks Kevin. I now understand the goal of this PR and I think I agree with you. Let's close. |
Description
Currently, using JSON is the facto standard and well supported for all normalizers.
What I found is that the
XmlEncoder
decodes/encodes the XML to array, but converts extra attributes with a@
prefix and uses#
to identity the node value.This conversion makes it extremely difficult to use the
ObjectNormalizer
and fill out DTOs with specific XML data instead of consuming just the array returned by theXmlEncoder
.Example:
The array:
['foo' => ['@bar' => 'value', '#' => 'baz']];
generates the following XML output:
What if I want to use my own DTOs?
Possible solution
Create a custom name converter that handles the extra attributes and XML node values. That way you can out of the box, use the
ObjectNormalizer
to set specific attributes in a specific object.Example
We can use custom prefixes for attributes and node values, so we match them with our DTOs:
By using this name converter, we can configure the
ObjectNormalizer
:Conclusion
By using this PR, it's possible to manage XML requests/responses just by using DTOs with types, instead of relying on arrays to build your XML requests and read from XML responses with extra attributes.