JMS Serializer ignored my YML Entity exclusions
Asked Answered
M

2

7

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?

Malkamalkah answered 18/1, 2015 at 23:55 Comment(0)
M
0

This was helpful Using JMSSerialize to serialize Doctrine2 Entities that follow SimplifiedYamlDriver convention

It would appear that the file names needs to be completely different if you do not specify a namespace. I never thought to specify a namespace as this is not mentioned in the main docs.

If there is no namespace then the addMetaDir usage is fine but you also need to make sure your file names look like this

Vendor.NameOfBundle.Entity.Project.yml
Malkamalkah answered 19/1, 2015 at 11:23 Comment(1)
For anyone still struggling with this issue - I had this problem today, and take a look on this answer -> github.com/schmittjoh/JMSSerializerBundle/issues/… Going with footsteps, if you have a null path make a breakpoint/var_dump in vendor/jms/metadata/src/Driver/FileLocator.php in findFileForClass() method You should then see what is it calling to load your metadata. I have adjusted filenames accordingly.Primo
S
2

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.

Stites answered 19/1, 2015 at 7:53 Comment(4)
I have also tried, that, see my amended question. Adding the dir has no effect at all. Even when its the right dir. I am completely lost.Malkamalkah
@JakeN hm.. as i see JMS serializer chaches data and read it, have you tried to clear test enviroment cache? php app/console cache:clear -e testStites
This is why #19987422Malkamalkah
check out my answer to this, the file names need to be exactly right and the docs are not great and describing why.Malkamalkah
M
0

This was helpful Using JMSSerialize to serialize Doctrine2 Entities that follow SimplifiedYamlDriver convention

It would appear that the file names needs to be completely different if you do not specify a namespace. I never thought to specify a namespace as this is not mentioned in the main docs.

If there is no namespace then the addMetaDir usage is fine but you also need to make sure your file names look like this

Vendor.NameOfBundle.Entity.Project.yml
Malkamalkah answered 19/1, 2015 at 11:23 Comment(1)
For anyone still struggling with this issue - I had this problem today, and take a look on this answer -> github.com/schmittjoh/JMSSerializerBundle/issues/… Going with footsteps, if you have a null path make a breakpoint/var_dump in vendor/jms/metadata/src/Driver/FileLocator.php in findFileForClass() method You should then see what is it calling to load your metadata. I have adjusted filenames accordingly.Primo

© 2022 - 2024 — McMap. All rights reserved.