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

Mapping random key-value pair of XML payload in Symfony2 #470

Closed
bentcoder opened this issue Jul 22, 2015 · 3 comments
Closed

Mapping random key-value pair of XML payload in Symfony2 #470

bentcoder opened this issue Jul 22, 2015 · 3 comments

Comments

@bentcoder
Copy link

Hi,

Well, I hope this is correct place to ask this question unless it is more relevant to Symfony!

Key randoms below can contain any key-value pair so whatever you feed in (as long as it is in value-key pair) will be accepted. e.g. <tomato>nonsense</tomato>. However my mapping doesn't pick them up so I get an empty array for randoms. What am I missing?

Note: I'm using "jms/serializer-bundle": "0.13.0", composer package with Symfony2.

PAYLOAD

<?xml version="1.0" encoding="UTF-8"?>
<product id="1">
    <title>Samsung</title>
    <randoms>
        <review>710</review>
        <popularity>good</popularity>
    </randoms>
</product>

MODEL MAPPING - I listed all public $randoms; below I tried.

namespace Application\BackendBundle\Model\Api\Xml;

use JMS\Serializer\Annotation as Serializer;

/**
 * @Serializer\XmlRoot("product")
 */
class Product
{
    /**
     * @Serializer\Type("string")
     */
    public $title;

    /**
     * @Serializer\Type("array<string, string>")
     * @Serializer\XmlKeyValuePairs
     */
    public $randoms;

    /**
     * @Serializer\Type("array<string, string>")
     * @Serializer\XmlKeyValuePairs
     */
    public $randoms = [];

    /**
     * @Serializer\Type("array")
     * @Serializer\XmlKeyValuePairs
     */
    public $randoms = [];

    /**
     * @Serializer\Type("array")
     * @Serializer\XmlKeyValuePairs
     */
    public $randoms;

    /**
     * @Serializer\Type("array")
     */
    public $randoms;

    /**
     * @Serializer\Type("array")
     */
    public $randoms = [];
}

JSON RESPONSE

{
    "title": "Samsung",
    "randoms": []
}
@bentcoder
Copy link
Author

Issues barely get answers here but I hope someone answers this question!

@sstok
Copy link

sstok commented Jul 25, 2015

You have defined the mapping of Randoms multiple times, you can only define this once as the last one will overwrite the previous ones.

I'm not sure its possible to set multiple accepted types in @Serializer\Type("array<string, string>")
Setting this annotation multiple times simple overwrites it.

There are a big number of open issues in https://github.com/schmittjoh/serializer/issues
So I don't think you will get an answer soon..

Also

    /**
     * @Serializer\Type("array")
     */
    public $randoms;

    /**
     * @Serializer\Type("array")
     */
    public $randoms = [];

This is really weird, if $ransoms must be an array set the default value to an array.

@bentcoder
Copy link
Author

@sstok - As I noted in my question "I listed all public $randoms; below I tried.", they are for demonstration purposes to give you guys an idea what I've tried before so I actually have only one definition in my model which is:

    /**
     * @Serializer\Type("array<string, string>")
     * @Serializer\XmlKeyValuePairs
     */
    public $randoms;

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

3 participants