Skip to content

Commit

Permalink
bug #58306 [Serializer] Collect denormalization errors for variadic p…
Browse files Browse the repository at this point in the history
…arams (mtarld)

This PR was merged into the 6.4 branch.

Discussion
----------

[Serializer] Collect denormalization errors for variadic params

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #58281
| License       | MIT

Wrap variadic parameters denormalization in a try/catch to collect denormalization errors.

Commits
-------

cdb2e6edfde [Serializer] Catch `NotNormalizableValueException` for variadic parameters
  • Loading branch information
nicolas-grekas committed Oct 3, 2024
2 parents fddf7ac + 6033573 commit a757250
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,16 @@ protected function instantiateObject(array &$data, string $class, array &$contex

$variadicParameters = [];
foreach ($data[$key] as $parameterKey => $parameterData) {
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
try {
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
} catch (NotNormalizableValueException $exception) {
if (!isset($context['not_normalizable_value_exceptions'])) {
throw $exception;
}

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

$params = array_merge(array_values($params), $variadicParameters);
Expand Down
24 changes: 24 additions & 0 deletions Tests/Fixtures/DummyWithVariadicParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;

use Symfony\Component\Uid\Uuid;

class DummyWithVariadicParameter
{
public array $variadic;

public function __construct(Uuid ...$variadic)
{
$this->variadic = $variadic;
}
}
15 changes: 15 additions & 0 deletions Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithObjectOrNull;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithVariadicParameter;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithVariadicProperty;
use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
use Symfony\Component\Serializer\Tests\Fixtures\FooImplementationDummy;
use Symfony\Component\Serializer\Tests\Fixtures\FooInterfaceDummyDenormalizer;
Expand Down Expand Up @@ -1637,6 +1639,19 @@ public function testPartialDenormalizationWithMissingConstructorTypes()

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

public function testPartialDenormalizationWithInvalidVariadicParameter()
{
$json = '{"variadic": ["a random string"]}';

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

$this->expectException(PartialDenormalizationException::class);

$serializer->deserialize($json, DummyWithVariadicParameter::class, 'json', [
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
}
}

class Model
Expand Down

0 comments on commit a757250

Please sign in to comment.