-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] deserialize from xml: Fix a collection that contains the only one element #27326
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
thank you @webnet-fr |
Thank you for your hint @nicolas-grekas ( There are still an strange error in tests for < PHP 7.0 which I am not sure how to deal with. Could you have a look, please? |
return true; | ||
} | ||
|
||
public function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null) |
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.
the "string" type hint is illegal here, branch 3.4 should work on PHP5.5
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.
(failure unrelated)
I'm not a fan of adding such quirks in the main normalizer hierarchy... But I've no better idea right now. Maybe should we introduce a specific XML normalizer instead? |
@dunglas: good enough as is? |
… only one element
Thank you @webnet-fr. |
…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
|
||
class SerializerCollectionDummy implements \Symfony\Component\Serializer\SerializerInterface, \Symfony\Component\Serializer\Normalizer\DenormalizerInterface | ||
{ | ||
/** @var \Symfony\Component\Serializer\Normalizer\DenormalizerInterface */ |
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 review 24b6848 and report any mistake
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.
Looks good to me
@webnet-fr as i wrote in issue 27279, provided above fix should be extended also for json-data Thanks |
In xml when parent node (
restaurants
) contains several children nodes with the same tag (restaurant
) it is clear that the children form a collection: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: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.