From eb84068dface3066dba746e2e5ec90a47c1d226f Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Thu, 21 Jun 2018 13:54:43 -0400 Subject: [PATCH 1/3] Document the required ClassMetadataFactory https://github.com/symfony/symfony/pull/26534 --- components/serializer.rst | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 164a29edc0b..22ca97ff734 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -170,9 +170,10 @@ needs three parameters: in Symfony 3.3. By default, additional attributes that are not mapped to the denormalized -object will be ignored by the Serializer component. Set the ``allow_extra_attributes`` -key of the deserialization context to ``false`` to let the serializer throw -an exception when additional attributes are passed:: +object will be ignored by the Serializer component. Include a +ClassMetadataFactory when constructing the normalizer and set the +``allow_extra_attributes`` key of the deserialization context to ``false`` to +let the serializer throw an exception when additional attributes are passed:: $data = << @@ -184,6 +185,9 @@ an exception when additional attributes are passed:: // this will throw a Symfony\Component\Serializer\Exception\ExtraAttributesException // because "city" is not an attribute of the Person class + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); + $normalizer = new ObjectNormalizer($classMetadataFactory); + $serializer = new Serializer(array($normalizer)); $person = $serializer->deserialize($data, 'Acme\Person', 'xml', array( 'allow_extra_attributes' => false, )); From 06cfc700a9bb5017698b61405183dd3d2e427433 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Thu, 21 Jun 2018 15:25:53 -0400 Subject: [PATCH 2/3] Codefence ClassMetadataFactory --- components/serializer.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/serializer.rst b/components/serializer.rst index 22ca97ff734..27e9aeda7a6 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -171,7 +171,7 @@ needs three parameters: By default, additional attributes that are not mapped to the denormalized object will be ignored by the Serializer component. Include a -ClassMetadataFactory when constructing the normalizer and set the +``ClassMetadataFactory`` when constructing the normalizer and set the ``allow_extra_attributes`` key of the deserialization context to ``false`` to let the serializer throw an exception when additional attributes are passed:: From 670adb1ce253fa958932bd002843bacefdc220ea Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 16 Jul 2018 20:56:19 +0200 Subject: [PATCH 3/3] Reword --- components/serializer.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 27e9aeda7a6..c4137e36a73 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -169,11 +169,11 @@ needs three parameters: Support for the ``allow_extra_attributes`` key in the context was introduced in Symfony 3.3. -By default, additional attributes that are not mapped to the denormalized -object will be ignored by the Serializer component. Include a -``ClassMetadataFactory`` when constructing the normalizer and set the -``allow_extra_attributes`` key of the deserialization context to ``false`` to -let the serializer throw an exception when additional attributes are passed:: +By default, additional attributes that are not mapped to the denormalized object +will be ignored by the Serializer component. If you prefer to throw an exception +when this happens, set the ``allow_extra_attributes`` context option to +``false`` and provide an object that implements ``ClassMetadataFactoryInterface` +when constructing the normalizer:: $data = <<