3232/**
3333 * Generic item normalizer.
3434 *
35+ * TODO: do not hardcode "id"
36+ *
3537 * @author Kévin Dunglas <dunglas@gmail.com>
3638 */
3739class ItemNormalizer extends AbstractItemNormalizer
@@ -59,7 +61,9 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
5961 }
6062
6163 if (isset ($ context ['resource_class ' ])) {
62- $ this ->updateObjectToPopulate ($ data , $ context );
64+ if ($ this ->updateObjectToPopulate ($ data , $ context )) {
65+ unset($ data ['id ' ]);
66+ }
6367 } else {
6468 // See https://github.com/api-platform/core/pull/2326 to understand this message.
6569 $ this ->logger ->warning ('The "resource_class" key is missing from the context. ' , [
@@ -68,24 +72,15 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
6872 }
6973 }
7074
71- // See https://github.com/api-platform/core/pull/7270 - id may be an allowed attribute due to being added in the
72- // overridden getAllowedAttributes below, in order to allow updating a nested item via ID. But in this case it
73- // may not "really" be an allowed attribute, ie we don't want to actually use it in denormalization. In this
74- // scenario it will not be present in parent::getAllowedAttributes
75- if (isset ($ data ['id ' ], $ context ['resource_class ' ])) {
76- $ parentAllowedAttributes = parent ::getAllowedAttributes ($ class , $ context , true );
77- if (\is_array ($ parentAllowedAttributes ) && !\in_array ('id ' , $ parentAllowedAttributes , true )) {
78- unset($ data ['id ' ]);
79- }
80- }
81-
8275 return parent ::denormalize ($ data , $ class , $ format , $ context );
8376 }
8477
85- private function updateObjectToPopulate (array $ data , array &$ context ): void
78+ private function updateObjectToPopulate (array $ data , array &$ context ): bool
8679 {
8780 try {
8881 $ context [self ::OBJECT_TO_POPULATE ] = $ this ->iriConverter ->getResourceFromIri ((string ) $ data ['id ' ], $ context + ['fetch_data ' => true ]);
82+
83+ return true ;
8984 } catch (InvalidArgumentException ) {
9085 $ operation = $ this ->resourceMetadataCollectionFactory ?->create($ context ['resource_class ' ])->getOperation ();
9186 if (
@@ -102,6 +97,8 @@ private function updateObjectToPopulate(array $data, array &$context): void
10297
10398 $ context [self ::OBJECT_TO_POPULATE ] = $ this ->iriConverter ->getResourceFromIri ($ iri , $ context + ['fetch_data ' => true ]);
10499 }
100+
101+ return false ;
105102 }
106103
107104 private function getContextUriVariables (array $ data , $ operation , array $ context ): array
@@ -122,8 +119,9 @@ private function getContextUriVariables(array $data, $operation, array $context)
122119 protected function getAllowedAttributes (string |object $ classOrObject , array $ context , bool $ attributesAsString = false ): array |bool
123120 {
124121 $ allowedAttributes = parent ::getAllowedAttributes ($ classOrObject , $ context , $ attributesAsString );
125- if (\is_array ($ allowedAttributes ) && ($ context ['api_denormalize ' ] ?? false )) {
126- $ allowedAttributes = array_merge ($ allowedAttributes , ['id ' ]);
122+ // id is a special case handled above it causes issues not allowing it
123+ if (\is_array ($ allowedAttributes ) && ($ context ['api_denormalize ' ] ?? false ) && !\in_array ('id ' , $ allowedAttributes , true )) {
124+ $ allowedAttributes [] = 'id ' ;
127125 }
128126
129127 return $ allowedAttributes ;
0 commit comments