Skip to content

Commit 4af03d0

Browse files
authored
Rewrite bad input denormalization error (#3697)
1 parent db13fcc commit 4af03d0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

features/jsonld/input_output.feature

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,16 @@ Feature: JSON-LD DTO input and output
319319
"data": 123
320320
}
321321
"""
322+
323+
@createSchema
324+
Scenario: Create a resource with a custom Input
325+
When I send a "POST" request to "/dummy_dto_customs" with body:
326+
"""
327+
{
328+
"foo": "test",
329+
"bar": "test"
330+
}
331+
"""
332+
Then the response status code should be 400
333+
And the response should be in JSON
334+
And the JSON node "hydra:description" should be equal to "The input data is misformatted."

src/Serializer/AbstractItemNormalizer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Symfony\Component\PropertyInfo\Type;
3232
use Symfony\Component\Serializer\Exception\LogicException;
3333
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
34+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
3435
use Symfony\Component\Serializer\Exception\RuntimeException;
3536
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
3637
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
@@ -190,9 +191,14 @@ public function denormalize($data, $class, $format = null, array $context = [])
190191
if (!$this->serializer instanceof DenormalizerInterface) {
191192
throw new LogicException('Cannot denormalize the input because the injected serializer is not a denormalizer');
192193
}
193-
$denormalizedInput = $this->serializer->denormalize($data, $inputClass, $format, $context);
194+
try {
195+
$denormalizedInput = $this->serializer->denormalize($data, $inputClass, $format, $context);
196+
} catch (NotNormalizableValueException $e) {
197+
throw new UnexpectedValueException('The input data is misformatted.', $e->getCode(), $e);
198+
}
199+
194200
if (!\is_object($denormalizedInput)) {
195-
throw new \UnexpectedValueException('Expected denormalized input to be an object.');
201+
throw new UnexpectedValueException('Expected denormalized input to be an object.');
196202
}
197203

198204
return $dataTransformer->transform($denormalizedInput, $resourceClass, $dataTransformerContext);

0 commit comments

Comments
 (0)