8000 bug #28904 [Serializer] fix MetadataAwareNameConverter break denormal… · symfony/symfony@915870e · GitHub
[go: up one dir, main page]

Skip to content

Commit 915870e

Browse files
bug #28904 [Serializer] fix MetadataAwareNameConverter break denormalization (Britaliope)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Serializer] fix MetadataAwareNameConverter break denormalization | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - During denormalization of an object, the `normalize` function of the `MetadataAwareNormalizer` is called to find the serialized name of constructor arguments. This do not work if the constructor argument is not in the serialized representation of the object (for example given with a `default_constructor_arguments` option ). **Checklist** - [x] Add test to cover the bug Commits ------- faf8b00 [Serializer] fix MetadataAwareNameConverter break denormalization
2 parents 69d04b5 + faf8b00 commit 915870e

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ private function getCacheValueForNormalization(string $propertyName, string $cla
7575
return null;
7676
}
7777

78-
return $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata()[$propertyName]->getSerializedName() ?? null;
78+
$attributesMetadata = $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata();
79+
if (!isset($attributesMetadata[$propertyName])) {
80+
return null;
81+
}
82+
83+
return $attributesMetadata[$propertyName]->getSerializedName() ?? null;
7984
}
8085

8186
private function normalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = array()): string
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
class NotSerializedConstructorArgumentDummy
15+
{
16+
private $bar;
17+
18+
public function __construct($foo)
19+
{
20+
}
21+
22+
public function getBar()
23+
{
24+
return $this->bar;
25+
}
26+
27+
public function setBar($bar)
28+
{
29+
$this->bar = $bar;
30+
}
31+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
2121
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
2222
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
23+
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
2324
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2425
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
2526
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
@@ -29,6 +30,7 @@
2930
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
3031
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
3132
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
33+
use Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy;
3234
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3335

3436
/**
@@ -365,6 +367,27 @@ public function testGroupsDenormalizeWithNameConverter()
365367
);
366368
}
367369

370+
public function testMetadataAwareNameConvertorWithNotSerializedConstructorParameter()
371+
{
372+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
373+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
374+
$this->normalizer->setSerializer($this->serializer);
375+
376+
$obj = new NotSerializedConstructorArgumentDummy('buz');
377+
$obj->setBar('xyz');
378+
379+
$this->assertEquals(
380+
$obj,
381+
$this->normalizer->denormalize(array('bar' => 'xyz'),
382+
'Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy',
383+
null,
384+
array(ObjectNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => array(
385+
'Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy' => array('foo' => 'buz'),
386+
))
387+
)
388+
);
389+
}
390+
368391
/**
369392
* @dataProvider provideCallbacks
370393
*/

0 commit comments

Comments
 (0)
0