Question

My XML output looks wierd when I request for xml..

Controller:

use FOS\RestBundle\Controller\Annotations as REST;
class RestController {
    /**
     * @REST\View
     */
    public function getAgenciesAction() {
      return array("bb"=>array('zz'=>'vv'),'zz');
    }
}

Request header: Aceept: application/xml

Response:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <entry>
    <entry><![CDATA[vv]]></entry>
  </entry>
  <entry><![CDATA[zz]]></entry>
</result>

why is only the end node returned and not all the keys and values?

Was it helpful?

Solution

The xml serializer in the FOSRestBundle doesn't serialize arrays with the key. Each array entry will result as <entry> in the response, regardless of the key. The output in your example is correct. Keys are only in the json output relevant.

Serialized entities have the correct output, as the field will result in <field>value</field>

Example controller

/**
 * @ApiDoc(
 *     description="Returns the own user details",
 *     statusCodes={
 *         200="Returned when successful",
 *         403="Returned when missing permissions",
 *     }
 * )
 *
 * @Rest\Get("/users/me")
 * @Rest\View(serializerGroups={"details"})
 */
public function getMeAction()
{
    $user = $this->getUser();

    return array('user' => $user);
}

and the response. You see, the key user is outputted as <entry>.

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <entry>
    <id><![CDATA[517781e2e707a00217000000]]></id>
    <username><![CDATA[admin]]></username>
    <email><![CDATA[admin@example.com]]></email>
    <company><![CDATA[acme]]></company>
  </entry>
</result>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top