Skip to content

Commit e713ac2

Browse files
committed
bug symfony#58705 [Serializer] Revert Default groups (mtarld)
This PR was merged into the 7.1 branch. Discussion ---------- [Serializer] Revert Default groups | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#58576, Fix symfony#57350 | License | MIT When introduced in symfony#51514, the behavior of group selection was wrong and introduced BC breaks (see the two related issues). This PR reverts this introduction so that they can be added properly in 7.3 (see symfony#58656). Commits ------- ab3220f [Serializer] Revert default groups
2 parents 00320cf + ab3220f commit e713ac2

File tree

12 files changed

+10
-115
lines changed

12 files changed

+10
-115
lines changed

src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ public function getProperties(string $class, array $context = []): ?array
3838
return null;
3939
}
4040

41-
$groups = $context['serializer_groups'] ?? [];
42-
$groupsHasBeenDefined = [] !== $groups;
43-
$groups = array_merge($groups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]);
44-
4541
$properties = [];
4642
$serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class);
4743

4844
foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
49-
if (!$serializerAttributeMetadata->isIgnored() && (!$groupsHasBeenDefined || array_intersect(array_merge($serializerAttributeMetadata->getGroups(), ['*']), $groups))) {
45+
if (!$serializerAttributeMetadata->isIgnored() && (null === $context['serializer_groups'] || \in_array('*', $context['serializer_groups'], true) || array_intersect($serializerAttributeMetadata->getGroups(), $context['serializer_groups']))) {
5046
$properties[] = $serializerAttributeMetadata->getName();
5147
}
5248
}

src/Symfony/Component/PropertyInfo/Tests/Extractor/SerializerExtractorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public function testGetProperties()
4343
public function testGetPropertiesWithIgnoredProperties()
4444
{
4545
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['a']]));
46-
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['Default']]));
47-
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['IgnorePropertyDummy']]));
4846
}
4947

5048
public function testGetPropertiesWithAnyGroup()

src/Symfony/Component/PropertyInfo/Tests/Fixtures/IgnorePropertyDummy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class IgnorePropertyDummy
2121
{
22-
#[Groups(['a', 'Default', 'IgnorePropertyDummy'])]
22+
#[Groups(['a'])]
2323
public $visibleProperty;
2424

2525
#[Groups(['a']), Ignore]

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ CHANGELOG
66

77
* Add arguments `$class`, `$format` and `$context` to `NameConverterInterface::normalize()` and `NameConverterInterface::denormalize()`
88
* Add `DateTimeNormalizer::CAST_KEY` context option
9-
* Add `Default` and "class name" default groups
109
* Add `AbstractNormalizer::FILTER_BOOL` context option
1110
* Add `CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES` context option
1211
* Deprecate `AbstractNormalizerContextBuilder::withDefaultContructorArguments(?array $defaultContructorArguments)`, use `withDefaultConstructorArguments(?array $defaultConstructorArguments)` instead (note the missing `s` character in Contructor word in deprecated method)

src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,13 @@ private function getCacheValueForAttributesMetadata(string $class, array $contex
128128
}
129129

130130
$metadataGroups = $metadata->getGroups();
131-
132131
$contextGroups = (array) ($context[AbstractNormalizer::GROUPS] ?? []);
133-
$contextGroupsHasBeenDefined = [] !== $contextGroups;
134-
$contextGroups = array_merge($contextGroups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]);
135132

136-
if ($contextGroupsHasBeenDefined && !$metadataGroups) {
133+
if ($contextGroups && !$metadataGroups) {
137134
continue;
138135
}
139136

140-
if ($metadataGroups && !array_intersect(array_merge($metadataGroups, ['*']), $contextGroups)) {
137+
if ($metadataGroups && !array_intersect($metadataGroups, $contextGroups) && !\in_array('*', $contextGroups, true)) {
141138
continue;
142139
}
143140

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,32 +223,27 @@ protected function getAllowedAttributes(string|object $classOrObject, array $con
223223
return false;
224224
}
225225

226-
$classMetadata = $this->classMetadataFactory->getMetadataFor($classOrObject);
227-
$class = $classMetadata->getName();
228-
229226
$groups = $this->getGroups($context);
230-
$groupsHasBeenDefined = [] !== $groups;
231-
$groups = array_merge($groups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]);
232227

233228
$allowedAttributes = [];
234229
$ignoreUsed = false;
235230

236-
foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
231+
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
237232
if ($ignore = $attributeMetadata->isIgnored()) {
238233
$ignoreUsed = true;
239234
}
240235

241236
// If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties()
242237
if (
243238
!$ignore
244-
&& (!$groupsHasBeenDefined || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups))
239+
&& ([] === $groups || \in_array('*', $groups, true) || array_intersect($attributeMetadata->getGroups(), $groups))
245240
&& $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context)
246241
) {
247242
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
248243
}
249244
}
250245

251-
if (!$ignoreUsed && !$groupsHasBeenDefined && $allowExtraAttributes) {
246+
if (!$ignoreUsed && [] === $groups && $allowExtraAttributes) {
252247
// Backward Compatibility with the code using this method written before the introduction of @Ignore
253248
return false;
254249
}

src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/GroupDummy.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
2727
protected $quux;
2828
private $fooBar;
2929
private $symfony;
30-
#[Groups(['Default'])]
31-
private $default;
32-
#[Groups(['GroupDummy'])]
33-
private $className;
3430

3531
#[Groups(['b'])]
3632
public function setBar($bar)
@@ -84,24 +80,4 @@ public function setQuux($quux): void
8480
{
8581
$this->quux = $quux;
8682
}
87-
88-
public function setDefault($default)
89-
{
90-
$this->default = $default;
91-
}
92-
93-
public function getDefault()
94-
{
95-
return $this->default;
96-
}
97-
98-
public function setClassName($className)
99-
{
100-
$this->className = $className;
101-
}
102-
103-
public function getClassName()
104-
{
105-
return $this->className;
106-
}
10783
}

src/Symfony/Component/Serializer/Tests/Mapping/TestClassMetadataFactory.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,6 @@ public static function createClassMetadata(string $namespace, bool $withParent =
6363
$symfony->addGroup('name_converter');
6464
}
6565

66-
$default = new AttributeMetadata('default');
67-
$default->addGroup('Default');
68-
$expected->addAttributeMetadata($default);
69-
70-
$className = new AttributeMetadata('className');
71-
$className->addGroup('GroupDummy');
72-
$expected->addAttributeMetadata($className);
73-
7466
// load reflection class so that the comparison passes
7567
$expected->getReflectionClass();
7668

src/Symfony/Component/Serializer/Tests/Normalizer/Features/GroupsTestTrait.php

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ public function testGroupsNormalize()
3636
$obj->setSymfony('symfony');
3737
$obj->setKevin('kevin');
3838
$obj->setCoopTilleuls('coopTilleuls');
39-
$obj->setDefault('default');
40-
$obj->setClassName('className');
4139

4240
$this->assertEquals([
4341
'bar' => 'bar',
44-
'default' => 'default',
45-
'className' => 'className',
4642
], $normalizer->normalize($obj, null, ['groups' => ['c']]));
4743

4844
$this->assertEquals([
@@ -52,54 +48,18 @@ public function testGroupsNormalize()
5248
'bar' => 'bar',
5349
'kevin' => 'kevin',
5450
'coopTilleuls' => 'coopTilleuls',
55-
'default' => 'default',
56-
'className' => 'className',
5751
], $normalizer->normalize($obj, null, ['groups' => ['a', 'c']]));
58-
59-
$this->assertEquals([
60-
'default' => 'default',
61-
'className' => 'className',
62-
], $normalizer->normalize($obj, null, ['groups' => ['unknown']]));
63-
64-
$this->assertEquals([
65-
'quux' => 'quux',
66-
'symfony' => 'symfony',
67-
'foo' => 'foo',
68-
'fooBar' => 'fooBar',
69-
'bar' => 'bar',
70-
'kevin' => 'kevin',
71-
'coopTilleuls' => 'coopTilleuls',
72-
'default' => 'default',
73-
'className' => 'className',
74-
], $normalizer->normalize($obj));
7552
}
7653

7754
public function testGroupsDenormalize()
7855
{
7956
$normalizer = $this->getDenormalizerForGroups();
8057

8158
$obj = new GroupDummy();
82-
$obj->setDefault('default');
83-
$obj->setClassName('className');
84-
85-
$data = [
86-
'foo' => 'foo',
87-
'bar' => 'bar',
88-
'quux' => 'quux',
89-
'default' => 'default',
90-
'className' => 'className',
91-
];
92-
93-
$denormalized = $normalizer->denormalize(
94-
$data,
95-
GroupDummy::class,
96-
null,
97-
['groups' => ['unknown']]
98-
);
99-
$this->assertEquals($obj, $denormalized);
100-
10159
$obj->setFoo('foo');
10260

61+
$data = ['foo' => 'foo', 'bar' => 'bar'];
62+
10363
$denormalized = $normalizer->denormalize(
10464
$data,
10565
GroupDummy::class,
@@ -117,11 +77,6 @@ public function testGroupsDenormalize()
11777
['groups' => ['a', 'b']]
11878
);
11979
$this->assertEquals($obj, $denormalized);
120-
121-
$obj->setQuux('quux');
122-
123-
$denormalized = $normalizer->denormalize($data, GroupDummy::class);
124-
$this->assertEquals($obj, $denormalized);
12580
}
12681

12782
public function testNormalizeNoPropertyInGroup()
@@ -130,12 +85,7 @@ public function testNormalizeNoPropertyInGroup()
13085

13186
$obj = new GroupDummy();
13287
$obj->setFoo('foo');
133-
$obj->setDefault('default');
134-
$obj->setClassName('className');
13588

136-
$this->assertEquals([
137-
'default' => 'default',
138-
'className' => 'className',
139-
], $normalizer->normalize($obj, null, ['groups' => ['notExist']]));
89+
$this->assertEquals([], $normalizer->normalize($obj, null, ['groups' => ['notExist']]));
14090
}
14191
}

src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,6 @@ public function testGroupsNormalizeWithNameConverter()
302302
'bar' => null,
303303
'foo_bar' => '@dunglas',
304304
'symfony' => '@coopTilleuls',
305-
'default' => null,
306-
'class_name' => null,
307305
],
308306
$this->normalizer->normalize($obj, null, [GetSetMethodNormalizer::GROUPS => ['name_converter']])
309307
);

0 commit comments

Comments
 (0)