@@ -1164,6 +1164,39 @@ to ``true``::
11641164
11651165.. _component-serializer-handling-circular-references :
11661166
1167+ Collecting Type Errors While Denormalizing
1168+ ------------------------------------------
1169+
1170+ When denormalizing a payload to an object with typed properties, you'll get an
1171+ exception if the payload contains properties that don't have the same type as
1172+ the object.
1173+
1174+ In those situations, use the ``COLLECT_DENORMALIZATION_ERRORS `` option to
1175+ collect all exceptions at once, and to get the object partially denormalized::
1176+
1177+ try {
1178+ $dto = $serializer->deserialize($request->getContent(), MyDto::class, 'json', [
1179+ DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
1180+ ]);
1181+ } catch (PartialDenormalizationException $e) {
1182+ $violations = new ConstraintViolationList();
1183+ /** @var NotNormalizableValueException */
1184+ foreach ($e->getErrors() as $exception) {
1185+ $message = sprintf('The type must be one of "%s" ("%s" given).', implode(', ', $exception->getExpectedTypes()), $exception->getCurrentType());
1186+ $parameters = [];
1187+ if ($exception->canUseMessageForUser()) {
1188+ $parameters['hint'] = $exception->getMessage();
1189+ }
1190+ $violations->add(new ConstraintViolation($message, '', $parameters, null, $exception->getPath(), null));
1191+ };
1192+
1193+ return $this->json($violations, 400);
1194+ }
1195+
1196+ .. versionadded :: 5.4
1197+
1198+ The ``COLLECT_DENORMALIZATION_ERRORS `` option was introduced in Symfony 5.4.
1199+
11671200Handling Circular References
11681201----------------------------
11691202
0 commit comments