@@ -1176,6 +1176,35 @@ to ``true``::
11761176
11771177.. _component-serializer-handling-circular-references :
11781178
1179+ Collecting type errors while denormalizing
1180+ ------------------------------------------
1181+
1182+ When denormalizing a payload to an object with type hints, if the payload
1183+ contains a property that doesn't have the same type as the object, an exception
1184+ is thrown.
1185+
1186+ It's possible to collect all exceptions at once, and to get the object partially
1187+ denormalized::
1188+
1189+ try {
1190+ $dto = $serializer->deserialize($request->getContent(), MyDto::class, 'json', [
1191+ DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
1192+ ]);
1193+ } catch (PartialDenormalizationException $e) {
1194+ $violations = new ConstraintViolationList();
1195+ /** @var NotNormalizableValueException */
1196+ foreach ($e->getErrors() as $exception) {
1197+ $message = sprintf('The type must be one of "%s" ("%s" given).', implode(', ', $exception->getExpectedTypes()), $exception->getCurrentType());
1198+ $parameters = [];
1199+ if ($exception->canUseMessageForUser()) {
1200+ $parameters['hint'] = $exception->getMessage();
1201+ }
1202+ $violations->add(new ConstraintViolation($message, '', $parameters, null, $exception->getPath(), null));
1203+ };
1204+
1205+ return $this->json($violations, 400);
1206+ }
1207+
11791208Handling Circular References
11801209----------------------------
11811210
0 commit comments