Question

Is it possible to use auto_detection for some bundles, while overriding the location of third-party models by specifying directories for them?

I've been playing with the JMSSerializerBundle the past couple of days trying to understand it and get it to work with a personal project, and it doesn't look like there is any way to use auto_detection as well as specify directories for third-party metadata.

If I write my FOSUserBundle Model.User.yml serialization data, e.g.:

FOS\UserBundle\Model\User:
    access_type: property
    exclusion_policy: ALL
    xml_root_name: user
    properties:
        username:
            type:     string
            expose:   true

And place it in the directory specified, while auto_detection is set to true, like so:

jms_serializer:
  metadata:
    debug: true
    file_cache:
      dir: "%kernel.cache_dir%/serializer"
    auto_detection: true
    directories:
      FOSUserBundle:
        namespace_prefix: FOS\UserBundle
        path: "@MyBundle/config/serializer/fosuser"

The serialization metadata is not picked up.

However, if I place the same metadata file under FOS/UserBundle/Resources/config/serializer (in the vendor bundle) and change the configuration to:

jms_serializer:
  metadata:
    debug: true
    file_cache:
      dir: "%kernel.cache_dir%/serializer"
    auto_detection: true
    #directories:
    #  FOSUserBundle:
    #    namespace_prefix: FOS\UserBundle
    #    path: "@MyBundle/config/serializer/fosuser"

The serialization metadata is read and properly applied.

Was it helpful?

Solution

There was a typo in the directory path, it should have read:

jms_serializer:
  metadata:
    debug: true
    file_cache:
      dir: "%kernel.cache_dir%/serializer"
    auto_detection: true
    directories:
      FOSUserBundle:
        namespace_prefix: FOS\UserBundle
        path: "@MyBundle/Resources/config/serializer/fosuser"

Took me digging into the FileLocator to understand why the file was not being loaded.

OTHER TIPS

Modifying files in the vendors directory is a very bad practice. What you are looking for is answered here:

JMSSerializerBundle. no control over third party meta data

Kinda late though :p

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top