V
V
Valery2013-05-17 22:58:20
symfony
Valery, 2013-05-17 22:58:20

Why is JMSSerializerBundle ignoring annotations?

Good evening,
the above gang completely ignores annotations in entities.
For example:

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use FOS\UserBundle\Entity\User as BaseUser;

/**
 * Модель пользователя
 *
 * @ORM\Entity
 * @ORM\Table(name="user")
 *
 * @JMS\ExclusionPolicy("all")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMS\Expose
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="UserGroup")
     * @ORM\JoinTable(name="user_group_relation",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     * @JMS\Exclude
     */
    protected $groups;
}

To check that it works at all, a long separator is set in the settings:
jms_serializer:
    metadata:
        cache: file
        debug: "%kernel.debug%"
        auto_detection: true
    property_naming:
        separator: "____"
        lower_case: true

* Cache cleared. I tried to change the type, played with other parameters
. The output is:
[
    {
        "id": 3,
        "username": "Admin",
        "username____canonical": "admin",
        "email": "ОТРЕЗАНО",
        "email____canonical": "ОТРЕЗАНО",
        "enabled": true,
        "salt": "2ov3jq7rg12cwgck8kgs440wsc4swk8",
        "password": "e4de2c084971ef0183ac507bce41ee1d57949de86e94b498f1ba8acc4f192af310282a6981d06e96db2d32e7402fdf0ef4d664c97b034e8eb0d8f0ed81a228e7",
        "last____login": "2013-05-17T19:34:25+0400",
        "groups": [
        {
            "id": 1,
            "name": "Администратор",
            "roles": [
            "ROLE_CMS"
            ]
        }
        ],
        "locked": false,
        "expired": false,
        "roles": [
        ],
        "credentials____expired": false
    }
]

I made a composer update
Symphony now 2.1.10
I read somewhere that it conflicts with FOSRestBundle and it was recommended to put a special version there - it did not help.
It feels like I forgot something somewhere. Maybe someone came across?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery, 2013-06-04
@Akuma

Through a lot of googling, it turned out that the answer was very close. So why not put it on display, suddenly someone will come in handy.
Due to class inheritance from FOSUserBundle, the annotations did not work.
Here is a treasured topic with a solution: stackoverflow.com/questions/12960141/jmsserializerbundle-no-control-over-third-party-meta-data
You just need to create a yml config for the Model and user from FOSUserBundle.
* Symphony 2.3

L
Lebnik, 2019-01-29
@Lebnik

$serializer = SerializerBuilder::create()
            ->setSerializationContextFactory(function () {
                return \JMS\Serializer\SerializationContext::create()
                    ->setSerializeNull(true)
                    ;
            })
            ->setDeserializationContextFactory(function () {
                return \JMS\Serializer\DeserializationContext::create()
                    ->setSerializeNull(true)
                    ;
            })
            ->setAnnotationReader($annotationReader)
            ->setPropertyNamingStrategy(new SerializedNameImportantThanPropertyNameStrategy())
            ->build();

Ignoring some parent classes: CComponent, CModel, CActiveRecord
$class = new \ReflectionClass($serializer);
        $property = $class->getProperty('navigator');
        $property->setAccessible(true);
        $navigator = $property->getValue($serializer);

        $class = new \ReflectionClass($navigator);
        $property = $class->getProperty('metadataFactory');
        $property->setAccessible(true);
        $metadataFactory = $property->getValue($navigator);

        $class = new \ReflectionClass($metadataFactory);
        $property = $class->getProperty('loadedClassMetadata');
        $property->setAccessible(true);
        $property->setValue($metadataFactory, [
            'CComponent' => new NullMetadata(new \stdClass()),
            'CModel' => new NullMetadata(new \stdClass()),
            'CActiveRecord' => new NullMetadata(new \stdClass()),
        ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question