-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Allow default group in serialization context #44035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue 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
[Serializer] Allow default group in serialization context #44035
Conversation
Hey! I think @BafS has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
Anyone available to help me with this PR? |
src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php
Show resolved
Hide resolved
cfdef73
to
4efbc42
Compare
I am waiting on this. What is holding this up? |
Hello, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @dunglas
|
||
private function getAttributeGroups(AttributeMetadataInterface $serializerAttributeMetadata): array | ||
{ | ||
$groups = empty($serializerAttributeMetadata->getGroups()) ? [AbstractNormalizer::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $serializerAttributeMetadata->getGroups(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$groups = empty($serializerAttributeMetadata->getGroups()) ? [AbstractNormalizer::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $serializerAttributeMetadata->getGroups(); | |
$groups = !$serializerAttributeMetadata->getGroups() ? [AbstractNormalizer::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $serializerAttributeMetadata->getGroups(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling getGroups()
two times isn't necessary. Storing the result of this function in a variable will improve the performance.
@@ -257,6 +263,13 @@ protected function getGroups(array $context): array | |||
return is_scalar($groups) ? (array) $groups : $groups; | |||
} | |||
|
|||
protected function getAttributeGroups(AttributeMetadataInterface $attributeMetadata): array | |||
{ | |||
$groups = empty($attributeMetadata->getGroups()) ? [self::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $attributeMetadata->getGroups(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$groups = empty($attributeMetadata->getGroups()) ? [self::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $attributeMetadata->getGroups(); | |
$groups = !$attributeMetadata->getGroups() ? [self::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $attributeMetadata->getGroups(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even
$groups = $attributeMetadata->getGroups() ?: [self::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS];
@@ -112,6 +112,12 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn | |||
*/ | |||
public const IGNORED_ATTRIBUTES = 'ignored_attributes'; | |||
|
|||
/** | |||
* The default group can be use on the serialization context to explicitly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* The default group can be use on the serialization context to explicitly | |
* The default group can be used on the serialization context to explicitly |
Couldn't we use the same conventions as for the Validator component for consistency? |
By that you mean use the group name "Default" instead of "_default"? Yes, I am in favour of that. (The only problem is that it is a BC break for code that already uses the group name "Default") |
Yes, I mean that, and also support using the name of the class as a group name ( Regarding the BC break, isn't using |
|
||
private function getAttributeGroups(AttributeMetadataInterface $serializerAttributeMetadata): array | ||
{ | ||
$groups = empty($serializerAttributeMetadata->getGroups()) ? [AbstractNormalizer::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $serializerAttributeMetadata->getGroups(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling getGroups()
two times isn't necessary. Storing the result of this function in a variable will improve the performance.
{ | ||
$groups = empty($serializerAttributeMetadata->getGroups()) ? [AbstractNormalizer::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $serializerAttributeMetadata->getGroups(); | ||
|
||
return array_merge($groups, ['*']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return array_merge($groups, ['*']); | |
$groups[] = '*'; | |
return $groups; |
This will also probably be a bit faster.
@@ -257,6 +263,13 @@ protected function getGroups(array $context): array | |||
return is_scalar($groups) ? (array) $groups : $groups; | |||
} | |||
|
|||
protected function getAttributeGroups(AttributeMetadataInterface $attributeMetadata): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is on the hot patch. Introducing a new function will have a significant performance impact when serializing a large set of data. Also, we try to not add new protected methods in this class (we're in the process of splitting it and using composition instead of inheritance).
Could you inline it? By the way, you should also inline the one in the extractor.
@@ -257,6 +263,13 @@ protected function getGroups(array $context): array | |||
return is_scalar($groups) ? (array) $groups : $groups; | |||
} | |||
|
|||
protected function getAttributeGroups(AttributeMetadataInterface $attributeMetadata): array | |||
{ | |||
$groups = empty($attributeMetadata->getGroups()) ? [self::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $attributeMetadata->getGroups(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even
$groups = $attributeMetadata->getGroups() ?: [self::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS];
* The default group can be use on the serialization context to explicitly | ||
* tell the serializer to allow properties without defined groups. | ||
*/ | ||
public const DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS = '_default'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to be overridden in the userland?
If not, we could mark it as final I guess, and maybe move it on top of L36 so that it's not considered as a context constant.
If yes, then why not store it in the context (and update the AbstractNormalizerContextBuilder
)?
Closing in favor of #51514, thanks for pushing this forward! |
The
_default
group can be use on the serialization context to explicitly tell the serializer to allow properties without defined@Groups
Exemple:
name
,age
,isBan
moderator
context group:isBan
*
context group:name
,age
,isBan
_default
context group:name
_default
andmoderator
context groups:name
,isBan
I use the
_default
keyword here but it can be easily changed for something else like I said in this issue #32622 (comment)