From 6e7d9f9378bee4352de15bc7883a28294f4b82d6 Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Tue, 29 Oct 2024 10:06:08 +0100 Subject: [PATCH] [Serializer] Revert default groups --- CHANGELOG.md | 1 - NameConverter/MetadataAwareNameConverter.php | 7 +-- Normalizer/AbstractNormalizer.php | 11 +--- Tests/Fixtures/Attributes/GroupDummy.php | 24 -------- Tests/Mapping/TestClassMetadataFactory.php | 8 --- Tests/Normalizer/Features/GroupsTestTrait.php | 56 +------------------ .../Normalizer/GetSetMethodNormalizerTest.php | 2 - Tests/Normalizer/ObjectNormalizerTest.php | 2 - Tests/Normalizer/PropertyNormalizerTest.php | 4 -- 9 files changed, 8 insertions(+), 107 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3118834d8..463572046 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ CHANGELOG * Add arguments `$class`, `$format` and `$context` to `NameConverterInterface::normalize()` and `NameConverterInterface::denormalize()` * Add `DateTimeNormalizer::CAST_KEY` context option - * Add `Default` and "class name" default groups * Add `AbstractNormalizer::FILTER_BOOL` context option * 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) diff --git a/NameConverter/MetadataAwareNameConverter.php b/NameConverter/MetadataAwareNameConverter.php index 327d92dc1..445ad7422 100644 --- a/NameConverter/MetadataAwareNameConverter.php +++ b/NameConverter/MetadataAwareNameConverter.php @@ -128,16 +128,13 @@ private function getCacheValueForAttributesMetadata(string $class, array $contex } $metadataGroups = $metadata->getGroups(); - $contextGroups = (array) ($context[AbstractNormalizer::GROUPS] ?? []); - $contextGroupsHasBeenDefined = [] !== $contextGroups; - $contextGroups = array_merge($contextGroups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]); - if ($contextGroupsHasBeenDefined && !$metadataGroups) { + if ($contextGroups && !$metadataGroups) { continue; } - if ($metadataGroups && !array_intersect(array_merge($metadataGroups, ['*']), $contextGroups)) { + if ($metadataGroups && !array_intersect($metadataGroups, $contextGroups) && !\in_array('*', $contextGroups, true)) { continue; } diff --git a/Normalizer/AbstractNormalizer.php b/Normalizer/AbstractNormalizer.php index 6f065984c..d8f796b4c 100644 --- a/Normalizer/AbstractNormalizer.php +++ b/Normalizer/AbstractNormalizer.php @@ -223,17 +223,12 @@ protected function getAllowedAttributes(string|object $classOrObject, array $con return false; } - $classMetadata = $this->classMetadataFactory->getMetadataFor($classOrObject); - $class = $classMetadata->getName(); - $groups = $this->getGroups($context); - $groupsHasBeenDefined = [] !== $groups; - $groups = array_merge($groups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]); $allowedAttributes = []; $ignoreUsed = false; - foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) { + foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) { if ($ignore = $attributeMetadata->isIgnored()) { $ignoreUsed = true; } @@ -241,14 +236,14 @@ 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 - && (!$groupsHasBeenDefined || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups)) + && ([] === $groups || \in_array('*', $groups, true) || array_intersect($attributeMetadata->getGroups(), $groups)) && $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context) ) { $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata; } } - if (!$ignoreUsed && !$groupsHasBeenDefined && $allowExtraAttributes) { + if (!$ignoreUsed && [] === $groups && $allowExtraAttributes) { // Backward Compatibility with the code using this method written before the introduction of @Ignore return false; } diff --git a/Tests/Fixtures/Attributes/GroupDummy.php b/Tests/Fixtures/Attributes/GroupDummy.php index 5c34c95a4..749e841a5 100644 --- a/Tests/Fixtures/Attributes/GroupDummy.php +++ b/Tests/Fixtures/Attributes/GroupDummy.php @@ -27,10 +27,6 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface protected $quux; private $fooBar; private $symfony; - #[Groups(['Default'])] - private $default; - #[Groups(['GroupDummy'])] - private $className; #[Groups(['b'])] public function setBar($bar) @@ -84,24 +80,4 @@ public function setQuux($quux): void { $this->quux = $quux; } - - public function setDefault($default) - { - $this->default = $default; - } - - public function getDefault() - { - return $this->default; - } - - public function setClassName($className) - { - $this->className = $className; - } - - public function getClassName() - { - return $this->className; - } } diff --git a/Tests/Mapping/TestClassMetadataFactory.php b/Tests/Mapping/TestClassMetadataFactory.php index d617ffaeb..61147316a 100644 --- a/Tests/Mapping/TestClassMetadataFactory.php +++ b/Tests/Mapping/TestClassMetadataFactory.php @@ -63,14 +63,6 @@ public static function createClassMetadata(string $namespace, bool $withParent = $symfony->addGroup('name_converter'); } - $default = new AttributeMetadata('default'); - $default->addGroup('Default'); - $expected->addAttributeMetadata($default); - - $className = new AttributeMetadata('className'); - $className->addGroup('GroupDummy'); - $expected->addAttributeMetadata($className); - // load reflection class so that the comparison passes $expected->getReflectionClass(); diff --git a/Tests/Normalizer/Features/GroupsTestTrait.php b/Tests/Normalizer/Features/GroupsTestTrait.php index ba4d76323..621ceec41 100644 --- a/Tests/Normalizer/Features/GroupsTestTrait.php +++ b/Tests/Normalizer/Features/GroupsTestTrait.php @@ -36,13 +36,9 @@ public function testGroupsNormalize() $obj->setSymfony('symfony'); $obj->setKevin('kevin'); $obj->setCoopTilleuls('coopTilleuls'); - $obj->setDefault('default'); - $obj->setClassName('className'); $this->assertEquals([ 'bar' => 'bar', - 'default' => 'default', - 'className' => 'className', ], $normalizer->normalize($obj, null, ['groups' => ['c']])); $this->assertEquals([ @@ -52,26 +48,7 @@ public function testGroupsNormalize() 'bar' => 'bar', 'kevin' => 'kevin', 'coopTilleuls' => 'coopTilleuls', - 'default' => 'default', - 'className' => 'className', ], $normalizer->normalize($obj, null, ['groups' => ['a', 'c']])); - - $this->assertEquals([ - 'default' => 'default', - 'className' => 'className', - ], $normalizer->normalize($obj, null, ['groups' => ['unknown']])); - - $this->assertEquals([ - 'quux' => 'quux', - 'symfony' => 'symfony', - 'foo' => 'foo', - 'fooBar' => 'fooBar', - 'bar' => 'bar', - 'kevin' => 'kevin', - 'coopTilleuls' => 'coopTilleuls', - 'default' => 'default', - 'className' => 'className', - ], $normalizer->normalize($obj)); } public function testGroupsDenormalize() @@ -79,27 +56,10 @@ public function testGroupsDenormalize() $normalizer = $this->getDenormalizerForGroups(); $obj = new GroupDummy(); - $obj->setDefault('default'); - $obj->setClassName('className'); - - $data = [ - 'foo' => 'foo', - 'bar' => 'bar', - 'quux' => 'quux', - 'default' => 'default', - 'className' => 'className', - ]; - - $denormalized = $normalizer->denormalize( - $data, - GroupDummy::class, - null, - ['groups' => ['unknown']] - ); - $this->assertEquals($obj, $denormalized); - $obj->setFoo('foo'); + $data = ['foo' => 'foo', 'bar' => 'bar']; + $denormalized = $normalizer->denormalize( $data, GroupDummy::class, @@ -117,11 +77,6 @@ public function testGroupsDenormalize() ['groups' => ['a', 'b']] ); $this->assertEquals($obj, $denormalized); - - $obj->setQuux('quux'); - - $denormalized = $normalizer->denormalize($data, GroupDummy::class); - $this->assertEquals($obj, $denormalized); } public function testNormalizeNoPropertyInGroup() @@ -130,12 +85,7 @@ public function testNormalizeNoPropertyInGroup() $obj = new GroupDummy(); $obj->setFoo('foo'); - $obj->setDefault('default'); - $obj->setClassName('className'); - $this->assertEquals([ - 'default' => 'default', - 'className' => 'className', - ], $normalizer->normalize($obj, null, ['groups' => ['notExist']])); + $this->assertEquals([], $normalizer->normalize($obj, null, ['groups' => ['notExist']])); } } diff --git a/Tests/Normalizer/GetSetMethodNormalizerTest.php b/Tests/Normalizer/GetSetMethodNormalizerTest.php index f846a0d97..2c30a57bc 100644 --- a/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -302,8 +302,6 @@ public function testGroupsNormalizeWithNameConverter() 'bar' => null, 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', - 'default' => null, - 'class_name' => null, ], $this->normalizer->normalize($obj, null, [GetSetMethodNormalizer::GROUPS => ['name_converter']]) ); diff --git a/Tests/Normalizer/ObjectNormalizerTest.php b/Tests/Normalizer/ObjectNormalizerTest.php index 50866a1b2..ed6a9d3be 100644 --- a/Tests/Normalizer/ObjectNormalizerTest.php +++ b/Tests/Normalizer/ObjectNormalizerTest.php @@ -507,8 +507,6 @@ public function testGroupsNormalizeWithNameConverter() 'bar' => null, 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', - 'default' => null, - 'class_name' => null, ], $this->normalizer->normalize($obj, null, [ObjectNormalizer::GROUPS => ['name_converter']]) ); diff --git a/Tests/Normalizer/PropertyNormalizerTest.php b/Tests/Normalizer/PropertyNormalizerTest.php index b93a7bb9f..9ac85920e 100644 --- a/Tests/Normalizer/PropertyNormalizerTest.php +++ b/Tests/Normalizer/PropertyNormalizerTest.php @@ -195,8 +195,6 @@ public function testNormalizeWithParentClass() 'fooBar' => null, 'symfony' => null, 'baz' => 'baz', - 'default' => null, - 'className' => null, ], $this->normalizer->normalize($group, 'any') ); @@ -321,8 +319,6 @@ public function testGroupsNormalizeWithNameConverter() 'bar' => null, 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', - 'default' => null, - 'class_name' => null, ], $this->normalizer->normalize($obj, null, [PropertyNormalizer::GROUPS => ['name_converter']]) );