Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcfdef73

Browse files
committed
[Serializer] Allow default group in serialization context
Fix#32622
1 parent42b994c commitcfdef73

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
namespaceSymfony\Component\PropertyInfo\Extractor;
1313

1414
useSymfony\Component\PropertyInfo\PropertyListExtractorInterface;
15+
useSymfony\Component\Serializer\Mapping\AttributeMetadataInterface;
1516
useSymfony\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+
privatefunctiongetAttributeGroups(AttributeMetadataInterface$serializerAttributeMetadata):array
61+
{
62+
$groups =empty($serializerAttributeMetadata->getGroups()) ? ['_default'] :$serializerAttributeMetadata->getGroups();
63+
64+
returnarray_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
usePHPUnit\Framework\TestCase;
1616
useSymfony\Component\PropertyInfo\Extractor\SerializerExtractor;
1717
useSymfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
18+
useSymfony\Component\PropertyInfo\Tests\Fixtures\DefaultGroupDummy;
1819
useSymfony\Component\PropertyInfo\Tests\Fixtures\IgnorePropertyDummy;
1920
useSymfony\Component\Serializer\Annotation\Ignore;
2021
useSymfony\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+
publicfunctiontestGetPropertiesWithAllGroup()
63+
{
64+
$this->assertSame(['somethingWithoutGroup','somethingWithGroup'],$this->extractor->getProperties(DefaultGroupDummy::class, ['serializer_groups' => ['*']]));
65+
}
66+
67+
publicfunctiontestGetPropertiesWithDefaultGroup()
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+
namespaceSymfony\Component\PropertyInfo\Tests\Fixtures;
5+
6+
useSymfony\Component\Serializer\Annotation\Groups;
7+
8+
finalclass 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
returnis_scalar($groups) ? (array)$groups :$groups;
273273
}
274274

275+
protectedfunctiongetAttributeGroups(AttributeMetadataInterface$attributeMetadata):array
276+
{
277+
$groups =empty($attributeMetadata->getGroups()) ? ['_default'] :$attributeMetadata->getGroups();
278+
279+
returnarray_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
publicfunctiontestGetAllowedAttributesAsObjects()
@@ -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
publicfunctiontestObjectWithStaticConstructor()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp