简体   繁体   中英

FOSRestBundle output wrong xml

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM