Skip to content

[Serializer] Add AbstractNormalizer::IGNORED_GROUPS to make it possible to ignore groups during serialization #57166

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;

Expand All @@ -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)
&& 0 === \count(array_intersect($attributeMetadata->getGroups(), $ignoredGroups))
) {
$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;
}
Expand All @@ -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?
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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);
Expand All @@ -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()
Expand Down
Loading