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

Skip to content

Commit 7df2468

Browse files
committed
bug #48661 [Serializer] fix context attribute with serializedName (nikophil)
This PR was merged into the 6.2 branch. Discussion ---------- [Serializer] fix context attribute with serializedName | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Hello, this PR fixes a bug where the `#[Context]` attribute is not read in denormalization process when it is used along with `#[SerializedName]` in a constructor. In the test I provided, the two dates are not equals without the fix, although the same context is provided. Commits ------- c87ac90 [Serializer] fix context attribute with serializedName
2 parents 1836270 + c87ac90 commit 7df2468

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ protected function instantiateObject(array &$data, string $class, array &$contex
331331
$params = [];
332332
foreach ($constructorParameters as $constructorParameter) {
333333
$paramName = $constructorParameter->name;
334+
$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
334335
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
335-
$attributeContext = $this->getAttributeDenormalizationContext($class, $key, $context);
336336

337337
$allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
338338
$ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1717
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
18+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1819
use Symfony\Component\PropertyInfo\Type;
1920
use Symfony\Component\Serializer\Annotation\SerializedName;
2021
use Symfony\Component\Serializer\Annotation\SerializedPath;
@@ -32,6 +33,7 @@
3233
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
3334
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
3435
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
36+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
3537
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
3638
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
3739
use Symfony\Component\Serializer\Serializer;
@@ -41,6 +43,7 @@
4143
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild;
4244
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild;
4345
use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux;
46+
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummyWithContextAttribute;
4447

4548
class AbstractObjectNormalizerTest extends TestCase
4649
{
@@ -525,6 +528,20 @@ public function testDenormalizeRecursiveWithObjectAttributeWithStringValue()
525528

526529
$this->assertInstanceOf(ObjectInner::class, $obj->getInner());
527530
}
531+
532+
public function testDenormalizeUsesContextAttributeForPropertiesInConstructorWithSeralizedName()
533+
{
534+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
535+
536+
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
537+
$normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, $extractor);
538+
$serializer = new Serializer([new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'd-m-Y']), $normalizer]);
539+
540+
/** @var ObjectDummyWithContextAttribute $obj */
541+
$obj = $serializer->denormalize(['property_with_serialized_name' => '01-02-2022', 'propertyWithoutSerializedName' => '01-02-2022'], ObjectDummyWithContextAttribute::class);
542+
543+
$this->assertSame($obj->propertyWithSerializedName->format('Y-m-d'), $obj->propertyWithoutSerializedName->format('Y-m-d'));
544+
}
528545
}
529546

530547
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Normalizer\Features;
13+
14+
use Symfony\Component\Serializer\Annotation\Context;
15+
use Symfony\Component\Serializer\Annotation\SerializedName;
16+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
17+
18+
final class ObjectDummyWithContextAttribute
19+
{
20+
public function __construct(
21+
#[Context([DateTimeNormalizer::FORMAT_KEY => 'm-d-Y'])]
22+
#[SerializedName('property_with_serialized_name')]
23+
public \DateTimeImmutable $propertyWithSerializedName,
24+
25+
#[Context([DateTimeNormalizer::FORMAT_KEY => 'm-d-Y'])]
26+
public \DateTimeImmutable $propertyWithoutSerializedName,
27+
) {
28+
}
29+
}

0 commit comments

Comments
 (0)