Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6da3264

Browse files
committed
[Serializer] Fix property name usage for denormalization
1 parent 0b4c37a commit 6da3264

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
331331
$resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
332332

333333
foreach ($normalizedData as $attribute => $value) {
334-
if ($this->nameConverter) {
334+
if ($this->nameConverter && (false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes))) {
335335
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context);
336336
}
337337

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Annotation\Groups;
15+
use Symfony\Component\Serializer\Annotation\SerializedName;
16+
17+
/**
18+
* @author Anthony GRASSIOT <[email protected]>
19+
*/
20+
class OtherSerializedNameDummy
21+
{
22+
/**
23+
* @Groups({"a"})
24+
*/
25+
private $buz;
26+
27+
public function setBuz($buz)
28+
{
29+
$this->buz = $buz;
30+
}
31+
32+
public function getBuz()
33+
{
34+
return $this->buz;
35+
}
36+
37+
/**
38+
* @Groups({"b"})
39+
* @SerializedName("buz")
40+
*/
41+
public function getBuzForExport()
42+
{
43+
return $this->buz.' Rocks';
44+
}
45+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
3333
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3434
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
35+
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
3536
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3637
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
3738
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
@@ -481,6 +482,23 @@ public function testGroupsDenormalizeWithNameConverter()
481482
);
482483
}
483484

485+
public function testGroupsDenormalizeWithMetaDataNameConverter()
486+
{
487+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
488+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
489+
$this->normalizer->setSerializer($this->serializer);
490+
491+
$obj = new OtherSerializedNameDummy();
492+
$obj->setBuz('Aldrin');
493+
494+
$this->assertEquals(
495+
$obj,
496+
$this->normalizer->denormalize([
497+
'buz' => 'Aldrin',
498+
], 'Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy', null, [ObjectNormalizer::GROUPS => ['a']])
499+
);
500+
}
501+
484502
// ignored attributes
485503

486504
protected function getNormalizerForIgnoredAttributes(): ObjectNormalizer

0 commit comments

Comments
 (0)