2
votes

I want to expose only a few properties of my User class, using JMSSerializerBundle and FOSRestBundle. It seems like the serializer bundle is not reading my configuration file.

My User class is in src/AppBundle/Entity/User and it extends the FOSUserBundle user class.

Here is my User class:

  <?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * User
 *
 * @ORM\Table(name="backoffice_user")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\UserRepository")
 */
class User extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="lastname", type="string", length=70)
     */
    private $lastname;

    /**
     * @var string
     *
     * @ORM\Column(name="firstname", type="string", length=70)
     */
    private $firstname;
}

This is my app/config.yml file

jms_serializer:
    metadata:
        debug: true
        auto_detection: true

And the yml mappgin file in src/AppBundle/Resources/config/serializer/Entity.User.yml :

AppBundle\Entity\User:
    exclusion_policy: ALL
    exclude: true
    properties:
        email:
            exclude: true
            expose: true

The file is not read (or at least not taken into account), because my API returns me all fields of my entity.

Am I forgetting something ?

2

2 Answers

8
votes

Your mapping file is not loaded, because the serializer applies the rules to the class where the properties are defined. In your case - that is FOS\UserBundle\Model\User. What you need is to override the Third-Party Metadata - a brief sample can be seen in bundle's documentation

In your config.yml, jms_serializer should probably look like this:

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            FOSUserBundle:
                namespace_prefix: "FOS\\UserBundle"
                path: "@AppBundle/Resources/config/serializer"

Inside directory serializer you should have a file named Model.User.yml with configuration like this:

FOS\UserBundle\Model\User:
    exclusion_policy: ALL
    # add your desired configuration below.
0
votes

In JMSSerializer you need to have one file for each class of the hierarchy.

So, the exclusion policy only apply to properties id, lastname and firstname; for BaseUser it use the default rule, which is to include everything.

To override a configuration file from another bundle, you first need to setup the folder for the namespace in the configuration with:

jms_serializer:
    metadata:
        directories:
            FOSUB:
                namespace_prefix: "FOS\\UserBundle"
                path: "%kernel.root_dir%/serializer/FOSUB"

And add the serializer config file in app/serializer/FOSUB/Model.User.yml. See the official docs for other configurations option.