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

Skip to content

Commit e4244b2

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

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
236236
*
237237
* @param object $object
238238
* @param string|null $format
239-
* @param array $context
240239
*
241240
* @return string[]
242241
*/
@@ -331,7 +330,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
331330
$resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
332331

333332
foreach ($normalizedData as $attribute => $value) {
334-
if ($this->nameConverter) {
333+
if ($this->nameConverter && (false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes))) {
335334
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context);
336335
}
337336

@@ -560,8 +559,6 @@ private function isMaxDepthReached(array $attributesMetadata, string $class, str
560559
*
561560
* {@inheritdoc}
562561
*
563-
* @param string|null $format
564-
*
565562
* @internal
566563
*/
567564
protected function createChildContext(array $parentContext, $attribute/*, ?string $format */)
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)