Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3ff28ab

Browse files
committed
Add COLLECT_EXTRA_ATTRIBUTES_ERRORS doc
1 parent398f589 commit3ff28ab

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

‎components/serializer.rst

Lines changed: 31 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,30 @@ 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+
if (null !== $extraAttributesException = $e->getExtraAttributesError()) {
1315+
foreach ($extraAttributesException->getExtraAttributes() as $extraAttribute) {
1316+
$violations->add(new ConstraintViolation('This attribute is not allowed.', '', [], null, $extraAttribute, null));
1317+
};
1318+
}
1319+
1320+
return $this->json($violations, 400);
1321+
}
1322+
12941323
Handling Circular References
12951324
----------------------------
12961325

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp