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 request payload works for JSON but not for XML #820

Closed
bentcoder opened this issue Sep 19, 2017 · 4 comments
Closed

Mapping request payload works for JSON but not for XML #820

bentcoder opened this issue Sep 19, 2017 · 4 comments

Comments

@bentcoder
Copy link

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

I have a model which is used to map JSON and XML payloads. JSON mapping works fine but XML doesn't. I asked similar question here but it was ignored with a passion!

Property random contains random/unknown/dynamic properties so I can not hardcode them. How do you map such properties?

Thanks

MODEL

/**
 * @Serializer\XmlRoot("data")
 */
class Data
{
    /**
     * @Serializer\Type("array<string>")
     * @Serializer\XmlValue
     */
    public $random;
}

JSON

{
  "random": {
    "hello": "world",
    "colour": "blue",
    "age": "19"
  }
}

RESULT

Data Object
(
      [random] => Array
          (
              [hello] => world
              [colour] => blue
              [age] => 19
          )
)

XML

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <random>
        <hello>world</hello>
        <colour>blue</colour>
        <age>19</age>
    </random>
</data>

RESULT

Data Object
(
      [random] => Array
          (
          )
)

This won't map either:

@Serializer\Type("array<string, string>")
@Serializer\XmlKeyValuePairs
@goetas
Copy link
Collaborator

goetas commented Sep 19, 2017

Random has to be a type. you should create a class that contains the fields hello, color, age. can't be just a plain array.

An alternative solutioni is custom type handler.

/**
 * @Serializer\XmlRoot("data")
 */
class Data
{
    /**
     * @Serializer\Type("MyType")
     */
    public $random;
}
class MyHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        return array(
            array(
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
                'format' => 'xml',
                'type' => 'MyType',
                'method' => 'deserializeMyType'
            ),
            array(
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
                'format' => 'xml',
                'type' => 'MyType',
                'method' => 'serializeMyType'
            )
        );
    }

    public function serializeAnyType(XmlSerializationVisitor $visitor, $data, array $type, Context $context)
    {
        // serialize your object here
    }

    public function deserializeAnyType(XmlDeserializationVisitor $visitor, $data, array $type)
    {
        // deserialize your object here
    }
}

@goetas goetas closed this as completed Sep 19, 2017
@goetas
Copy link
Collaborator

goetas commented Sep 19, 2017

Not sure which is the format for XmlKeyValuePairs, will re-open for now

@goetas goetas reopened this Sep 19, 2017
@bentcoder
Copy link
Author

For unknown class properties: all we need is to achieve result below after sending an appropriate JSON and XML request payload.

Data Object
(
      [random] => Array
          (
              [hello] => world
              [colour] => blue
              [age] => 19
          )
)

If the model below works (it does!) for JSON then it should also work for XML by default.

/**
 * @Serializer\XmlRoot("data")
 */
class Data
{
    /**
     * @Serializer\Type("array<string>")
     * @Serializer\XmlValue
     */
    public $random;
}

I am happy to change the model as long as I can make XML mapping happy so I would be greateful if you could show me an example.

Thanks

@goetas
Copy link
Collaborator

goetas commented Sep 22, 2017

checked right now, xmlKeyValuePairs is not implemented when de-serializing... will be happy to accept a pull request!

fdyckhoff added a commit to dyvelop/serializer that referenced this issue Nov 22, 2017
fdyckhoff added a commit to dyvelop/serializer that referenced this issue Nov 24, 2017
relates to schmittjoh#820

deserialize arrays with key-value pairs

add test case with other values from default

set current metadata from stack in serialization context
@goetas goetas added this to the 1.12.0 milestone May 28, 2018
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

2 participants