-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Serializer default groups #32622
New issue
Have a question about this project? Sign up for a free GitHub account to open an is 8000 sue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
…allows any group (nrobinaubertin) This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- [Serializer] Add special '*' serialization group that allows any group | Q | A | ------------- | --- | Branch? | master for features | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32622 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Hi, I added support for a special serialization group: '*'. This group lets any group serialize the attribute marked with it. The BC break comes from the fact that someone could have used the '*' group in their code. Commits ------- 54e24a8 [Serializer] Add special '*' serialization group that allows any group
I think this issue could be reopened. I don't think @kingschnulli was talking about a context group that serialize everything, like the
@fabpot should I create a new issue or can this one be reopened ? |
Thanks. I already have a working POC that use a I don't know what could be the best option to reduce the BC break.
|
This is exactly what I was talking about, makes it way easier to integrate into existing code bases. |
Another example if it can help people understand the purpose of this feature: Currently, I am using 3 classes to handle common and r/w operations:
With a default group, everything could be simplified to a single class: <?php
namespace App\DTO\Account;
use App\API\Model\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Valid;
class Profile
{
/** User's nickname */
#[Length(max: 64)]
public ?string $nickname = null;
/** Profile picture's URL */
#[Groups("read")]
public ?string $pictureUrl = null;
/** Profile picture update */
#[Image(maxSize: '1M', detectCorrupted: true)]
#[Valid]
#[Groups("write")]
public ?File $picture = null;
} |
Any progress on this? @noemi-salaun seems to have provided a PR, but that seems to have stopped? |
@TerjeBr , as I can see, you can use the special char |
@sergey-kravchenk0 I do not think Let's say you have 7 properties on a class, and 2 of them has group annotation "cat" and 1 has group annotation "dog" and 4 has no group annotation. Then I want to serialize an object of that class, and I want what is in group "cat" and also what has no group annotation, but not what is in group "dog". As I have understood it if I specify |
@TerjeBr I see, you are right, now there is no way to define the "Default" group. The only solution is to explicitly define the "Default" group for properties. |
Thank you for this suggestion. |
Ohh yes! |
I've created workaround for this issue by decorating the <?php
namespace App\Serialization;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
class MetadataLoader implements LoaderInterface
{
public function __construct(
private readonly LoaderInterface $decorated
) {
}
public function loadClassMetadata(ClassMetadataInterface $classMetadata)
{
$loaded = $this->decorated->loadClassMetadata($classMetadata);
foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
if (empty($attributeMetadata->getGroups())) {
$attributeMetadata->addGroup('default');
}
}
return $loaded;
}
}
App\Serialization\MetadataLoader:
decorates: 'serializer.mapping.chain_loader' Now if you apply the |
Thank you for this suggestion. |
Keep it open please. There is also a PR for it already |
Hey! I tried a different approach to solve the issue in this PR: #51514 |
Should this be closed, since #51514 has been merged? |
Yes, I think it can be 🙂 (when it'll be merged) |
#51514 has not been merged yet |
…ps (mtarld) This PR was merged into the 7.1 branch. Discussion ---------- [Serializer] Add Default and "class name" default groups | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #32622 | License | MIT | Doc PR | TODO Add `Default` and "class name" groups to the (de)normalization context, following Validator's component naming convention. Commits ------- de58759 [Serializer] Add default groups
Let's say I have an entity Book:
What I'm trying to achieve now is to serialize the $foo property, regardless of the current context groups. Is there some way to configure some kind of "default group" for properties where no group was given?
I know I could just add the group to my property, but I was hoping to configure something (or decorate something) so the serializer will return the property $foo.
TLDR: How can I define a default group for all properties? Is this already possible or might be a new feature?
The text was updated successfully, but these errors were encountered: