Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
carsonbot commentedNov 14, 2021
Hey! I think@BafS has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
noemi-salaun commentedJan 21, 2022
Anyone available to help me with this PR? |
src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
cfdef73 to4efbc42CompareTerjeBr commentedJul 7, 2022
I am waiting on this. What is holding this up? |
ninsuo commentedSep 18, 2022
Hello, |
fabpot left a comment
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
| privatefunctiongetAttributeGroups(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.
CallinggetGroups() two times isn't necessary. Storing the result of this function in a variable will improve the performance.
| protectedfunctiongetAttributeGroups(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];
| publicconstIGNORED_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 beuse on the serialization context to explicitly | |
| * The default group can beused on the serialization context to explicitly |
dunglas commentedSep 20, 2022
Couldn't we usethe same conventions as for the Validator component for consistency? |
TerjeBr commentedSep 20, 2022
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") |
dunglas commentedSep 20, 2022
Yes, I mean that, and also support using the name of the class as a group name ( Regarding the BC break, isn't using |
| privatefunctiongetAttributeGroups(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.
CallinggetGroups() 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(); | ||
| returnarray_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.
| returnarray_merge($groups, ['*']); | |
| $groups[] ='*'; | |
| return$groups; |
This will also probably be a bit faster.
| returnis_scalar($groups) ? (array)$groups :$groups; | ||
| } | ||
| protectedfunctiongetAttributeGroups(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.
| protectedfunctiongetAttributeGroups(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. | ||
| */ | ||
| publicconstDEFAULT_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 theAbstractNormalizerContextBuilder)?
nicolas-grekas commentedNov 20, 2023
Closing in favor of#51514, thanks for pushing this forward! |
Uh oh!
There was an error while loading.Please reload this page.
The
_defaultgroup can be use on the serialization context to explicitly tell the serializer to allow properties without defined@GroupsExemple:
name,age,isBanmoderatorcontext group:isBan*context group:name,age,isBan_defaultcontext group:name_defaultandmoderatorcontext groups:name,isBanI use the
_defaultkeyword here but it can be easily changed for something else like I said in this issue#32622 (comment)