8000 bug #14415 [Serializer] Fix a bug when using groups together with a n… · symfony/symfony@09cde7c · GitHub
[go: up one dir, main page]

Skip to content

Commit 09cde7c

Browse files
committed
bug #14415 [Serializer] Fix a bug when using groups together with a name converter (dunglas)
This PR was squashed before being merged into the 2.7 branch (closes #14415). Discussion ---------- [Serializer] Fix a bug when using groups together with a name converter | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a * Fix the bug * Increase test coverage Commits ------- 454ef8c [Serializer] Fix a bug when using groups together with a name converter
2 parents f85ba5d + 454ef8c commit 09cde7c

File tree

8 files changed

+178
-14
lines changed

8 files changed

+178
-14
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public function normalize($object, $format = null, array $context = array())
5858
foreach ($reflectionMethods as $method) {
5959
if ($this->isGetMethod($method)) {
6060
$attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
61-
6261
if (in_array($attributeName, $this->ignoredAttributes)) {
6362
continue;
6463
}
@@ -104,14 +103,14 @@ public function denormalize($data, $class, $format = null, array $context = arra
104103
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
105104

106105
foreach ($normalizedData as $attribute => $value) {
106+
if ($this->nameConverter) {
107+
$attribute = $this->nameConverter->denormalize($attribute);
108+
}
109+
107110
$allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
108111
$ignored = in_array($attribute, $this->ignoredAttributes);
109112

110113
if ($allowed && !$ignored) {
111-
if ($this->nameConverter) {
112-
$attribute = $this->nameConverter->denormalize($attribute);
113-
}
114-
115114
$setter = 'set'.ucfirst($attribute);
116115

117116
if (method_exists($object, $setter)) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ public function denormalize($data, $class, $format = null, array $context = arra
141141
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
142142

143143
foreach ($normalizedData as $attribute => $value) {
144+
if ($this->nameConverter) {
145+
$attribute = $this->nameConverter->denormalize($attribute);
146+
}
147+
144148
$allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
145149
$ignored = in_array($attribute, $this->ignoredAttributes);
146150

147151
if ($allowed && !$ignored) {
148-
if ($this->nameConverter) {
149-
$attribute = $this->nameConverter->normalize($attribute);
150-
}
151-
152152
try {
153153
$this->propertyAccessor->setValue($object, $attribute, $value);
154154
} catch (NoSuchPropertyException $exception) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
2222
* @Groups({"a"})
2323
*/
2424
private $foo;
25+
/**
26+
* @Groups({"b", "c", "name_converter"})
27+
*/
2528
protected $bar;
2629
private $fooBar;
2730
private $symfony;
@@ -58,7 +61,7 @@ public function setFooBar($fooBar)
5861
}
5962

6063
/**
61-
* @Groups({"a", "b"})
64+
* @Groups({"a", "b", "name_converter"})
6265
*/
6366
public function isFooBar()
6467
{

src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummyInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
interface GroupDummyInterface
2020
{
2121
/**
22-
* @Groups({"a"})
22+
* @Groups({"a", "name_converter"})
2323
*/
2424
public function getSymfony();
2525
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ public static function createClassMetadata($withParent = false, $withInterface =
3030
$bar = new AttributeMetadata('bar');
3131
$bar->addGroup('b');
3232
$bar->addGroup('c');
33+
$bar->addGroup('name_converter');
3334
$expected->addAttributeMetadata($bar);
3435

3536
$fooBar = new AttributeMetadata('fooBar');
3637
$fooBar->addGroup('a');
3738
$fooBar->addGroup('b');
39+
$fooBar->addGroup('name_converter');
3840
$expected->addAttributeMetadata($fooBar);
3941

4042
$symfony = new AttributeMetadata('symfony');
@@ -53,6 +55,7 @@ public static function createClassMetadata($withParent = false, $withInterface =
5355

5456
if ($withInterface) {
5557
$symfony->addGroup('a');
58+
$symfony->addGroup('name_converter');
5659
}
5760

5861
// load reflection class so that the comparison passes

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1516
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
1617
use Symfony\Component\Serializer\Serializer;
1718
use Symfony\Component\Serializer\SerializerInterface;
@@ -115,6 +116,16 @@ public function testLegacyDenormalizeOnCamelCaseFormat()
115116
$this->assertEquals('camelCase', $obj->getCamelCase());
116117
}
117118

119+
public function testNameConverterSupport()
F438
120+
{
121+
$this->normalizer = new GetSetMethodNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
122+
$obj = $this->normalizer->denormalize(
123+
array('camel_case' => 'camelCase'),
124+
__NAMESPACE__.'\GetSetDummy'
125+
);
126+
$this->assertEquals('camelCase', $obj->getCamelCase());
127+
}
128+
118129
public function testDenormalizeNull()
119130
{
120131
$this->assertEquals(new GetSetDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\GetSetDummy'));
@@ -262,6 +273,48 @@ public function testGroupsDenormalize()
262273
$this->assertEquals($obj, $normalized);
263274
}
264275

276+
public function testGroupsNormalizeWithNameConverter()
277+
{
278+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
279+
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
280+
$this->normalizer->setSerializer($this->serializer);
281+
282+
$obj = new GroupDummy();
283+
$obj->setFooBar('@dunglas');
284+
$obj->setSymfony('@coopTilleuls');
285+
$obj->setCoopTilleuls('les-tilleuls.coop');
286+
287+
$this->assertEquals(
288+
array(
289+
'bar' => null,
290+
'foo_bar' => '@dunglas',
291+
'symfony' => '@coopTilleuls',
292+
),
293+
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
294+
);
295+
}
296+
297+
public function testGroupsDenormalizeWithNameConverter()
298+
{
299+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
300+
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
301+
$this->normalizer->setSerializer($this->serializer);
302+
303+
$obj = new GroupDummy();
304+
$obj->setFooBar('@dunglas');
305+
$obj->setSymfony('@coopTilleuls');
306+
307+
$this->assertEquals(
308+
$obj,
309+
$this->normalizer->denormalize(array(
310+
'bar' => null,
311+
'foo_bar' => '@dunglas',
312+
'symfony' => '@coopTilleuls',
313+
'coop_tilleuls' => 'les-tilleuls.coop',
314+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
315+
);
316+
}
317+
265318
/**
266319
* @dataProvider provideCallbacks
267320
*/

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1516
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1617
use Symfony\Component\Serializer\Serializer;
1718
use Symfony\Component\Serializer\SerializerInterface;
@@ -111,6 +112,16 @@ public function testLegacyDenormalizeOnCamelCaseFormat()
111112
$this->assertEquals('camelCase', $obj->getCamelCase());
112113
}
113114

115+
public function testNameConverterSupport()
116+
{
117+
$this->normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
118+
$obj = $this->normalizer->denormalize(
119+
array('camel_case' => 'camelCase'),
120+
__NAMESPACE__.'\ObjectDummy'
121+
);
122+
$this->assertEquals('camelCase', $obj->getCamelCase());
123+
}
124+
114125
public function testDenormalizeNull()
115126
{
116127
$this->assertEquals(new ObjectDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\ObjectDummy'));
@@ -206,6 +217,48 @@ public function testGroupsDenormalize()
206217
$this->assertEquals($obj, $normalized);
207218
}
208219

220+
public function testGroupsNormalizeWithNameConverter()
221+
{
222+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
223+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
224+
$this->normalizer->setSerializer($this->serializer);
225+
226+
$obj = new GroupDummy();
227+
$obj->setFooBar('@dunglas');
228+
$obj->setSymfony('@coopTilleuls');
229+
$obj->setCoopTilleuls('les-tilleuls.coop');
230+
231+
$this->assertEquals(
232+
array(
233+
'bar' => null,
234+
'foo_bar' => '@dunglas',
235+
'symfony' => '@coopTilleuls',
236+
),
237+
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
238+
);
239+
}
240+
241+
public function testGroupsDenormalizeWithNameConverter()
242+
{
243+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
244+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
245+
$this->normalizer->setSerializer($this->serializer);
246+
247+
$obj = new GroupDummy();
248+
$obj->setFooBar('@dunglas');
249+
$obj->setSymfony('@coopTilleuls');
250+
251+
$this->assertEquals(
252+
$obj,
253+
$this< F438 /span>->normalizer->denormalize(array(
254+
'bar' => null,
255+
'foo_bar' => '@dunglas',
256+
'symfony' => '@coopTilleuls',
257+
'coop_tilleuls' => 'les-tilleuls.coop',
258+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
259+
);
260+
}
261+
209262
/**
210263
* @dataProvider provideCallbacks
211264
*/
@@ -423,7 +476,7 @@ public function setCamelCase($camelCase)
423476

424477
public function otherMethod()
425478
{
426-
throw new \RuntimeException("Dummy::otherMethod() should not be called");
479+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
427480
}
428481

429482
public function setObject($object)
@@ -462,7 +515,7 @@ public function isBaz()
462515

463516
public function otherMethod()
464517
{
465-
throw new \RuntimeException("Dummy::otherMethod() should not be called");
518+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
466519
}
467520
}
468521

@@ -495,6 +548,6 @@ public function getBaz()
495548

496549
public function otherMethod()
497550
{
498-
throw new \RuntimeException("Dummy::otherMethod() should not be called");
551+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
499552
}
500553
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1616
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
17+
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1718
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
1819
use Symfony\Component\Serializer\Serializer;
1920
use Symfony\Component\Serializer\SerializerInterface;
@@ -129,6 +130,16 @@ public function testLegacyCamelizedAttributesDenormalize()
129130
), __NAMESPACE__.'\PropertyCamelizedDummy'), $obj);
130131
}
131132

133+
public function testNameConverterSupport()
134+
{
135+
$this->normalizer = new PropertyNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
136+
$obj = $this->normalizer->denormalize(
137+
array('camel_case' => 'camelCase'),
138+
__NAMESPACE__.'\PropertyDummy'
139+
);
140+
$this->assertEquals('camelCase', $obj->getCamelCase());
141+
}
142+
132143
public function testConstructorDenormalize()
133144
{
134145
$obj = $this->normalizer->denormalize(
@@ -239,6 +250,48 @@ public function testGroupsDenormalize()
239250
$this->assertEquals($obj, $normalized);
240251
}
241252

253+
public function testGroupsNormalizeWithNameConverter()
254+
{
255+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
256+
$this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
257+
$this->normalizer->setSerializer($this->serializer);
258+
259+
$obj = new GroupDummy();
260+
$obj->setFooBar('@dunglas');
261+
$obj->setSymfony('@coopTilleuls');
262+
$obj->setCoopTilleuls('les-tilleuls.coop');
263+
264+
$this->assertEquals(
265+
array(
266+
'bar' => null,
267+
'foo_bar' => '@dunglas',
268+
'symfony' => '@coopTilleuls',
269+
),
270+
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
271+
);
272+
}
273+
274+
public function testGroupsDenormalizeWithNameConverter()
275+
{
276+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
277+
$this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
278+
$this->normalizer->setSerializer($this->serializer);
279+
280+
$obj = new GroupDummy();
281+
$obj->setFooBar('@dunglas');
282+
$obj->setSymfony('@coopTilleuls');
283+
284+
$this->assertEquals(
285+
$obj,
286+
$this->normalizer->denormalize(array(
287+
'bar' => null,
288+
'foo_bar' => '@dunglas',
289+
'symfony' => '@coopTilleuls',
290+
'coop_tilleuls' => 'les-tilleuls.coop',
291+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
292+
);
293+
}
294+
242295
public function provideCallbacks()
243296
{
244297
return array(

0 commit comments

Comments
 (0)
0