Skip to content

Commit 29ee022

Browse files
feature #49944 [Serializer] Make ProblemNormalizer give details about ValidationFailedException and PartialDenormalizationException (nicolas-grekas)
This PR was merged into the 6.3 branch. Discussion ---------- [Serializer] Make `ProblemNormalizer` give details about `ValidationFailedException` and `PartialDenormalizationException` | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR provides some infrastructure that I think is missing and would be nice using in #49138 With this patch, if an `HttpException` is thrown and if it wraps either a `ValidationFailedException` or a `PartialDenormalizationException`, Symfony will return a JSON containing the details of the validation failures / missing properties when the request comes with `Accept: application/json`. I also fixed a few minor things that I found while digging the code on this topic. Commits ------- 576f026edc [Serializer] Make `ProblemNormalizer` give details about `ValidationFailedException` and `PartialDenormalizationException`
2 parents 880df0c + 265fb94 commit 29ee022

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@
155155
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
156156
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
157157
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
158+
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
158159
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
160+
use Symfony\Component\Serializer\SerializerAwareInterface;
159161
use Symfony\Component\Stopwatch\Stopwatch;
160162
use Symfony\Component\String\LazyString;
161163
use Symfony\Component\String\Slugger\SluggerInterface;
@@ -1750,6 +1752,12 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
17501752
$container->removeDefinition('serializer.normalizer.mime_message');
17511753
}
17521754

1755+
// compat with Symfony < 6.3
1756+
if (!is_subclass_of(ProblemNormalizer::class, SerializerAwareInterface::class)) {
1757+
$container->getDefinition('serializer.normalizer.problem')
1758+
->setArguments(['%kernel.debug%']);
1759+
}
1760+
17531761
$serializerLoaders = [];
17541762
if (isset($config['enable_annotations']) && $config['enable_annotations']) {
17551763
if ($container->getParameter('kernel.debug')) {

Resources/config/serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
->tag('serializer.normalizer', ['priority' => -950])
104104

105105
->set('serializer.normalizer.problem', ProblemNormalizer::class)
106-
->args([param('kernel.debug')])
106+
->args([param('kernel.debug'), '$translator' => service('translator')->nullOnInvalid()])
107107
->tag('serializer.normalizer', ['priority' => -890])
108108

109109
->set('serializer.denormalizer.unwrapping', UnwrappingDenormalizer::class)

0 commit comments

Comments
 (0)