diff --git a/src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php b/src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php index 08ff10d04ac8f..672a73df887c7 100644 --- a/src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php +++ b/src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php @@ -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, ['*']); + } } diff --git a/src/Symfony/Component/PropertyInfo/Tests/Extractor/SerializerExtractorTest.php b/src/Symfony/Component/PropertyInfo/Tests/Extractor/SerializerExtractorTest.php index 66d8c06b3e6ff..37c48a807b606 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Extractor/SerializerExtractorTest.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Extractor/SerializerExtractorTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor; use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy; +use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultGroupDummy; use Symfony\Component\PropertyInfo\Tests\Fixtures\IgnorePropertyDummy; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; @@ -52,4 +53,14 @@ public function testGetPropertiesWithAnyGroup() { $this->assertSame(['analyses', 'feet'], $this->extractor->getProperties(AdderRemoverDummy::class, ['serializer_groups' => null])); } + + public function testGetPropertiesWithAllGroup() + { + $this->assertSame(['somethingWithoutGroup', 'somethingWithGroup'], $this->extractor->getProperties(DefaultGroupDummy::class, ['serializer_groups' => ['*']])); + } + + public function testGetPropertiesWithDefaultGroup() + { + $this->assertSame(['somethingWithoutGroup'], $this->extractor->getProperties(DefaultGroupDummy::class, ['serializer_groups' => ['_default']])); + } } diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/DefaultGroupDummy.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/DefaultGroupDummy.php new file mode 100644 index 0000000000000..f1168672fd698 --- /dev/null +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/DefaultGroupDummy.php @@ -0,0 +1,17 @@ +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 + { + $groups = empty($attributeMetadata->getGroups()) ? [self::DEFAULT_GROUP_FOR_ATTRIBUTE_WITHOUT_GROUPS] : $attributeMetadata->getGroups(); + + return array_merge($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 ee305403837c2..8a28d0a2f80c8 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -87,6 +87,9 @@ 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 => ['_default']], true); + $this->assertEquals(['a1'], $result); } public function testGetAllowedAttributesAsObjects() @@ -122,6 +125,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::GROUPS => ['_default']], false); + $this->assertEquals([$a1], $result); } public function testObjectWithStaticConstructor()