-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Serializer] Add ability to collect denormalization errors #38165
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
Closed
julienfalque
wants to merge
4
commits into
symfony:master
from
julienfalque:serializer-error-collection
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2946d58
[Serializer] Add ability to collect denormalization errors
julienfalque efb8dff
Remove redundant PHPDocs
julienfalque 6488a62
Make invariant violation exception mandatory
julienfalque a974fc6
Failing to write property is not an invariant violation
julienfalque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/Symfony/Component/Serializer/Exception/InvariantViolationException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?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; | ||
|
||
use Symfony\Component\Serializer\InvariantViolation; | ||
|
||
final class InvariantViolationException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
private $violations; | ||
|
||
/** | ||
* @param array<string, array<InvariantViolation>> $violations | ||
*/ | ||
public function __construct(array $violations) | ||
{ | ||
parent::__construct('Denormalization failed because some values were invalid.'); | ||
|
||
$this->violations = $violations; | ||
} | ||
|
||
/** | ||
* @return array<string, array<InvariantViolation>> | ||
*/ | ||
public function getViolations(): array | ||
{ | ||
return $this->violations; | ||
} | ||
|
||
/** | ||
* @return array<string, array<InvariantViolation>> | ||
*/ | ||
public function getViolationsNestedIn(string $parentPath): array | ||
{ | ||
if ('' === $parentPath) { | ||
throw new \InvalidArgumentException('Parent path cannot be empty.'); | ||
} | ||
|
||
$nestedViolations = []; | ||
|
||
foreach ($this->violations as $path => $violations) { | ||
$path = '' !== $path ? "{$parentPath}.{$path}" : $parentPath; | ||
|
||
$nestedViolations[$path] = $violations; | ||
} | ||
|
||
return $nestedViolations; | ||
} | ||
|
||
/** | ||
* @return array<string, array<string>> | ||
*/ | ||
public function getViolationMessages(): array | ||
{ | ||
$messages = []; | ||
|
||
foreach ($this->violations as $path => $violations) { | ||
foreach ($violations as $violation) { | ||
$messages[$path][] = $violation->getMessage(); | ||
} | ||
} | ||
|
||
return $messages; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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; | ||
|
||
class InvariantViolation | ||
{ | ||
private $normalizedValue; | ||
private $message; | ||
private $exception; | ||
|
||
public function __construct($normalizedValue, string $message, \Throwable $exception) | ||
{ | ||
$this->normalizedValue = $normalizedValue; | ||
$this->message = $message; | ||
$this->exception = $exception; | ||
} | ||
|
||
public function getNormalizedValue() | ||
{ | ||
return $this->normalizedValue; | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getException(): \Throwable | ||
{ | ||
return $this->exception; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So, the idea is that all denormalizers need to be aware of the
COLLECT_INVARIANT_VIOLATIONS
context key and behave differently based on this? Either throwing aNotNormalizableValueException
or aInvariantViolationException
? Is that correct?Could we just catch the existing
NotNormalizableValueException
? Or is the problem that these error messages are currently a bit "internal" (e.g.Failed to denormalize attribute foo for class Foo\Bar
) and so the new exception message allows denormalizers to advertise a safer, "validation" message? Or is there another purpose?If the only purpose is to advertise a validation-safe message, perhaps we could add a new optional property+method (thinking out loud) to
NotNormalizableValueException
that contains this - e.g.getValidationMessage()
? Or maybe I'm way off understanding the mechanics :)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.
Yes, I think collecting denormalization errors should be opt-in. Though throwing
InvariantViolationException
is the only implemenation I managed to get working for now.My first attempt was to wrap all denormalizations using an interface (e.g.
InvariantViolationCatcherInterface
). One implementation would just throw immediatly (current behavior) and another would collect errors instead. But I faced too many issues and gave up with this approach. Maybe I should give it another try?Here we catch
InvariantViolationException
because we rely on nested denormalizers calls to denormalize nested values. Those denormalizers will only throw this kind of exception for each invariant violation ifCOLLECT_INVARIANT_VIOLATIONS
is enabled, so if we catch one here, we can assume it's enabled without actually checking the context in this method.I'd like to introduce an interface for exceptions that would simplify this maybe, but I'll wait until we're sure the current approach is correct.
Indeed, the purpose is to advertise validation-safe messages (and maybe more validation related data later). Not sure
NotNormalizableValueException
is suitable for that though: currently, denormalizers might throw other exceptions (e.g.UnexpectedValueException
). We could either:NotNormalizableValueException
, but replacing a thrown exception with another would be a BC break I guess;WDYT?