2

I'm trying to build an API with FOSRestBundle and JMSSerializerBundle on Symfony2. I succeeded to run it with json output perfectly. But the XML output is not good. It runs, produce an XML data, but the data is useless because of the xml tag names.

PHP Code for Controller:

use JMS\Serializer\Annotation;

....
....
....

/**
 * Version
 * @return Response
 *
 * @Annotation\XmlKeyValuePairs
 *
 * @ApiDoc(
 *  resource=true,
 *  description="Method for API Version",
 *  statusCodes={
 *      200="Success"
 *  },
 *  parameters={
 *  }
 * )
 */
public function versionAction () {
    $response = [
        'code'    => 200,
        'message' => 'api version',
        'data'    => [
            'version'    => '0.0.1',
            'lastupdate' => '2015-11-21 23:29:00',
            'now'        => (new \DateTime())->format('Y-m-d H:i:s')
        ]
    ];

    return $response;
}

XML Output:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <entry>200</entry>
  <entry><![CDATA[api version]]></entry>
  <entry>
    <entry><![CDATA[0.0.1]]></entry>
    <entry><![CDATA[2015-11-21 23:29:00]]></entry>
    <entry><![CDATA[2015-11-22 00:41:30]]></entry>
  </entry>
</result>

The problem here is the XML tags are all "entry". What I want is like this:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <code>200</code>
  <message>api version</message>
  <data>
    <version>0.0.1></version>
    <lastupdate>2015-11-21 23:29:00</lastupdate>
    <now>2015-11-22 00:41:30</now>
  </data>
</result>

I'm using @Annotation\XmlKeyValuePairs as documentation says, but couldn't get the result I want. There is a fix for that on here and it's working, but it's not a good solution because the file is from composer.

I dont know if I'm missing something or it's a bug in JMSSerializerBundle, or Symfony2 or do I have to use another annotation to get XmlKeyValuePairs working?

Tugrul
  • 71
  • 4
  • By the way, I can use @XmlAttributeMap annotation, for `` output. But it's not working too. – Tugrul Nov 21 '15 at 23:22

1 Answers1

0

Take a look on something like this https://github.com/schmittjoh/JMSSerializerBundle/issues/59#issuecomment-374888667 This was a big issue for me to

<?php

namespace Ekv\System\Tmp\JmsSerializer;

use Doctrine\Common\Annotations\AnnotationRegistry;
use JMS\Serializer\Annotation\XmlKeyValuePairs;
use JMS\Serializer\Annotation\XmlList;
use JMS\Serializer\Annotation\XmlRoot;

/** @XmlRoot("api_result") */
class JmsXmlSerModel
{
    /**
     * @var array
     * @XmlList(inline = true, entry = "item")
     * @XmlKeyValuePairs
     */
    private $array = array(
        'key-one' => 'foo',
        'key-two' => 1,
        'nested-array' => array(
            'bar' => 'foo',
        ),
        'without-keys' => array(
            1,
            'test'
        ),
        'mixed' => array(
            'test',
            'foo' => 'bar',
            '1_foo' => 'bar'
        ),
        1 => 'foo'
    );

    public function __construct($inArr)
    {
        $this->array = $inArr;
        $this->init();
    }

    protected function init()
    {
        new XmlRoot();
        new XmlList();
        new XmlKeyValuePairs();
//      https://stackoverflow.com/q/14629137/1101589
//        \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
//            'JMS\Serializer\Annotation',
//            PATH_ROOT.'/vendor/jms/serializer/src'
//        );
    }

    public static function usage($inputArray)
    {
        $serializer = \JMS\Serializer\SerializerBuilder::create()->build();
        $xml = $serializer->serialize(new self($inputArray), 'xml');
        //pa($res);
        pa($xml);exit;
    }
}
Yehor
  • 6,203
  • 3
  • 20
  • 28