2323
2424use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
2525use ApiPlatform \Core \Serializer \AbstractItemNormalizer ;
26- use ApiPlatform \Core \Serializer \ItemNormalizer as GenericItemNormalizer ;
26+ use ApiPlatform \Core \Serializer \ItemNormalizer as BaseItemNormalizer ;
2727
2828/**
2929 * GraphQL normalizer.
3030 *
3131 * @author Kévin Dunglas <dunglas@gmail.com>
3232 */
33- final class ItemNormalizer extends GenericItemNormalizer
33+ final class ItemNormalizer extends BaseItemNormalizer
3434{
3535 const FORMAT = 'graphql ' ;
3636 const ITEM_KEY = '#item ' ;
3737
38+ /**
39+ * {@inheritdoc}
40+ */
41+ public function supportsNormalization ($ data , $ format = null )
42+ {
43+ return self ::FORMAT === $ format && parent ::supportsNormalization ($ data , $ format );
44+ }
45+
3846 /**
3947 * {@inheritdoc}
4048 */
@@ -58,16 +66,35 @@ protected function normalizeCollectionOfRelations(PropertyMetadata $propertyMeta
5866 /**
5967 * {@inheritdoc}
6068 */
61- public function supportsNormalization ($ data , $ format = null )
69+ public function supportsDenormalization ($ data, $ type , $ format = null )
6270 {
63- return self ::FORMAT === $ format && parent ::supportsNormalization ($ data , $ format );
71+ return self ::FORMAT === $ format && parent ::supportsDenormalization ($ data, $ type , $ format );
6472 }
6573
6674 /**
6775 * {@inheritdoc}
6876 */
69- public function supportsDenormalization ( $ data , $ type , $ format = null )
77+ protected function getAllowedAttributes ( $ classOrObject , array $ context , $ attributesAsString = false )
7078 {
71- return self ::FORMAT === $ format && parent ::supportsDenormalization ($ data , $ type , $ format );
79+ $ allowedAttributes = parent ::getAllowedAttributes ($ classOrObject , $ context , $ attributesAsString );
80+
81+ if (($ context ['api_denormalize ' ] ?? false ) && false !== ($ indexId = array_search ('id ' , $ allowedAttributes , true ))) {
82+ $ allowedAttributes [] = '_id ' ;
83+ array_splice ($ allowedAttributes , $ indexId , 1 );
84+ }
85+
86+ return $ allowedAttributes ;
87+ }
88+
89+ /**
90+ * {@inheritdoc}
91+ */
92+ protected function setAttributeValue ($ object , $ attribute , $ value , $ format = null , array $ context = [])
93+ {
94+ if ('_id ' === $ attribute ) {
95+ $ attribute = 'id ' ;
96+ }
97+
98+ parent ::setAttributeValue ($ object , $ attribute , $ value , $ format , $ context );
7299 }
73100}
0 commit comments