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

Commita440104

Browse files
committed
[Serializer] Add default groups
1 parent19f5240 commita440104

File tree

12 files changed

+136
-15
lines changed

12 files changed

+136
-15
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ public function getProperties(string $class, array $context = []): ?array
3838
returnnull;
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+
4145
$properties = [];
4246
$serializerClassMetadata =$this->classMetadataFactory->getMetadataFor($class);
4347

4448
foreach ($serializerClassMetadata->getAttributesMetadata()as$serializerAttributeMetadata) {
45-
if (!$serializerAttributeMetadata->isIgnored() && (null ===$context['serializer_groups'] ||array_intersect($context['serializer_groups'],$serializerAttributeMetadata->getGroups()))) {
49+
if (!$serializerAttributeMetadata->isIgnored() && (!$groupsHasBeenDefined ||array_intersect(array_merge($serializerAttributeMetadata->getGroups(), ['*']),$groups))) {
4650
$properties[] =$serializerAttributeMetadata->getName();
4751
}
4852
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function testGetProperties()
4343
publicfunctiontestGetPropertiesWithIgnoredProperties()
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']]));
4648
}
4749

4850
publicfunctiontestGetPropertiesWithAnyGroup()

‎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'])]
22+
#[Groups(['a','Default','IgnorePropertyDummy'])]
2323
public$visibleProperty;
2424

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

‎src/Symfony/Component/Serializer/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add`Default` and "class name" default groups
8+
49
7.0
510
---
611

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

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

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

133-
if ($contextGroups && !$metadataGroups) {
136+
if ($contextGroupsHasBeenDefined && !$metadataGroups) {
134137
continue;
135138
}
136139

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

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,25 +214,32 @@ protected function getAllowedAttributes(string|object $classOrObject, array $con
214214
}
215215

216216
$groups =$this->getGroups($context);
217+
$groupsHasBeenDefined = [] !==$groups;
218+
219+
$classMetadata =$this->classMetadataFactory->getMetadataFor($classOrObject);
220+
$class =$classMetadata->getName();
221+
222+
$groups =array_merge($groups, ['Default', (false !==$nsSep =strrpos($class,'\\')) ?substr($class,$nsSep +1) :$class]);
217223

218224
$allowedAttributes = [];
219225
$ignoreUsed =false;
220-
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata()as$attributeMetadata) {
226+
227+
foreach ($classMetadata->getAttributesMetadata()as$attributeMetadata) {
221228
if ($ignore =$attributeMetadata->isIgnored()) {
222229
$ignoreUsed =true;
223230
}
224231

225232
// If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties()
226233
if (
227234
!$ignore
228-
&& ([] ===$groups ||array_intersect(array_merge($attributeMetadata->getGroups(), ['*']),$groups))
235+
&& (!$groupsHasBeenDefined ||array_intersect(array_merge($attributeMetadata->getGroups(), ['*']),$groups))
229236
&&$this->isAllowedAttribute($classOrObject,$name =$attributeMetadata->getName(),null,$context)
230237
) {
231238
$allowedAttributes[] =$attributesAsString ?$name :$attributeMetadata;
232239
}
233240
}
234241

235-
if (!$ignoreUsed &&[] ===$groups &&$allowExtraAttributes) {
242+
if (!$ignoreUsed &&!$groupsHasBeenDefined &&$allowExtraAttributes) {
236243
// Backward Compatibility with the code using this method written before the introduction of @Ignore
237244
returnfalse;
238245
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ 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;
3034

3135
#[Groups(['b'])]
3236
publicfunctionsetBar($bar)
@@ -80,4 +84,24 @@ public function setQuux($quux): void
8084
{
8185
$this->quux =$quux;
8286
}
87+
88+
publicfunctionsetDefault($default)
89+
{
90+
$this->default =$default;
91+
}
92+
93+
publicfunctiongetDefault()
94+
{
95+
return$this->default;
96+
}
97+
98+
publicfunctionsetClassName($className)
99+
{
100+
$this->className =$className;
101+
}
102+
103+
publicfunctiongetClassName()
104+
{
105+
return$this->className;
106+
}
83107
}

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

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

66+
$default =newAttributeMetadata('default');
67+
$default->addGroup('Default');
68+
$expected->addAttributeMetadata($default);
69+
70+
$className =newAttributeMetadata('className');
71+
$className->addGroup('GroupDummy');
72+
$expected->addAttributeMetadata($className);
73+
6674
// load reflection class so that the comparison passes
6775
$expected->getReflectionClass();
6876

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

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ public function testGroupsNormalize()
3131
$obj =newGroupDummy();
3232
$obj->setFoo('foo');
3333
$obj->setBar('bar');
34+
$obj->setQuux('quux');
3435
$obj->setFooBar('fooBar');
3536
$obj->setSymfony('symfony');
3637
$obj->setKevin('kevin');
3738
$obj->setCoopTilleuls('coopTilleuls');
39+
$obj->setDefault('default');
40+
$obj->setClassName('className');
3841

3942
$this->assertEquals([
4043
'bar' =>'bar',
44+
'default' =>'default',
45+
'className' =>'className',
4146
],$normalizer->normalize($obj,null, ['groups' => ['c']]));
4247

4348
$this->assertEquals([
@@ -47,35 +52,76 @@ public function testGroupsNormalize()
4752
'bar' =>'bar',
4853
'kevin' =>'kevin',
4954
'coopTilleuls' =>'coopTilleuls',
55+
'default' =>'default',
56+
'className' =>'className',
5057
],$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));
5175
}
5276

5377
publicfunctiontestGroupsDenormalize()
5478
{
5579
$normalizer =$this->getDenormalizerForGroups();
5680

5781
$obj =newGroupDummy();
58-
$obj->setFoo('foo');
82+
$obj->setDefault('default');
83+
$obj->setClassName('className');
5984

60-
$data = ['foo' =>'foo','bar' =>'bar'];
85+
$data = [
86+
'foo' =>'foo',
87+
'bar' =>'bar',
88+
'quux' =>'quux',
89+
'default' =>'default',
90+
'className' =>'className',
91+
];
6192

62-
$normalized =$normalizer->denormalize(
93+
$denormalized =$normalizer->denormalize(
94+
$data,
95+
GroupDummy::class,
96+
null,
97+
['groups' => ['unknown']]
98+
);
99+
$this->assertEquals($obj,$denormalized);
100+
101+
$obj->setFoo('foo');
102+
103+
$denormalized =$normalizer->denormalize(
63104
$data,
64105
GroupDummy::class,
65106
null,
66107
['groups' => ['a']]
67108
);
68-
$this->assertEquals($obj,$normalized);
109+
$this->assertEquals($obj,$denormalized);
69110

70111
$obj->setBar('bar');
71112

72-
$normalized =$normalizer->denormalize(
113+
$denormalized =$normalizer->denormalize(
73114
$data,
74115
GroupDummy::class,
75116
null,
76117
['groups' => ['a','b']]
77118
);
78-
$this->assertEquals($obj,$normalized);
119+
$this->assertEquals($obj,$denormalized);
120+
121+
$obj->setQuux('quux');
122+
123+
$denormalized =$normalizer->denormalize($data, GroupDummy::class);
124+
$this->assertEquals($obj,$denormalized);
79125
}
80126

81127
publicfunctiontestNormalizeNoPropertyInGroup()
@@ -84,7 +130,12 @@ public function testNormalizeNoPropertyInGroup()
84130

85131
$obj =newGroupDummy();
86132
$obj->setFoo('foo');
133+
$obj->setDefault('default');
134+
$obj->setClassName('className');
87135

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ public function testGroupsNormalizeWithNameConverter()
292292
'bar' =>null,
293293
'foo_bar' =>'@dunglas',
294294
'symfony' =>'@coopTilleuls',
295+
'default' =>null,
296+
'class_name' =>null,
295297
],
296298
$this->normalizer->normalize($obj,null, [GetSetMethodNormalizer::GROUPS => ['name_converter']])
297299
);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp