JMS Serializer ignored my YML Entity exceptions

My configuration

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

My Entity.Project.yml file called Entity.Project.yml contains

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

I load a serializer, for example, from inside the controller

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

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

But if I use this instead of the user subscriber, then the exceptions work

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

Even explicitly adding dir doesn't 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 documents are not clear how this path should be set. The above method is not an error, but does not draw in YML files. Method errors are described below and indicate that 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 to get JMS Serializer to view my YML file to exclude fields as well as use a subscriber?

+6
source share
2 answers

It was useful Using JMSSerialize to serialize Doctrine2 objects that follow the SimplifiedYamlDriver Convention

It looks like the file names should be completely different unless you specify a namespace. I never thought of specifying a namespace, as this is not mentioned in the main documents.

If there is no namespace, then using addMetaDir great, but you should also make sure your file names look like this:

 Vendor.NameOfBundle.Entity.Project.yml 
0
source

As I see from the documentation you need to configure Yaml files:

you must configure the metadata directory in which these files are located:

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

Read more in the manual.

+1
source

Source: https://habr.com/ru/post/981191/


All Articles