Skip to content

Commit cfdef73

Browse files
committed
[Serializer] Allow default group in serialization context
Fix #32622
1 parent 42b994c commit cfdef73

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\PropertyInfo\Extractor;
1313

1414
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
15+
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
1516
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
1617

1718
/**
@@ -48,11 +49,18 @@ public function getProperties(string $class, array $context = []): ?array
4849

4950
foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
5051
$ignored = method_exists($serializerAttributeMetadata, 'isIgnored') && $serializerAttributeMetadata->isIgnored();
51-
if (!$ignored && (null === $context['serializer_groups'] || array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups()))) {
52+
if (!$ignored && (null === $context['serializer_groups'] || array_intersect($context['serializer_groups'], $this->getAttributeGroups($serializerAttributeMetadata)))) {
5253
$properties[] = $serializerAttributeMetadata->getName();
5354
}
5455
}
5556

5657
return $properties;
5758
}
59+
60+
private function getAttributeGroups(AttributeMetadataInterface $serializerAttributeMetadata): array
61+
{
62+
$groups = empty($serializerAttributeMetadata->getGroups()) ? ['_default'] : $serializerAttributeMetadata->getGroups();
63+
64+
return array_merge($groups, ['*']);
65+
}
5866
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;
1717
use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
18+
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultGroupDummy;
1819
use Symfony\Component\PropertyInfo\Tests\Fixtures\IgnorePropertyDummy;
1920
use Symfony\Component\Serializer\Annotation\Ignore;
2021
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
@@ -57,4 +58,14 @@ public function testGetPropertiesWithAnyGroup()
5758
{
5859
$this->assertSame(['analyses', 'feet'], $this->extractor->getProperties(AdderRemoverDummy::class, ['serializer_groups' => null]));
5960
}
61+
62+
public function testGetPropertiesWithAllGroup()
63+
{
64+
$this->assertSame(['somethingWithoutGroup', 'somethingWithGroup'], $this->extractor->getProperties(DefaultGroupDummy::class, ['serializer_groups' => ['*']]));
65+
}
66+
67+
public function testGetPropertiesWithDefaultGroup()
68+
{
69+
$this->assertSame(['somethingWithoutGroup'], $this->extractor->getProperties(DefaultGroupDummy::class, ['serializer_groups' => ['_default']]));
70+
}
6071
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
5+
6+
use Symfony\Component\Serializer\Annotation\Groups;
7+
8+
final class DefaultGroupDummy
9+
{
10+
11+
public $somethingWithoutGroup;
12+
13+
/**
14+
* @Groups({"a"})
15+
*/
16+
public $somethingWithGroup;
17+
}

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Return empty collections as `ArrayObject` from `Serializer::normalize()` when `PRESERVE_EMPTY_OBJECTS` is set
1010
* Add support for collecting type errors during denormalization
1111
* Add missing arguments in `MissingConstructorArgumentsException`
12+
* Add `_default` group serialization context that allow serializing properties without explicit group
1213

1314
5.3
1415
---

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected function getAllowedAttributes($classOrObject, array $context, bool $at
250250
// If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties()
251251
if (
252252
!$ignore &&
253-
([] === $groups || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups)) &&
253+
([] === $groups || array_intersect($this->getAttributeGroups($attributeMetadata), $groups)) &&
254254
$this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context)
255255
) {
256256
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
@@ -272,6 +272,13 @@ protected function getGroups(array $context): array
272272
return is_scalar($groups) ? (array) $groups : $groups;
273273
}
274274

275+
protected function getAttributeGroups(AttributeMetadataInterface $attributeMetadata): array
276+
{
277+
$groups = empty($attributeMetadata->getGroups()) ? ['_default'] : $attributeMetadata->getGroups();
278+
279+
return array_merge($groups, ['*']);
280+
}
281+
275282
/**
276283
* Is this attribute allowed?
277284
*

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public function testGetAllowedAttributesAsString()
7878

7979
$result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['*']], true);
8080
$this->assertEquals(['a1', 'a2', 'a3', 'a4'], $result);
81+
82+
$result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['_default']], true);
83+
$this->assertEquals(['a1'], $result);
8184
}
8285

8386
public function testGetAllowedAttributesAsObjects()
@@ -113,6 +116,9 @@ public function testGetAllowedAttributesAsObjects()
113116

114117
$result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['*']], false);
115118
$this->assertEquals([$a1, $a2, $a3, $a4], $result);
119+
120+
$result = $this->normalizer->getAllowedAttributes('c', [AbstractNormalizer::GROUPS => ['_default']], false);
121+
$this->assertEquals([$a1], $result);
116122
}
117123

118124
public function testObjectWithStaticConstructor()

0 commit comments

Comments
 (0)