Skip to content

[Serializer] Fix partial denormalization with missing constructor arguments #53115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -378,32 +378,32 @@ protected function instantiateObject(array &$data, string $class, array &$contex
} elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
$parameterData = $data[$key];
if (null === $parameterData && $constructorParameter->allowsNull()) {
$params[] = null;
$params[$paramName] = null;
$unsetKeys[] = $key;

continue;
}

try {
$params[] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
$params[$paramName] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
} catch (NotNormalizableValueException $exception) {
if (!isset($context['not_normalizable_value_exceptions'])) {
throw $exception;
}

$context['not_normalizable_value_exceptions'][] = $exception;
$params[] = $parameterData;
$params[$paramName] = $parameterData;
}

$unsetKeys[] = $key;
} elseif (\array_key_exists($key, $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
$params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
$params[$paramName] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
} elseif (\array_key_exists($key, $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
$params[] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
$params[$paramName] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
} elseif ($constructorParameter->isDefaultValueAvailable()) {
$params[] = $constructorParameter->getDefaultValue();
$params[$paramName] = $constructorParameter->getDefaultValue();
} elseif ($constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
$params[] = null;
$params[$paramName] = null;
} else {
if (!isset($context['not_normalizable_value_exceptions'])) {
$missingConstructorArguments[] = $constructorParameter->name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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;

final class Php80WithOptionalConstructorParameter
{
public function __construct(
public string $one,
public string $two,
public ?string $three = null,
) {
}
}
56 changes: 56 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
use Symfony\Component\Serializer\Tests\Fixtures\Php80WithOptionalConstructorParameter;
use Symfony\Component\Serializer\Tests\Fixtures\Php80WithPromotedTypedConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\WithTypedConstructor;
Expand Down Expand Up @@ -1433,6 +1434,61 @@ public static function provideCollectDenormalizationErrors()
[new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()))],
];
}

/**
* @requires PHP 8
*/
public function testPartialDenormalizationWithMissingConstructorTypes()
{
$json = '{"one": "one string", "three": "three string"}';

$extractor = new PropertyInfoExtractor([], [new ReflectionExtractor()]);

$serializer = new Serializer(
[new ObjectNormalizer(null, null, null, $extractor)],
['json' => new JsonEncoder()]
);

try {
$serializer->deserialize($json, Php80WithOptionalConstructorParameter::class, 'json', [
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);

$this->fail();
} catch (\Throwable $th) {
$this->assertInstanceOf(PartialDenormalizationException::class, $th);
}

$this->assertInstanceOf(Php80WithOptionalConstructorParameter::class, $object = $th->getData());

$this->assertSame('one string', $object->one);
$this->assertFalse(isset($object->two));
$this->assertSame('three string', $object->three);

$exceptionsAsArray = array_map(function (NotNormalizableValueException $e): array {
return [
'currentType' => $e->getCurrentType(),
'expectedTypes' => $e->getExpectedTypes(),
'path' => $e->getPath(),
'useMessageForUser' => $e->canUseMessageForUser(),
'message' => $e->getMessage(),
];
}, $th->getErrors());

$expected = [
[
'currentType' => 'array',
'expectedTypes' => [
'unknown',
],
'path' => null,
'useMessageForUser' => true,
'message' => 'Failed to create object because the class misses the "two" property.',
],
];

$this->assertSame($expected, $exceptionsAsArray);
}
}

class Model
Expand Down