[Serializer] Add special '*' serialization group that allows any group

This commit is contained in:
Niels Robin-Aubertin 2020-08-24 12:24:24 +02:00 committed by Fabien Potencier
parent 704c648ba5
commit 54e24a8999
2 changed files with 7 additions and 1 deletions

View File

@ -250,7 +250,7 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
// If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties()
if (
!$ignore &&
(false === $groups || array_intersect($attributeMetadata->getGroups(), $groups)) &&
(false === $groups || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups)) &&
$this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context)
) {
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;

View File

@ -72,6 +72,9 @@ class AbstractNormalizerTest extends TestCase
$result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['other']], true);
$this->assertEquals(['a3', 'a4'], $result);
$result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['*']], true);
$this->assertEquals(['a1', 'a2', 'a3', 'a4'], $result);
}
public function testGetAllowedAttributesAsObjects()
@ -104,6 +107,9 @@ class AbstractNormalizerTest extends TestCase
$result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['other']], false);
$this->assertEquals([$a3, $a4], $result);
$result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['*']], false);
$this->assertEquals([$a1, $a2, $a3, $a4], $result);
}
public function testObjectWithStaticConstructor()