Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS Serializer ignored my YML Entity exclusions

My config is

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            NameOfBundle:
                namespace_prefix: ""
                path: "@VendorNameOfBundle/Resources/config/serializer"

My YML file named Entity.Project.yml contains

Vendor\NameOfBundle\Entity\Project:
    exclusion_policy: ALL
    properties:
        id:
            expose: true

I am loading the serializer like so from within a Controller

$serializer = SerializerBuilder::create()
    ->configureListeners(function(EventDispatcher $dispatcher) {
        $dispatcher->addSubscriber(new ProjectSubscriber($this->container));
    })
    ->addDefaultListeners()
    ->build();

This completely ignored my YML file and exposes all fields from the Project. I have cleared the cache.

But if I use this instead without the custom subscriber, then the exclusions work

 $serializer = $this->get("jms_serializer");

Even explicitly adding a dir does not work either

$serializer = SerializerBuilder::create()
    ->configureListeners(function(EventDispatcher $dispatcher) {
        $dispatcher->addSubscriber(new ProjectSubscriber($this->container));
    })
    ->addDefaultListeners()
    ->addMetadataDir(realpath($this->get('kernel')->getRootDir()."/../") . '/src/Vendor/NameOfBundle/Resources/config/serializer')        
    ->build();

The docs are not clear on how this path should befined. The above method does not error, but does not pull in the YML files. The below method errors and says the directory does not exist;

$serializer = SerializerBuilder::create()
    ->configureListeners(function(EventDispatcher $dispatcher) {
        $dispatcher->addSubscriber(new ProjectSubscriber($this->container));
    })
    ->addDefaultListeners()
    ->addMetadataDir('@VendorNameOfBundle/Resources/config/serializer')
    ->build();

How do I make the JMS Serializer look at my YML file in order to exclude the fields and also use the Subscriber?

like image 306
Jake N Avatar asked Jan 18 '15 23:01

Jake N


1 Answers

As i see from documentation you need to setup your Yaml files:

it is necessary to configure a metadata directory where those files are located:

$serializer =
    JMS\Serializer\SerializerBuilder::create()
        ->addMetadataDir($someDir)
        ->build();

For more information read manual.

like image 189
Peter Popelyshko Avatar answered Oct 01 '22 05:10

Peter Popelyshko