-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add ability to collect denormalization errors #38968
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?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\Exception; | ||
|
||
interface AggregableExceptionInterface extends ExceptionInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?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\Exception; | ||
|
||
final class AggregatedException extends RuntimeException implements AggregableExceptionInterface | ||
{ | ||
/** | ||
* This property is public for performance reasons to reduce the number of hot path function calls. | ||
* | ||
* @var bool | ||
* | ||
* @internal | ||
*/ | ||
public $isEmpty = true; | ||
|
||
/** | ||
* @var array<string, AggregableExceptionInterface> | ||
*/ | ||
private $collection; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct('Errors occurred during the denormalization process'); | ||
} | ||
|
||
public function put(string $param, AggregableExceptionInterface $exception): self | ||
{ | ||
$this->isEmpty = false; | ||
$this->collection[$param] = $exception; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array<string, AggregableExceptionInterface> | ||
*/ | ||
public function all(): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($this->collection as $param => $exception) { | ||
if ($exception instanceof self) { | ||
foreach ($exception->all() as $nestedParams => $nestedException) { | ||
$result["$param.$nestedParams"] = $nestedException; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
$result[$param] = $exception; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?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\Exception; | ||
|
||
final class VariadicConstructorArgumentsException extends RuntimeException implements AggregableExceptionInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
use Symfony\Component\Serializer\Encoder\CsvEncoder; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\Encoder\XmlEncoder; | ||
use Symfony\Component\Serializer\Exception\AggregableExceptionInterface; | ||
use Symfony\Component\Serializer\Exception\AggregatedException; | ||
use Symfony\Component\Serializer\Exception\ExtraAttributesException; | ||
use Symfony\Component\Serializer\Exception\LogicException; | ||
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; | ||
|
@@ -311,6 +313,8 @@ public function denormalize($data, string $type, string $format = null, array $c | |
$object = $this->instantiateObject($normalizedData, $type, $context, $reflectionClass, $allowedAttributes, $format); | ||
$resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object); | ||
|
||
$aggregatedException = new AggregatedException(); | ||
|
||
foreach ($normalizedData as $attribute => $value) { | ||
if ($this->nameConverter) { | ||
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context); | ||
|
@@ -331,18 +335,31 @@ public function denormalize($data, string $type, string $format = null, array $c | |
} | ||
} | ||
|
||
$value = $this->validateAndDenormalize($resolvedClass, $attribute, $value, $format, $context); | ||
try { | ||
$this->setAttributeValue($object, $attribute, $value, $format, $context); | ||
} catch (InvalidArgumentException $e) { | ||
throw new NotNormalizableValueException(sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type), $e->getCode(), $e); | ||
$value = $this->validateAndDenormalize($resolvedClass, $attribute, $value, $format, $context); | ||
try { | ||
$this->setAttributeValue($object, $attribute, $value, $format, $context); | ||
} catch (InvalidArgumentException $e) { | ||
throw new NotNormalizableValueException(sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type), $e->getCode(), $e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we try to avoid the extra try / catch. I'm nitpicking but catching and throwing is very costly in PHP so let's avoid it when not strictly necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inner try / catch was here for some reason. It was wrapped around to handle all calls that throw exceptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think he suggested to remove the inner try/catch, add another catch clause to the outer one when you deal with this exception by throwing or aggregating it depending on the context |
||
} | ||
} catch (AggregableExceptionInterface $e) { | ||
if (!($context[self::COLLECT_EXCEPTIONS] ?? false)) { | ||
throw $e; | ||
} | ||
|
||
$aggregatedException->put($attribute, $e); | ||
continue; | ||
} | ||
} | ||
|
||
if (!empty($extraAttributes)) { | ||
throw new ExtraAttributesException($extraAttributes); | ||
} | ||
|
||
if (($context[self::COLLECT_EXCEPTIONS] ?? false) && !$aggregatedException->isEmpty) { | ||
throw $aggregatedException; | ||
} | ||
|
||
return $object; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
class CollectErrorsArrayDummy | ||
{ | ||
/** | ||
* @var \DateTimeImmutable | ||
*/ | ||
public $date; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
class CollectErrorsDummy | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
public $int; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $array; | ||
|
||
/** | ||
* @var CollectErrorsArrayDummy[] | ||
*/ | ||
public $arrayOfObjects; | ||
|
||
/** | ||
* @var CollectErrorsArrayDummy[] | ||
*/ | ||
public $stringArrayOfObjects; | ||
|
||
/** | ||
* @var CollectErrorsObjectDummy | ||
*/ | ||
public $object; | ||
|
||
/** | ||
* @var CollectErrorsVariadicObjectDummy | ||
*/ | ||
public $variadicObject; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
class CollectErrorsObjectDummy | ||
{ | ||
public $foo; | ||
public $bar; | ||
public $baz; | ||
|
||
public function __construct(int $foo, int $bar, int $baz) | ||
{ | ||
$this->foo = $foo; | ||
$this->bar = $bar; | ||
$this->baz = $baz; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
class CollectErrorsVariadicObjectDummy | ||
{ | ||
public $foo; | ||
|
||
/** @var CollectErrorsVariadicObjectDummy[] */ | ||
public $args; | ||
|
||
public function __construct(int $foo, CollectErrorsVariadicObjectDummy ...$args) | ||
{ | ||
$this->foo = $foo; | ||
$this->args = $args; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a quick look I think there is two spots that should not be handled:
symfony/src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php
Line 102 in 49eafee
symfony/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php
Line 48 in 49eafee
Those cases are because of an invalid type or missing class, I think they should stop the denormalization process as usual.