schmittjoh / JMSSerializerBundle

Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
http://jmsyst.com/bundles/JMSSerializerBundle
MIT License
1.8k stars 311 forks source link

Add whole tree to Groups or set group to whole class #949

Open realjjaveweb opened 3 months ago

realjjaveweb commented 3 months ago
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no

I am looking for a way how to mark the whole DTO tree as a part of a serializer group, because #[Serializer\Groups([... works only on the property levels, then if you request the group in the serialization context, any property - whatsoever nested - has to have the group defined.

<?php
// ...
class ParentDTO
{
    // ... 
    #[Serializer\Groups([...
    private KidDTO $kidDTO;
    // ...
}

Now when the groups are used during serialization to JSON through SerializationContext, the $kidDTO will result in an empty object kidDTO: {} unless, the groups are added to all its properties.

This is very unhandy, since some DTOs are so granular and shared among other DTOs that adding all groups of all DTOs from the parent tree to all the subtree properties is simply unrealistic and would create unnecessary code mess.

Is there a way how to do one of the following:

  1. Set on the property that for a certain group, the whole subtree should be included? (preferred)
  2. Define the Groups on the class level? (compromise)

(1.) would especially be appreciated in case I want to nest some external DTO where I can't really add my custom serialization groups.

scyzoryck commented 3 months ago

Hi!

I think at the moment serialiser do not support such options. Feel free to propose some Pull Request with the changes for it - I think it should be kinda easy to implement in GroupsExclusionStrategy :)