Skip to content

Commit 1fc5d75

Browse files
committed
Add COLLECT_EXTRA_ATTRIBUTES_ERRORS doc
1 parent 398f589 commit 1fc5d75

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

components/serializer.rst

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ when constructing the normalizer::
195195
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
196196
]);
197197

198+
.. tip::
199+
200+
If your object contains nested objects, an ``ExtraAttributesException`` will be thrown at first nested object containing
201+
extra attributes. When this happens, you can use :ref:`Attributes Groups section <component-serializer-collecting-extra-attributes-errors-while-denormalizing>`
202+
198203
Deserializing in an Existing Object
199204
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200205

@@ -1266,8 +1271,8 @@ Collecting Type Errors While Denormalizing
12661271
------------------------------------------
12671272

12681273
When denormalizing a payload to an object with typed properties, you'll get an
1269-
exception if the payload contains properties that don't have the same type as
1270-
the object.
1274+
exception ``NotNormalizableValueException`` if the payload contains properties that don't have the same type as
1275+
the object, at first incorrect type.
12711276

12721277
In those situations, use the ``COLLECT_DENORMALIZATION_ERRORS`` option to
12731278
collect all exceptions at once, and to get the object partially denormalized::
@@ -1291,6 +1296,34 @@ collect all exceptions at once, and to get the object partially denormalized::
12911296
return $this->json($violations, 400);
12921297
}
12931298

1299+
Collecting Extra Attributes Errors While Denormalizing
1300+
------------------------------------------
1301+
1302+
When denormalizing a payload with ``ALLOW_EXTRA_ATTRIBUTES`` set to false to an object with nested objects, you'll get an
1303+
exception at first ``ExtraAttributesException`` in nested objects.
1304+
1305+
In those situations, use the ``COLLECT_EXTRA_ATTRIBUTES_ERRORS`` option to
1306+
collect all exceptions at once, and to get the object partially denormalized::
1307+
1308+
try {
1309+
$dto = $serializer->deserialize($request->getContent(), MyDto::class, 'json', [
1310+
DenormalizerInterface::COLLECT_EXTRA_ATTRIBUTES_ERRORS => true,
1311+
]);
1312+
} catch (PartialDenormalizationException $e) {
1313+
$violations = new ConstraintViolationList();
1314+
/** @var NotNormalizableValueException $exception */
1315+
foreach ($e->getErrors() as $exception) {
1316+
$message = sprintf('The type must be one of "%s" ("%s" given).', implode(', ', $exception->getExpectedTypes()), $exception->getCurrentType());
1317+
$parameters = [];
1318+
if ($exception->canUseMessageForUser()) {
1319+
$parameters['hint'] = $exception->getMessage();
1320+
}
1321+
$violations->add(new ConstraintViolation($message, '', $parameters, null, $exception->getPath(), null));
1322+
}
1323+
1324+
return $this->json($violations, 400);
1325+
}
1326+
12941327
Handling Circular References
12951328
----------------------------
12961329

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy