Skip to content

Commit

Permalink
bug #27389 [Serializer] Fix serializer tries to denormalize null valu…
Browse files Browse the repository at this point in the history
…es on nullable properties (ogizanagi)

This PR was merged into the 3.4 branch.

Discussion
----------

[Serializer] Fix serializer tries to denormalize null values on nullable properties

| Q             | A
| ------------- | ---
| Branch?       | 3.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #27384   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

Commits
-------

ca31488 [Serializer] Fix serializer tries to denormalize null values on nullable properties
  • Loading branch information
ogizanagi committed May 30, 2018
2 parents 28c8c85 + ca31488 commit 16ebf43
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Expand Up @@ -349,6 +349,12 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
}
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
$parameterData = $data[$key];
if (null === $parameterData && $constructorParameter->allowsNull()) {
$params[] = null;
// Don't run set for a parameter passed to the constructor
unset($data[$key]);
continue;
}
try {
if (null !== $constructorParameter->getClass()) {
if (!$this->serializer instanceof DenormalizerInterface) {
Expand Down
@@ -0,0 +1,32 @@
<?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 NullableConstructorArgumentDummy
{
private $foo;

public function __construct(?\stdClass $foo)
{
$this->foo = $foo;
}

public function setFoo($foo)
{
$this->foo = 'this setter should not be called when using the constructor argument';
}

public function getFoo()
{
return $this->foo;
}
}
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
Expand Down Expand Up @@ -116,4 +117,15 @@ public function testObjectWithStaticConstructor()
$this->assertEquals('baz', $dummy->quz);
$this->assertNull($dummy->foo);
}

/**
* @requires PHP 7.1
*/
public function testObjectWithNullableConstructorArgument()
{
$normalizer = new ObjectNormalizer();
$dummy = $normalizer->denormalize(array('foo' => null), NullableConstructorArgumentDummy::class);

$this->assertNull($dummy->getFoo());
}
}

0 comments on commit 16ebf43

Please sign in to comment.