I have the SonataUserBundle
and JMSSerializerBundle
set up and running. Now i am trying to override the serializer configuration for Application\Sonata\UserBundle\Entity\User.
The XML file i am using for this configuration was generated by the SonataEasyExtendsBundle
in:
Application\Sonata\UserBundle\Resources\config\serializer\Entity.User.xml
and looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<!--
This file has been generated by the EasyExtends bundle
( http://sonata-project.org/bundles/easy-extends )
@author <yourname> <youremail>
-->
<class name="Application\Sonata\UserBundle\Entity\User" exclusion-policy="all" xml-root-name="user">
<property name="id" type="integer" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
</class>
</serializer>
I also configured app/config/config.yml
to use that XML file:
jms_serializer:
metadata:
auto_detection: true
directories:
ApplicationSonatUserBundle:
path: @ApplicationSonataUserBundle/Resources/config/serializer
namespace_prefix: Application\Sonata\UserBundle\Model
Now according to the configuration i should only see the id field when the object gets serialized. But when i test this all fields are exposed. This is obviously a bad idea.
I am using FOSRestBundle
to implement the API layer where this is being used. And if i run:
get_class($this->getUser())
//outputs Application\Sonata\UserBundle\Entity\User
So FOSUserBundle
is using the right User
entity.
I have been looking at this for a couple of hours and can't find what's going wrong...
finally solved this, apparently you must reference the class where the properties actually reside. In this case i was trying to expose the property id which resides in FOS/UserBundle/Model/Entity. I ended up configuring properties for both Sonata/UserBundle/Model/User and the FOS user class. My XML now looks like this:
<serializer>
<class name="Sonata\UserBundle\Model\User" exclusion-policy="all" xml-root-name="user">
<property name="dateOfBirth" type="DateTime" expose="true" since-version="1.0" groups="profile" />
<property name="firstname" type="string" expose="true" since-version="1.0" groups="profile" />
<property name="lastname" type="string" expose="true" since-version="1.0" groups="profile" />
<property name="website" type="string" expose="true" since-version="1.0" groups="profile" />
<property name="locale" type="string" expose="true" since-version="1.0" groups="profile" />
<property name="phone" type="string" expose="true" since-version="1.0" groups="profile" />
</class>
<class name="FOS\UserBundle\Model\User" exclusion-policy="all" xml-root-name="user">
<property name="id" type="integer" expose="true" since-version="1.0" groups="profile" />
<property name="username" type="string" expose="true" since-version="1.0" groups="profile" />
<property name="email" type="string" expose="true" since-version="1.0" groups="profile" />
<property name="roles" type="array" expose="true" since-version="1.0" groups="profile" />
</class>
</serializer>