Why does JMS Serializer ignore mappings for nested collections?
Asked Answered
A

2

12

I have entities in a OneToMany relation:

Forecast -> has many -> Brick(s)

I've created serialization mappings for each in Resources/config/serializer/Entity.xxx.yml where xxx is entity name.

Each entity has exclusion policy set to ALL and some of it's properties are exposed, eg:

Acme\ForecastBundle\Entity\Forecast:   
  exclusion_policy: ALL   
  xml_root_name: forecast   
  properties:
    id:
      expose: true
      type: integer
    regionUid:
      expose: true
      type: string
    description:
      expose: true
      type: string
    bricks:
      expose: true
      type: array<Acme\ForecastBundle\Entity\Brick>
      xml_list:
        inline: true
        entry_name: brick

When in my template I do {{ forecast|serialize|raw }} I am getting:

Forecast -> as I expected -> only exposed fields are serialized

Bricks collection -> all properties are serialized -> it seems that my Entity.Brick.yml is ignored... why?

In brick I have only ID and name exposed.. but in serialized output I have all properties (created_at, updated_at.. and more).. why? They should be excluded by "exclusion_policy: ALL". It seems that config for nested collection is not used.

Acme\ForecastBundle\Entity\Brick:   
  exclusion_policy: ALL   
  xml_root_name: brick
  properties:
    id:
      expose: true
      type: integer
    name:
      expose: true
      type: string

EDIT:

Yes, I did clear cache after each change in .yml config

After some suggestions I added @ExclusionPolicy("ALL") annotation to the Brick class and @Expose on ID, just to see what happens.. and suddenly it works! Not only ID is exposed, but everything is like in my YML configuration.

So I removed the annotations.. and it still works!

So it seems that adding Annotations somehow forced serializer to recognize my YML config. I don't know why though. That worries me.

Is it possible APC cache is guilty? I did clear Symfony2 cache numerous of times, but APC only a few.

Antipodal answered 11/1, 2014 at 21:46 Comment(4)
What happens if you serialize just one brick standalone? Also try to clear cache with rm -r app/cachePhyllode
Brick standalone was serialized correctly. And I did clear Symfony2 cache. Now I'm thinking it might have been APC cache. But is that possible?Antipodal
I'm always got the problem with jms-bundles cache... Anyway problem in cachePhyllode
app/console cache:clear is not enough, it needs to be removed via rm -rf var/cache/ (for SF4.x)Fotheringhay
A
9

Since there were no anwsers, I'll post what I eventually did/found out (for future reference and anyone who encounters the same problem). I'll quote my edit:

After some suggestions I added @ExclusionPolicy("ALL") annotation to the Brick class and @Expose on ID, just to see what happens.. and suddenly it works! Not only ID is exposed, but everything is like in my YML configuration.

So I removed the annotations.. and it still works!

So it seems that adding Annotations somehow forced serializer to recognize my YML config. I don't know why though. That worries me.

Antipodal answered 22/1, 2014 at 10:35 Comment(2)
This is exactly what happened to me as well... I was trying to get it to work for days. Can anyone find if this feature is documented?Cadastre
Update: Have you tried clearing the cache? I have tried to reproduce the phenomenon and for a few times, I have realized that app/console cache:clear did not clear the jms_serializer cache, resulting in this behavior. Adding annotations to the entity forces the cache to be recreated.Cadastre
T
0

Apparently adding use JMS\Serializer\Annotation\ExclusionPolicy; to the entity is enough.

I m guessing it was not working because we did not implement the class to do so.

Territorialize answered 4/1, 2017 at 16:40 Comment(1)
This is more a comment than an answer.Stratus

© 2022 - 2024 — McMap. All rights reserved.