Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 253
Yuriy Sorokin Avatar asked Sep 27 '22 08:09

Yuriy Sorokin


1 Answers

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

like image 116
Carlos Martinez Ival Avatar answered Oct 06 '22 00:10

Carlos Martinez Ival