From 2702d8e5ff48964076190d2f0ef56524227cd04a Mon Sep 17 00:00:00 2001 From: zim32 Date: Sun, 26 May 2024 14:57:40 +0200 Subject: [PATCH 1/3] Add ignored_groups to serializer --- .../Serializer/Normalizer/AbstractNormalizer.php | 15 ++++++++++++++- .../Tests/Normalizer/AbstractNormalizerTest.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 004581399bcae..c80169a526231 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -59,6 +59,8 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn */ public const GROUPS = 'groups'; + public const IGNORED_GROUPS = 'ignored_groups'; + /** * Limit (de)normalize to the specified names. * @@ -230,6 +232,9 @@ protected function getAllowedAttributes(string|object $classOrObject, array $con $groupsHasBeenDefined = [] !== $groups; $groups = array_merge($groups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]); + $ignoredGroups = $this->getIgnoredGroups($context); + $ignoreGroupsHasBeenDefined = [] !== $ignoredGroups; + $allowedAttributes = []; $ignoreUsed = false; @@ -243,12 +248,13 @@ protected function getAllowedAttributes(string|object $classOrObject, array $con !$ignore && (!$groupsHasBeenDefined || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups)) && $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context) + && count(array_intersect($attributeMetadata->getGroups(), $ignoredGroups)) === 0 ) { $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata; } } - if (!$ignoreUsed && !$groupsHasBeenDefined && $allowExtraAttributes) { + if (!$ignoreUsed && !$groupsHasBeenDefined &&!$ignoreGroupsHasBeenDefined && $allowExtraAttributes) { // Backward Compatibility with the code using this method written before the introduction of @Ignore return false; } @@ -263,6 +269,13 @@ protected function getGroups(array $context): array return \is_scalar($groups) ? (array) $groups : $groups; } + protected function getIgnoredGroups(array $context): array + { + $groups = $context[self::IGNORED_GROUPS] ?? $this->defaultContext[self::IGNORED_GROUPS] ?? []; + + return \is_scalar($groups) ? (array) $groups : $groups; + } + /** * Is this attribute allowed? */ diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php index 3108fe3c6ae3c..2559a02a8ca72 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -73,6 +73,7 @@ public function testGetAllowedAttributesAsString() $a4 = new AttributeMetadata('a4'); $a4->addGroup('test'); $a4->addGroup('other'); + $a4->addGroup('ignore'); $classMetadata->addAttributeMetadata($a4); $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata); @@ -85,6 +86,15 @@ public function testGetAllowedAttributesAsString() $result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['*']], true); $this->assertEquals(['a1', 'a2', 'a3', 'a4'], $result); + + $result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['test'], AbstractNormalizer::IGNORED_GROUPS => ['ignore']], true); + $this->assertEquals(['a2'], $result); + + $result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['*'], AbstractNormalizer::IGNORED_GROUPS => ['ignore']], true); + $this->assertEquals(['a1', 'a2', 'a3'], $result); + + $result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::IGNORED_GROUPS => ['ignore']], true); + $this->assertEquals(['a1', 'a2', 'a3'], $result); } public function testGetAllowedAttributesAsObjects() @@ -105,6 +115,7 @@ public function testGetAllowedAttributesAsObjects() $a4 = new AttributeMetadata('a4'); $a4->addGroup('test'); $a4->addGroup('other'); + $a4->addGroup('ignore'); $classMetadata->addAttributeMetadata($a4); $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata); @@ -120,6 +131,9 @@ public function testGetAllowedAttributesAsObjects() $result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['*']], false); $this->assertEquals([$a1, $a2, $a3, $a4], $result); + + $result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::IGNORED_GROUPS => ['ignore']], false); + $this->assertEquals([$a1, $a2, $a3], $result); } public function testObjectWithStaticConstructor() From eb0a33672ae61f1c07e56d4b1d95b840f243f8fa Mon Sep 17 00:00:00 2001 From: zim32 Date: Sun, 26 May 2024 15:01:59 +0200 Subject: [PATCH 2/3] Modify CHANGELOG.md --- src/Symfony/Component/Serializer/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 3118834d80175..4f66e83c6ee48 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -11,6 +11,7 @@ CHANGELOG * Add `CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES` context option * Deprecate `AbstractNormalizerContextBuilder::withDefaultContructorArguments(?array $defaultContructorArguments)`, use `withDefaultConstructorArguments(?array $defaultConstructorArguments)` instead (note the missing `s` character in Contructor word in deprecated method) * Add `XmlEncoder::CDATA_WRAPPING_PATTERN` context option + * Add `AbstractNormalizer::IGNORED_GROUPS` to make it possible to ignore groups during serialization 7.0 --- From e2394d94747160d64b4a106a7b102273d23a1c82 Mon Sep 17 00:00:00 2001 From: zim32 Date: Sun, 26 May 2024 15:29:14 +0200 Subject: [PATCH 3/3] Code style fixes --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index c80169a526231..d9a172293594a 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -248,13 +248,13 @@ protected function getAllowedAttributes(string|object $classOrObject, array $con !$ignore && (!$groupsHasBeenDefined || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups)) && $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context) - && count(array_intersect($attributeMetadata->getGroups(), $ignoredGroups)) === 0 + && 0 === \count(array_intersect($attributeMetadata->getGroups(), $ignoredGroups)) ) { $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata; } } - if (!$ignoreUsed && !$groupsHasBeenDefined &&!$ignoreGroupsHasBeenDefined && $allowExtraAttributes) { + if (!$ignoreUsed && !$groupsHasBeenDefined && !$ignoreGroupsHasBeenDefined && $allowExtraAttributes) { // Backward Compatibility with the code using this method written before the introduction of @Ignore return false; }