Yuriy Sorokin
Yuriy Sorokin

Reputation: 51

FOSUserBundle + JMSSerializerBundle + exposing extra fields

I have an issue when serializing a User instance with one additional field $name, which extends the base User from FOSUserBundle:

<?php
namespace AppBundle\Entity\User;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * User
 */
class User extends BaseUser
{
    /**
     * @var string
     */
    private $name;

    /**
     * Set name
     * @param string $name
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

To simplify, I need to expose only $salt field from User entity using JMSSerializerBundle

#AppBundle\Resources\config\serializer\Model.User.yml
FOS\UserBundle\Model\User:
    exclusion_policy: all
    properties:
        salt:
            expose: true

Here's the config for it:

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

The issue is that the serializer exposes also $name field, which I don't want as I need only to have $salt exposed:

{
    "salt": "abcdefg",
    "name": "Admin"
}

I believe I need to tell the serializer to use a config for my AppBundle\Entity\User instead of the base user entity from FOSUserBundle, but I have no clue how to implement it.

Upvotes: 5

Views: 3199

Answers (2)

Carlos Martinez Ival
Carlos Martinez Ival

Reputation: 127

This is how I solved the issue . I have a User.php class that inherit from FOS\UserBundle\Model\User as BaseUser. I need control the serialization from both my BaseUser class and my User class.

Solution: you need 2 separated config file to control each class.

config.yml

#Serializer configuration
    jms_serializer:
        metadata:
            directories:
                AppBundle:
                    path: "@AppBundle/Resources/config/serializer"
                FOSUB:
                    namespace_prefix: "FOS\\UserBundle"
                    path: "%kernel.root_dir%/serializer/FOSUB"

Model.User.yml

FOS\UserBundle\Model\User:
    exclusion_policy: ALL
    properties:
        id:
            expose: true
        username:
            expose: true
        email:
            expose: true
        enabled:
            expose: true

Entity.User.yml

AppBundle\Entity\User:
    exclusion_policy: ALL
    properties:
        imageAvatar:
            expose: true
        updatedAt:
            expose: true



namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;



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


   /**
     * @ORM\Column(type="string", length=255)
     *
     * @var string
     */
    private $imageAvatar;



    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTime
     */
    private $updatedAt;

Clear the cache after each change in the configuration files.

cache:clear

Upvotes: 4

D_R
D_R

Reputation: 4962

It happens because you are using the exclusion_policy: all on the parent entity, instead of on the child entity, which still exposing all of it's properties.

you should switch to your bundle in the configuration (config.yml) under the jms_seriazlier:directories.

any-name:
    namespace_prefix: "My\\FooBundle"
    path: "@MyFooBundle/Resources/config/serializer"

now you could use the same configuration to expose only the desired property.

#AppBundle\Resources\config\serializer\Entity.User.yml
My\FooBundle\Entity\User:
    exclusion_policy: all
    properties:
        salt:
            expose: true

Upvotes: 0

Related Questions