-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,7 +12,9 @@ | |||||||||
namespace Symfony\Component\PropertyInfo\Extractor; | ||||||||||
|
||||||||||
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; | ||||||||||
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; | ||||||||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; | ||||||||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; | ||||||||||
|
||||||||||
/** | ||||||||||
* Lists available properties using Symfony Serializer Component metadata. | ||||||||||
|
@@ -48,11 +50,18 @@ public function getProperties(string $class, array $context = []): ?array | |||||||||
|
||||||||||
foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) { | ||||||||||
$ignored = method_exists($serializerAttributeMetadata, 'isIgnored') && $serializerAttributeMetadata->isIgnored(); | ||||||||||
if (!$ignored && (null === $context['serializer_groups'] || array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups()))) { | ||||||||||
if (!$ignored && (null === $context['serializer_groups'] || array_intersect($context['serializer_groups'], $this->getAttributeGroups($serializerAttributeMetadata)))) { | ||||||||||
$properties[] = $serializerAttributeMetadata->getName(); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
return $properties; | ||||||||||
} | ||||||||||
|
||||||||||
private function getAttributeGroups(AttributeMetadataInterface $serializerAttributeMetadata): array | ||||||||||
{ | ||||||||||
$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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This will also probably be a bit faster. |
||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\PropertyInfo\Tests\Fixtures; | ||
|
||
use Symfony\Component\Serializer\Annotation\Groups; | ||
|
||
final class DefaultGroupDummy | ||
{ | ||
|
||
public $somethingWithoutGroup; | ||
|
||
/** | ||
* @Groups({"a"}) | ||
*/ | ||
public $somethingWithGroup; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* 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 commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be overridden in the userland? |
||||||
|
||||||
/** | ||||||
* @internal | ||||||
*/ | ||||||
|
@@ -235,7 +241,7 @@ protected function getAllowedAttributes(string|object $classOrObject, array $con | |||||
// If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties() | ||||||
if ( | ||||||
!$ignore && | ||||||
([] === $groups || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups)) && | ||||||
([] === $groups || array_intersect($this->getAttributeGroups($attributeMetadata), $groups)) && | ||||||
$this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context) | ||||||
) { | ||||||
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata; | ||||||
|
@@ -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 commentThe 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. |
||||||
{ | ||||||
$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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or even $groups = $attributeMetadata->getGroups() ?: [self::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS]; |
||||||
|
||||||
return array_merge($groups, ['*']); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Is this attribute allowed? | ||||||
* | ||||||
|
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.
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.