-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Symfony serializer does not map collections correctly #27279
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
Yes it definetly looks like a bug. Current implementation of XmlEncoder decodes
as
At the same time this xml
will be decoded to:
This situation seems natural as there is no way in xml to indicate that a particular node is an element of a collection (unlike yaml or json).
Otherwise two different yaml snippets :
and
would be deserialized without any problem though the second snippent is not quite appropriate for your model. Status: Reviewed |
Thanks for the report and the confirmation. Would any of you like to submit a fix? |
…ontains the only one element (webnet-fr) This PR was squashed before being merged into the 3.4 branch (closes #27326). Discussion ---------- [Serializer] deserialize from xml: Fix a collection that contains the only one element | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27279 | License | MIT | Doc PR | In xml when parent node (`restaurants`) contains several children nodes with the same tag (`restaurant`) it is clear that the children form a collection: ``` restaurants = {array} [1] restaurant = {array} [2] 0 = {array} [2] name = "Some restaurant name" type = "Chinese" 1 = {array} [2] name = "Another restaurant name" type = "Italian" ``` Afterwards the object denormalizer has no problem to create a collection of restaurants. But when there is only one child (`restaurant`) the decoded normalized array will not contain a collection: ``` restaurants = {array} [1] restaurant = {array} [2] name = "Some restaurant name" type = "Chinese" ``` In this situation the object denormalizer threw unexpected exception. This PR modifies `AbstractObjectNormalizer` that is it will fill a collection containing the sole element properly. Commits ------- 1f346f4 [Serializer] deserialize from xml: Fix a collection that contains the only one element
I ask @fabpot attention to reopen issue Faced the same issue, but now for the json-data(nested object) Same error: Fix provided in commit 1f346f4 works well if remove xml condition. I believe it is necessary to expand fix for json too. |
@nano-freelancer if you want anyone to really care, you'd better submit a new issue with detailed description. If you can submit a fix, that'd be even better. Thanks. |
@nicolas-grekas done #28550 |
@nano-freelancer can you post the corresponding PHP entity please? |
in controller: |
Symfony version(s) affected: 4.1.0
Description
When there is a collection with a single item, the serializer creates produces an error instead of an array with one object.
How to reproduce
https://github.com/fhink/symfony-collection-serializer-bug
Just run
php src/serialize.php
Possible Solution
fhink/symfony-collection-serializer-bug@2f5984b
Additional context
Tested with PHP 7.1+
https://stackoverflow.com/questions/49816306/symfony-serializer-deserialise-xml-which-contains-an-element-with-variable-amoun
The text was updated successfully, but these errors were encountered: