schmittjoh / JMSSerializerBundle

Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
http://jmsyst.com/bundles/JMSSerializerBundle
MIT License
1.8k stars 313 forks source link

Xml serializer doesn't work on custom FOSUserBundle model #475

Open edouardkombo opened 9 years ago

edouardkombo commented 9 years ago

Hi,

I'm stucked since four days with a strange issue, this is my yml config of Jms Serializer (1.0.0). I need to override the FOSUserBundle serializer to apply some constraints to the user api.

Inside the SonataUserBundle, I've overrided "Application/Sonata/UserBundle/Resources/config/serializer/FOSUserBundle/Model.User.xml"

Inside my JMSSerializer, I can access FOSUserBundle user properties with no problems.

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            ApplicationSonataUserBundle:
                path: %kernel.root_dir%/../vendor/sonata-project/user-bundle/Resources/config/serializer/FOSUserBundle
                namespace_prefix: FOS\UserBundle

But, when I target my own xml serializer (Application/Sonata/UserBundle/Resources/config/serializer/FOSUserBundle/) properties, with this configuration.

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            ApplicationSonataUserBundle:
                path: %kernel.root_dir%/../src/Application/Sonata/UserBundle/Resources/config/serializer/FOSUserBundle
                namespace_prefix: FOS\UserBundle

I get this error:

Could not find class FOS\UserBundle\Model\User inside XML element

Then, when I change the namespace prefix, like this.

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            ApplicationSonataUserBundle:
                path: %kernel.root_dir%/../src/Application/Sonata/UserBundle/Resources/config/serializer/FOSUserBundle
                namespace_prefix: Application\Sonata\UserBundle

I get this error:

Warning: in_array() expects parameter 2 to be array, null given
in vendor/sonata-project/core-bundle/Form/Type/DoctrineORMSerializationType.php at line 95

if (!in_array($this->group, $propertyMetadata->groups)) {
    continue;

}

This last error is shown no matter what kind of other namespace or path I write.

This is my Application/Sonata/UserBundle/Resources/config/serializer/FOSUserBundle/Model.User.xml file

<?xml version="1.0" encoding="UTF-8"?>
<serializer>

    <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
                        http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

        <class name="Application\Sonata\UserBundle\Model\User" access_type="property" exclusion-policy="all" xml-root-name="user">

            <property name="username"            type="string"   expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" >
                <constraint name="NotBlank" />
                <constraint name="Length">
                    <option name="min">4</option>
                    <option name="max">20</option>
                    <option name="minMessage">Your username must contain at least {{ limit }} characters</option>
                    <option name="maxMessage">Your username must contain {{ limit }} characters maximum</option>
                </constraint>
            </property>

            <property name="usernameCanonical"   type="string"   expose="true" since-version="1.0" groups="sonata_api_read,sonata_search" />

            <property name="email"               type="string"   expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" >
                <constraint name="NotBlank" />
                <constraint name="Email">
                    <option name="message">'{{ value }}' is not a valid user email address.</option>
                    <option name="checkMX">true</option>
                </constraint>
            </property>

            <property name="emailCanonical"      type="string"   expose="true" since-version="1.0" groups="sonata_api_read,sonata_search" />
            <property name="enabled"             type="boolean"  expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />

            <property name="plainPassword"       type="string"   expose="true" since-version="1.0" groups="sonata_api_write" >
                <constraint name="NotBlank" />
                <constraint name="Length">
                    <option name="min">6</option>
                    <option name="max">20</option>
                    <option name="minMessage">Your password must contain at least {{ limit }} characters</option>
                    <option name="maxMessage">Your password must contain {{ limit }} characters maximum</option>
                </constraint>
            </property>

            <property name="lastLogin"           type="DateTime" expose="true" since-version="1.0" groups="sonata_api_read,sonata_search" />
            <property name="locked"              type="boolean"  expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
            <property name="expired"             type="boolean"  expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
            <property name="expiresAt"           type="DateTime" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
            <property name="confirmationToken"   type="string"   expose="true" since-version="1.0" groups="sonata_api_read,sonata_search" />
            <property name="passwordRequestedAt" type="DateTime" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
            <property name="roles"               type="array"    expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
            <property name="credentialsExpired"  type="boolean"  expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
            <property name="credentialsExpireAt" type="DateTime" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
            <property name="creationIp"          type="string"   expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />
            <property name="lastAccessIp"        type="string"   expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search" />

        </class>
    </constraint-mapping>
</serializer>

Does anyone encountered the same problem, or can simply guide me through the light?

rsister88 commented 8 years ago

I get this error:

Could not find class FOS\UserBundle\Model\User inside XML element

I know it's an old issue, but if someone is getting this error, simply put this code inside your Model.User.xml file:

    <class name="FOS\UserBundle\Model\User" exclusion-policy="all" xml-root-name="user">

    </class>

To add some extra information "just in case", I had to extend both config files: Model.User.xml and Entity.User.xml. Inside the first one I configured Sonata and FOS entities fields and inside the second one my custom user entity fields.