Skip to content

[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

Closed

Conversation

monteiro
Copy link
Contributor

@monteiro monteiro commented Dec 23, 2019

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #34579
License MIT
Doc PR I have still to create it. If this gets accepted, I will!

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 the XmlEncoder.

Example:

The array:
['foo' => ['@bar' => 'value', '#' => 'baz']];
generates the following XML output:

<?xml version="1.0"?>
 <response>
     <foo bar="value">
        baz
     </foo>
</response>

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:

$objectNormalizer  = new ObjectNormalizer(null, new SpecialAttributesConverter(), null, $phpDocExtractor);

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.

@monteiro monteiro requested a review from dunglas as a code owner December 23, 2019 09:53
@monteiro monteiro changed the title serializer: add xml name converter to use objects instead of arrays [Serializer] add xml name converter to use objects instead of arrays Dec 23, 2019
@nicolas-grekas nicolas-grekas added this to the next milestone Dec 26, 2019
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please target master

@monteiro monteiro changed the base branch from 4.4 to master December 26, 2019 08:49
@monteiro
Copy link
Contributor Author

@nicolas-grekas Already did the fixes and put the branch agaisnt master. Thanks a lot for the review!

@fabpot
Copy link
Member

fabpot commented Aug 11, 2020

@dunglas Can you have a look at this one? Thank you.

@dunglas
Copy link
Member

dunglas commented Aug 16, 2020

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.

@fabpot
Copy link
Member

fabpot commented Aug 16, 2020

Thanks Kevin. I now understand the goal of this PR and I think I agree with you. Let's close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Serializer] Extra attributes name converter for XML when using ObjectNormalizer
6 participants