Skip to content

Commit c11dc74

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: Add the missing redirections Deleting incomplete components page [Serializer] Document `DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS`
2 parents d1f197f + eb1ffd3 commit c11dc74

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

_build/redirection_map

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@
514514
/service_container/3.3-di-changes https://symfony.com/doc/3.4/service_container/3.3-di-changes.html
515515
/frontend/encore/shared-entry /frontend/encore/split-chunks
516516
/testing/functional_tests_assertions /testing#testing-application-assertions
517+
/components https://symfony.com/components
518+
/components/index https://symfony.com/components
517519
/logging/monolog_regex_based_excludes /logging/monolog_exclude_http_codes
518520
/security/named_encoders /security/named_hashers
519521
/components/inflector /components/string#inflector

components/index.rst

Lines changed: 0 additions & 16 deletions
This file was deleted.

components/serializer.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
11671200
Handling Circular References
11681201
----------------------------
11691202

0 commit comments

Comments
 (0)