Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4cb67a

Browse files
deviantintegraljaviereguiluz
authored andcommitted
Document the required ClassMetadataFactory
symfony/symfony#26534
1 parent 435566d commit a4cb67a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

components/serializer.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,10 @@ needs three parameters:
170170
in Symfony 3.3.
171171

172172
By default, additional attributes that are not mapped to the denormalized
173-
object will be ignored by the Serializer component. Set the ``allow_extra_attributes``
174-
key of the deserialization context to ``false`` to let the serializer throw
175-
an exception when additional attributes are passed::
173+
object will be ignored by the Serializer component. Include a
174+
ClassMetadataFactory when constructing the normalizer and set the
175+
``allow_extra_attributes`` key of the deserialization context to ``false`` to
176+
let the serializer throw an exception when additional attributes are passed::
176177

177178
$data = <<<EOF
178179
<person>
@@ -184,6 +185,9 @@ an exception when additional attributes are passed::
184185

185186
// this will throw a Symfony\Component\Serializer\Exception\ExtraAttributesException
186187
// because "city" is not an attribute of the Person class
188+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
189+
$normalizer = new ObjectNormalizer($classMetadataFactory);
190+
$serializer = new Serializer(array($normalizer));
187191
$person = $serializer->deserialize($data, 'Acme\Person', 'xml', array(
188192
'allow_extra_attributes' => false,
189193
));

0 commit comments

Comments
 (0)