JMSSerializerBundle. no control over third party meta data

I bet xxx\xxx\Entity\User: refers to your own namespace and class.

If it is, it is the wrong way to do.

The rules must be applied to the class where the properties live.

Given the property you exposed in your configuration, I guess you’re using FOSUserBundle.

Therefore, you must apply your rules on FOS\UserBundle\Model\User.

Then you need to add a JMSSerializer config to indicate where the serializer metadata live for the given namespace.

It should look like:

jms_serializer:
  metadata:
    auto_detection: true
    directories:
      FOSUserBundle:
        namespace_prefix: "FOS\\UserBundle"
        path: "@YourUserBundle/Resources/config/serializer/fos"

In fos/ directory you should have Model.User.yml

With something like:

FOS\UserBundle\Model\User:
  exclusion_policy: ALL
  properties:
    id:
      expose: true
      groups: [list, details]
    username:
      expose: true
      groups: [details]
    email:
      expose: true
      groups: [me]
    roles:
      expose: true
      groups: [details]

Details:

When applying rules to the Serializer through metadata, the Serializer looks for the property which are declared inside the class which is defined in the Metadata.

Example:

class Foo {
     protected $foo;
}

class Bar extends Foo {
     protected $bar;
}

Your metadata will look like this:

Foo:
  exclusion_policy: ALL
  properties:
      foo: 
          expose: true

Bar:
  exclusion_policy: ALL
  properties:
      bar: 
          expose: true

THE EXAMPLE BELOW IS NOT THE CORRECT WAY TO DO

Bar:
  exclusion_policy: ALL
  properties:
      foo: 
          expose: true
      bar: 
          expose: true

if you do this, only the rules on the property bar will be applied (and exposed).

Leave a Comment