0

I have configured JMS Serializer like this:

jms_serializer:
    visitors:
        xml_serialization:
            format_output: '%kernel.debug%'
    metadata:
        auto_detection: true
        directories:
            App:
                namespace_prefix: "App\\Entity"
                path: "%kernel.root_dir%/serializer"

This is how metadata config in src/serializer/SystemUser.yml looks like for entity SystemUser:

App\Entity\Api\Auth\SystemUser:
    exclusion_policy: ALL
    properties:
        id:
            expose: true
        password:
            expose: false
        username:
            expose: true
        email:
            expose: true
        last_login:
            expose: true

I specifically used .yml extension because it is mentioned in the documentation that .yml extension must be used here:

https://jmsyst.com/bundles/JMSSerializerBundle/2.x/configuration#defining-metadata

I have entity in src/Entity/Api/Auth/SystemUser.php that looks like this:

<?php

namespace App\Entity\Api\Auth;

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

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class SystemUser extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

But whatever property I exclude in SystemUser.yml file the output does not changes. It displays all properties.

I am using latest Symfony 4.3

Caslav Sabani
  • 421
  • 6
  • 20

1 Answers1

1

Your file src/serializer/SystemUser.yml has a wrong name. With namespace_prefix: "App\Entity" and your SystemUser.php namespace it should be Api.Auth.SystemUser.yml

This is because your User extends of BaseUser. You need do this:

jms_serializer.yaml

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            App:
                namespace_prefix: 'App\Entity'
                path: '%kernel.root_dir%/serializer'
            FOSUB:
                namespace_prefix: 'FOS\UserBundle'
                path: '%kernel.root_dir%/serializer'

src/serializer/Model.User.yml

FOS\UserBundle\Model\User:
    exclusion_policy: ALL
    properties:
        id:
            exclude: false
        username:
            exclude: false
Ihor Kostrov
  • 2,361
  • 14
  • 23
  • What should be the name of src/serializer/SystemUser.yml I have renamed it to Api.Auth.SystemUser.yml but it still does not work. Nothing changes. – Caslav Sabani Aug 27 '19 at 16:43
  • Cleared cache successfully still nothing changes. – Caslav Sabani Aug 27 '19 at 16:49
  • I am using fos rest bundle. This is how controller method looks like: public function getById(SystemUser $user) { return $this->handleView($this->view($user)); } – Caslav Sabani Aug 27 '19 at 16:50
  • I am not getting any errors. But the response stays the same nothing changes. When I change namespace in Api.Auth.SystemUser.yml then I get error until I correct it to real namespace so it looks like it is loading that file but not applying any serialization rules. – Caslav Sabani Aug 27 '19 at 16:56
  • Awesome, thank you very much. I have tried all the options and combinations but no luck. Please if you can try it with entity that extends FosUserBundle User entity and try to have the entity in more subfolders maybe that is causing the issue. – Caslav Sabani Aug 27 '19 at 17:05
  • It works very weird. I changed `namespace_prefix: "App\\Entity"` to `namespace_prefix: 'App\Entity'`, clered couple times cache and this works for me. – Ihor Kostrov Aug 27 '19 at 17:54
  • This still does not work for me. I have cleared cache several times with php bin/console cache:clear and I changed to 'App\Entity' but everything is the same. Here is the link to the repo if you are willing to check it out and test it: https://bitbucket.org/hardcoremore/zhuko-test-api/src/master/ I don't know what else to do – Caslav Sabani Aug 28 '19 at 10:14
  • 1
    Updated answer. – Ihor Kostrov Aug 28 '19 at 17:03
  • Finally it works!!! Thanks so much. But at the end I only needed to include src/serializer/Model.User.yml file my Api.Auth.SystemUser.yml file was totally ignored even though SystemUser entity extends the FOS User entity. So the correct answer at the end is that JMS Serializer bundle will not serialize parent classes properties defined in child class configuration file like I did with Api.Auth.SystemUser.yml – Caslav Sabani Aug 29 '19 at 09:03