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

xml-root-name not working #262

Closed
marcospassos opened this issue Jan 15, 2013 · 12 comments
Closed

xml-root-name not working #262

marcospassos opened this issue Jan 15, 2013 · 12 comments

Comments

@marcospassos
Copy link

I have:

<?xml version="1.0" encoding="UTF-8" ?>
<serializer>
    <class name="Colibri\Bundle\ProductBundle\Model\Option\Option" xml-root-name="option" accessor-order="custom" custom-accessor-order="id,type" exclusion-policy="ALL" access-type="public_method">
        <virtual-property name="type" expose="true" method="getType" />
        <property name="id" expose="true" read-only="true" />
        <property name="name" expose="true" />
        <property name="displayName" expose="true" />
    </class>
</serializer>

Result:

<result>
    <id>1</id>
    <name>
    <![CDATA[ Sabores ]]>
    </name>
    <displayname>
     <![CDATA[ Sabores ]]>
    </displayname>
</result>

I forgot something?

@wjaspers
Copy link

That's odd, because it works correctly for PHP annotations.


use JMS/Serializer/Annotation as Serializer;

/**
 * @Serializer\XmlRoot("option")
 */
class Thingy {
  /**
   * @Serializer\Expose
   * @Serializer\SerializedName("id")
   */
  protected $id = 1;

  /**
   * @Serializer\Expose
   * @Serializer\SerializedName("name")
   */
  protected $name = 'hello';

  /**
   * @Serializer\Expose
   * @Serializer\SerializedName("displayName")
   */
  protected $displayName = 'Hello';
}

To test:

$thingy = new Thingy();
$out = $serializer->serialize($thingy, 'xml');
var_dump($out);
<option>
  <id><![CDATA[1]]></id>
  <name><![CDATA[hello]]></name>
  <displayName><![CDATA[Hello]]></displayName>
</option>

@brunoroux
Copy link

Actually I'm having exact same behavior with php annotations :

<?php

namespace GMP\CommonAdminBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use JMS\Serializer\Annotation as JMS;

/**
 * Article
 *
 * @ORM\Table(name="articles")
 * @ORM\Entity(repositoryClass="GMP\CommonAdminBundle\Entity\ArticleRepository")
 * @Gedmo\SoftDeleteable(fieldName="deleted")
 * @JMS\ExclusionPolicy("all")
 * @JMS\XmlRoot("article")
 */
class Article
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMS\Expose
     * @JMS\SerializedName("id")
     */
    private $id;
}

which outputs as :

<entry>
<id>1</id>
</entry>

@dupuchba
Copy link

Same error here...

@dupuchba
Copy link

@wjaspers what is you version of the bundle and the serializer? I have reproduced your exactly your User.php, and it is not working properly!

@coccovax
Copy link

when having a collection XmlRoot does not work.
Having this:

<?php
...
public function indexAction()
{
  $entities = $this->getDoctrine()->getManager()->getRepository('BlahBlahBundle:Entity')->findAll();
  $serializer = $this->get('serializer');
  $out = $serializer->serialize($entities, 'xml');
  var_dump($out);
  die;
}
...
?>

we have this output:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <entry id="11">
    <origin_site><![CDATA[mainsite]]></origin_site>
    <first_name><![CDATA[foo]]></first_name>
    <last_name><![CDATA[bar]]></last_name>
    <email><![CDATA[[email protected]]]></email>
  </entry>
</result>

and the Entity class is annotated in this way:

<?php
.....
use JMS\Serializer\Annotation as Serializer;

/**
 * Entity
 *
 * @Serializer\XmlRoot("entity_tag")
 */
class Entity {
....

@hd-deman
Copy link

+1
use

/**
 * @Serializer\XmlRoot("name")

get

<entry>
<id>1</id>
</entry>

@ebonhand09
Copy link

This is also affecting me

@alexkappa
Copy link

Same here, using yaml.

@brunoroux
Copy link

I took a look at this bug.
Found out that in JMS\Serializer\XmlSerializationVisitor entryName is set to default value "entry" because currentMetadata is null
Probably is the same in YamlSerializationVisitor or in JsonSerializationVisitor.

@schmittjoh : Any idea where I should look at to solve this ? Couldn't figure out what should init metadata in those classes.

@schmittjoh
Copy link
Owner

@XmlRootName only applies to the "root" node i.e. your object is directly passed to the serialize() call. It is not applied if your object is serialized as part of a collection f.e. as reported by some.

If there is an issue with @XmlRootName not being used when your object is the root node, a first step would be to create a failing test case on the schmittjoh/serializer repository and request a pull.

I'm closing this here as it also looks more like a misunderstanding of what @XmlRootName means. If you still think, it's an issue please create a new issue on schmittjoh/serializer containing:

  1. the data that you are serializing
  2. the expected output of the serialize() call
  3. the actual output of the serialize() call

@montera82
Copy link

this is still an issue in jms 1.1

@MvanRietschoten
Copy link

MvanRietschoten commented Nov 14, 2017

For the people who are here, because they think it should set the name for their array/ArrayCollection children take a look at https://jmsyst.com/libs/serializer/master/reference/annotations#xmllist
Put @xmllist(inline = true, entry = "comment") above your @type annotation with a correct entry name and see that it works ;) Love this bundle :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests