Skip to content
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

Array deserialization problem #597

Closed
nikophil opened this issue Aug 1, 2017 · 3 comments
Closed

Array deserialization problem #597

nikophil opened this issue Aug 1, 2017 · 3 comments

Comments

@nikophil
Copy link

nikophil commented Aug 1, 2017

Hello,

i think i encountered a weird issue in the xml deserialization.

I got a Feed class that contains a collection of Products. These products have an array of string (entry = ean.

Here is my simplified model :

<?php

use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation as JMS;

/**
 * Class Feed
 * @JMS\XmlRoot("Feed")
 */
class Feed
{
    /**
     * @var ArrayCollection<Product>
     * @JMS\SerializedName("Products")
     * @JMS\XmlList(inline=false, entry="Product")
     * @JMS\Type("ArrayCollection<Product>")
     */
    private $products;
    
    public function __construct()
    {
        $this->products   = new ArrayCollection();
    }

    /**
     * @return ArrayCollection
     */
    public function getProducts()
    {
        return $this->products;
    }

    /**
     * @param ArrayCollection $products
     */
    public function setProducts($products)
    {
        $this->products = $products;
    }

    /**
     * @param Product $product
     */
    public function addProduct($product)
    {
        if (!$this->products->contains($product)) {
            $this->products->add($product);
        }
    }
<?php

use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation as JMS;

/**
 * Class Product
 * @JMS\XmlRoot("Product")
 */
class Product
{
    /**
     * @var array
     * @JMS\SerializedName("EANs")
     * @JMS\XmlList(inline=false, entry="EAN")
     * @JMS\Type("array<string>")
     */
    private $EANs;

    public function __construct()
    {
        $this->eans = [];
    }
}

When i create the xml from the php model, i got exactly what i want :

<?php
        $feed = new Feed();
        $product = new Product();
        $product->setName('Name');
        $product->setEANs(['1', '2', '3']);
        $feed->addProduct($product);

        $serializer = SerializerBuilder::create()->build();
        $feedSerialized=$serializer->serialize($feed, 'xml');

Results in :

<?xml version="1.0" encoding="UTF-8"?>
<Feed>
  <Products>
    <Product>
      <Name><![CDATA[Name]]></Name>
      <EANs>
        <EAN><![CDATA[1]]></EAN>
        <EAN><![CDATA[2]]></EAN>
        <EAN><![CDATA[3]]></EAN>
      </EANs>
    </Product>
  </Products>
</Feed>

But then, when i deserialize back this feed :

<?php
        $feedUnserialized = $serializer->deserialize($feedSerialized, Feed::class, 'xml');

My product->eans is an empty array...

The weird thing is when i work with only one product (no feed, no array collection) my eans array is well hydrated :

<?php
        $product = new Product();
        $product->setName('Name');
        $product->setEANs(['1', '2', '3']);
        $productSerialized=$serializer->serialize($product, 'xml');
        $productUnserialized=$serializer->deserialize($productSerialized, Product::class, 'xml');

I got the expected xml, and then the expected EANs hydrated in my $productUnserialized object.

Is that a bug from the deserializer ?

Thanks for helping

@goetas
Copy link
Collaborator

goetas commented Aug 1, 2017

Can you try to reproduce it in a failing test case ? a good place to start is https://github.com/schmittjoh/serializer/blob/master/tests/Serializer/XmlSerializationTest.php#L221

@nikophil
Copy link
Author

nikophil commented Aug 1, 2017

Hello,
and thanks for your answer.
While trying to reproduce the bug in a test case, i discovered from where the bug is coming from :
i have a "namespace" annotation on my "Feed" class, with the uri attribute linking to an xsl file.
Is the serializer checking that xsl schema definition ?
or that still a bug ? (i got the test case if you want)

@goetas
Copy link
Collaborator

goetas commented Aug 1, 2017

i have a "namespace" annotation on my "Feed" class, with the uri attribute linking to an xsl file.
Is the serializer checking that xsl schema definition ?

depends on what you have exactly as namespace... but in general yes..

Closing

@goetas goetas closed this as completed Aug 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants