Skip to content

Commit

Permalink
bug #58705 [Serializer] Revert Default groups (mtarld)
Browse files Browse the repository at this point in the history
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 #58576, Fix #57350
| License       | MIT

When introduced in #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 #58656).

Commits
-------

ab3220f6cd5 [Serializer] Revert default groups
  • Loading branch information
fabpot committed Nov 9, 2024
2 parents eefcb59 + 1a71858 commit 3748f85
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 8 deletions.
6 changes: 1 addition & 5 deletions Extractor/SerializerExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ public function getProperties(string $class, array $context = []): ?array
return null;
}

$groups = $context['serializer_groups'] ?? [];
$groupsHasBeenDefined = [] !== $groups;
$groups = array_merge($groups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]);

$properties = [];
$serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class);

foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
if (!$serializerAttributeMetadata->isIgnored() && (!$groupsHasBeenDefined || array_intersect(array_merge($serializerAttributeMetadata->getGroups(), ['*']), $groups))) {
if (!$serializerAttributeMetadata->isIgnored() && (null === $context['serializer_groups'] || \in_array('*', $context['serializer_groups'], true) || array_intersect($serializerAttributeMetadata->getGroups(), $context['serializer_groups']))) {
$properties[] = $serializerAttributeMetadata->getName();
}
}
Expand Down
2 changes: 0 additions & 2 deletions Tests/Extractor/SerializerExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public function testGetProperties()
public function testGetPropertiesWithIgnoredProperties()
{
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['a']]));
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['Default']]));
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['IgnorePropertyDummy']]));
}

public function testGetPropertiesWithAnyGroup()
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/IgnorePropertyDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class IgnorePropertyDummy
{
#[Groups(['a', 'Default', 'IgnorePropertyDummy'])]
#[Groups(['a'])]
public $visibleProperty;

#[Groups(['a']), Ignore]
Expand Down

0 comments on commit 3748f85

Please sign in to comment.