Skip to content

Commit

Permalink
bug #28904 [Serializer] fix MetadataAwareNameConverter break denormal…
Browse files Browse the repository at this point in the history
…ization (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
  • Loading branch information
nicolas-grekas committed Oct 20, 2018
2 parents 69d04b5 + faf8b00 commit 915870e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
Expand Up @@ -75,7 +75,12 @@ private function getCacheValueForNormalization(string $propertyName, string $cla
return null;
}

return $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata()[$propertyName]->getSerializedName() ?? null;
$attributesMetadata = $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata();
if (!isset($attributesMetadata[$propertyName])) {
return null;
}

return $attributesMetadata[$propertyName]->getSerializedName() ?? null;
}

private function normalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = array()): string
Expand Down
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

class NotSerializedConstructorArgumentDummy
{
private $bar;

public function __construct($foo)
{
}

public function getBar()
{
return $this->bar;
}

public function setBar($bar)
{
$this->bar = $bar;
}
}
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand All @@ -29,6 +30,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy;
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;

/**
Expand Down Expand Up @@ -365,6 +367,27 @@ public function testGroupsDenormalizeWithNameConverter()
);
}

public function testMetadataAwareNameConvertorWithNotSerializedConstructorParameter()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
$this->normalizer->setSerializer($this->serializer);

$obj = new NotSerializedConstructorArgumentDummy('buz');
$obj->setBar('xyz');

$this->assertEquals(
$obj,
$this->normalizer->denormalize(array('bar' => 'xyz'),
'Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy',
null,
array(ObjectNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => array(
'Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy' => array('foo' => 'buz'),
))
)
);
}

/**
* @dataProvider provideCallbacks
*/
Expand Down

0 comments on commit 915870e

Please sign in to comment.