8000 [Serializer] Make `ProblemNormalizer` give details about Messenger’s `ValidationFailedException` by MatTheCat · Pull Request #51779 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Make ProblemNormalizer give details about Messenger’s ValidationFailedException #51779

New issue

Have a ques 8000 tion 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 1 commit into from
Sep 30, 2023
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Deprecate passing an annotation reader to the constructor of `AnnotationLoader`
* Allow the `Groups` attribute/annotation on classes
* JsonDecode: Add `json_decode_detailed_errors` option
* Make `ProblemNormalizer` give details about Messenger's `ValidationFailedException`

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Messenger\Exception\ValidationFailedException as MessageValidationFailedException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
use Symfony\Component\Serializer\SerializerAwareInterface;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function normalize(mixed $object, string $format = null, array $context =
),
];
$data['detail'] = implode("\n", array_map(fn ($e) => $e['propertyPath'].': '.$e['title'], $data['violations']));
} elseif ($exception instanceof ValidationFailedException
} elseif (($exception instanceof ValidationFailedException || $exception instanceof MessageValidationFailedException)
&& $this->serializer instanceof NormalizerInterface
&& $this->serializer->supportsNormalization($exception->getViolations(), $format, $context)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Messenger\Exception\ValidationFailedException as MessageValidationFailedException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
Expand Down Expand Up @@ -102,4 +103,28 @@ public function testNormalizeValidationFailedException()
$exception = new HttpException(422, 'Validation Failed', $exception);
$this->assertSame($expected, $this->normalizer->normalize(FlattenException::createFromThrowable($exception), null, ['exception' => $exception]));
}

public function testNormalizeMessageValidationFailedException()
{
$this->normalizer->setSerializer(new Serializer([new ConstraintViolationListNormalizer()]));

$expected = [
'type' => 'https://symfony.com/errors/validation',
'title' => 'Validation Failed',
'status' => 422,
'detail' => 'Invalid value',
'violations' => [
[
'propertyPath' => '',
'title' => 'Invalid value',
'template' => '',
'parameters' => [],
],
],
];

$exception = new MessageValidationFailedException(new \stdClass(), new ConstraintViolationList([new ConstraintViolation('Invalid value', '', [], '', null, null)]));
$exception = new HttpException(422, 'Validation Failed', $exception);
$this->assertSame($expected, $this->normalizer->normalize(FlattenException::createFromThrowable($exception), null, ['exception' => $exception]));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"symfony/form": "^5.4|^6.0|^7.0",
"symfony/http-foundation": "^5.4|^6.0|^7.0",
"symfony/http-kernel": "^5.4|^6.0|^7.0",
"symfony/messenger": "^5.4|^6.0|^7.0",
"symfony/mime": "^5.4|^6.0|^7.0",
"symfony/property-access": "^5.4|^6.0|^7.0",
"symfony/property-info": "^5.4.24|^6.2.11|^7.0",
Expand Down
0