Skip to content

[Serializer] Problem when serializing/deserializing empty arrays in xml #43193

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
nikophil opened this issue Sep 27, 2021 · 3 comments
Closed

Comments

@nikophil
Copy link
Contributor

nikophil commented Sep 27, 2021

Symfony version(s) affected: all? actually testing on 5.3.4

Description
When serializing an object with an empty array in xml and then deserializing this array into the same class, I encounter an error.

How to reproduce

class Test
{
     public function __construct(private SerializerInterface $serializer)
    {
    }

    public function test() {
        $object = new class(){
            public array $array = [];
        };
        
        $data = $this->serializer->serialize($object, 'xml');

        /**
         * $data is:
         * <?xml version="1.0"?>
         * <response>
         *  <array/>
         * </response>
         */

        $this->serializer->deserialize($data, get_class($object), 'xml');

        // :boom: 
        //  [Symfony\Component\Serializer\Exception\NotNormalizableValueException]                                  
        //  The type of the "array" attribute for class "class@anonymous" must be one of "array" ("string" given). 
    }

(NB: the error is not due to the anonymous class)

@nikophil nikophil added the Bug label Sep 27, 2021
@nikophil nikophil changed the title [Serializer] Problem with serializing/deserializing empty arrays in xml [Serializer] Problem when serializing/deserializing empty arrays in xml Sep 27, 2021
@xabbuh
Copy link
Member

xabbuh commented Sep 27, 2021

Can you create a small example application that allows to reproduce your issue?

@nikophil
Copy link
Contributor Author

nikophil commented Sep 27, 2021

Hello @xabbuh and thanks for your answer

here is a reproducer

https://github.com/nikophil/serializer_error

just run bin/console app:test

@nikophil
Copy link
Contributor Author

Thanks for tackling that @alexandre-daubois

fabpot added a commit that referenced this issue Sep 29, 2021
…4.4) (alexandre-daubois)

This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer] Fix denormalizing XML array with empty body (4.4)

This happens for example with XML empty tags denormalizing to an object array attribute.

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #43193 on Symfony 4.4
| License       | MIT
| Doc PR        | _NA_

Not sure how to deal with versions differences as they differ just a little bit. Anyway, here's the fix for 4.4 and you can find the fix for 5.x on #43204 😄

Commits
-------

d6b7e2d [Serializer] Fix denormalizing of array with empty body
@fabpot fabpot closed this as completed Sep 29, 2021
fabpot added a commit that referenced this issue Sep 29, 2021
…5.x) (alexandre-daubois)

This PR was merged into the 5.3 branch.

Discussion
----------

[Serializer] Fix denormalizing XML array with empty body (5.x)

This happens for example with XML empty tags denormalizing to an object array attribute.

| Q             | A
| ------------- | ---
| Branch?       | 5.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #43193 on Symfony 5.x
| License       | MIT
| Doc PR        | _NA_

Fix for 4.4 can be found in #43205

Commits
-------

d94b7c1 [Serializer] Fix denormalizing of array with empty body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants