From 1c5b478736287cf4419ef19937a2faee2270876c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 13 Sep 2021 18:39:33 +0200 Subject: [PATCH] [Serializer] Document `DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS` --- components/serializer.rst | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index 9c8b73a04a1..07e083b4374 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1176,6 +1176,35 @@ to ``true``:: .. _component-serializer-handling-circular-references: +Collecting type errors while denormalizing +------------------------------------------ + +When denormalizing a payload to an object with type hints, if the payload +contains a property that doesn't have the same type as the object, an exception +is thrown. + +It's possible to collect all exceptions at once, and to get the object partially +denormalized:: + + try { + $dto = $serializer->deserialize($request->getContent(), MyDto::class, 'json', [ + DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true, + ]); + } catch (PartialDenormalizationException $e) { + $violations = new ConstraintViolationList(); + /** @var NotNormalizableValueException */ + foreach ($e->getErrors() as $exception) { + $message = sprintf('The type must be one of "%s" ("%s" given).', implode(', ', $exception->getExpectedTypes()), $exception->getCurrentType()); + $parameters = []; + if ($exception->canUseMessageForUser()) { + $parameters['hint'] = $exception->getMessage(); + } + $violations->add(new ConstraintViolation($message, '', $parameters, null, $exception->getPath(), null)); + }; + + return $this->json($violations, 400); + } + Handling Circular References ----------------------------