Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a9df0c

Browse files
committed
feature #51779 [Serializer] Make ProblemNormalizer give details about Messenger’s ValidationFailedException (MatTheCat)
This PR was merged into the 6.4 branch. Discussion ---------- [Serializer] Make `ProblemNormalizer` give details about Messenger’s `ValidationFailedException` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #51739 | License | MIT This PR fixes #51739 by handling `Symfony\Component\Messenger\Exception\ValidationFailedException` in the `ProblemNormalizer`. Commits ------- 43240ab [Serializer] Make `ProblemNormalizer` give details about Messenger’s `ValidationFailedException`
2 parents 103106d + 43240ab commit 0a9df0c

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Deprecate passing an annotation reader to the constructor of `AnnotationLoader`
1010
* Allow the `Groups` attribute/annotation on classes
1111
* JsonDecode: Add `json_decode_detailed_errors` option
12+
* Make `ProblemNormalizer` give details about Messenger's `ValidationFailedException`
1213

1314
6.3
1415
---

src/Symfony/Component/Serializer/Normalizer/ProblemNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\ErrorHandler\Exception\FlattenException;
1515
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
16+
use Symfony\Component\Messenger\Exception\ValidationFailedException as MessageValidationFailedException;
1617
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1718
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
1819
use Symfony\Component\Serializer\SerializerAwareInterface;
@@ -84,7 +85,7 @@ public function normalize(mixed $object, string $format = null, array $context =
8485
),
8586
];
8687
$data['detail'] = implode("\n", array_map(fn ($e) => $e['propertyPath'].': '.$e['title'], $data['violations']));
87-
} elseif ($exception instanceof ValidationFailedException
88+
} elseif (($exception instanceof ValidationFailedException || $exception instanceof MessageValidationFailedException)
8889
&& $this->serializer instanceof NormalizerInterface
8990
&& $this->serializer->supportsNormalization($exception->getViolations(), $format, $context)
9091
) {

src/Symfony/Component/Serializer/Tests/Normalizer/ProblemNormalizerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\ErrorHandler\Exception\FlattenException;
1616
use Symfony\Component\HttpKernel\Exception\HttpException;
17+
use Symfony\Component\Messenger\Exception\ValidationFailedException as MessageValidationFailedException;
1718
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1819
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
1920
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
@@ -102,4 +103,28 @@ public function testNormalizeValidationFailedException()
102103
$exception = new HttpException(422, 'Validation Failed', $exception);
103104
$this->assertSame($expected, $this->normalizer->normalize(FlattenException::createFromThrowable($exception), null, ['exception' => $exception]));
104105
}
106+
107+
public function testNormalizeMessageValidationFailedException()
108+
{
109+
$this->normalizer->setSerializer(new Serializer([new ConstraintViolationListNormalizer()]));
110+
111+
$expected = [
112+
'type' => 'https://symfony.com/errors/validation',
113+
'title' => 'Validation Failed',
114+
'status' => 422,
115+
'detail' => 'Invalid value',
116+
'violations' => [
117+
[
118+
'propertyPath' => '',
119+
'title' => 'Invalid value',
120+
'template' => '',
121+
'parameters' => [],
122+
],
123+
],
124+
];
125+
126+
$exception = new MessageValidationFailedException(new \stdClass(), new ConstraintViolationList([new ConstraintViolation('Invalid value', '', [], '', null, null)]));
127+
$exception = new HttpException(422, 'Validation Failed', $exception);
128+
$this->assertSame($expected, $this->normalizer->normalize(FlattenException::createFromThrowable($exception), null, ['exception' => $exception]));
129+
}
105130
}

src/Symfony/Component/Serializer/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"symfony/form": "^5.4|^6.0|^7.0",
3434
"symfony/http-foundation": "^5.4|^6.0|^7.0",
3535
"symfony/http-kernel": "^5.4|^6.0|^7.0",
36+
"symfony/messenger": "^5.4|^6.0|^7.0",
3637
"symfony/mime": "^5.4|^6.0|^7.0",
3738
"symfony/property-access": "^5.4|^6.0|^7.0",
3839
"symfony/property-info": "^5.4.24|^6.2.11|^7.0",

0 commit comments

Comments
 (0)