33
33
/**
34
34
* Generic item normalizer.
35
35
*
36
+ * TODO: do not hardcode "id"
37
+ *
36
38
* @author Kévin Dunglas <[email protected] >
37
39
*/
38
40
class ItemNormalizer extends AbstractItemNormalizer
@@ -60,7 +62,9 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
60
62
}
61
63
62
64
if (isset ($ context ['resource_class ' ])) {
63
- $ this ->updateObjectToPopulate ($ data , $ context );
65
+ if ($ this ->updateObjectToPopulate ($ data , $ context )) {
66
+ unset($ data ['id ' ]);
67
+ }
64
68
} else {
65
69
// See https://github.com/api-platform/core/pull/2326 to understand this message.
66
70
$ this ->logger ->warning ('The "resource_class" key is missing from the context. ' , [
@@ -69,24 +73,15 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
69
73
}
70
74
}
71
75
72
- // See https://github.com/api-platform/core/pull/7270 - id may be an allowed attribute due to being added in the
73
- // overridden getAllowedAttributes below, in order to allow updating a nested item via ID. But in this case it
74
- // may not "really" be an allowed attribute, ie we don't want to actually use it in denormalization. In this
75
- // scenario it will not be present in parent::getAllowedAttributes
76
- if (isset ($ data ['id ' ], $ context ['resource_class ' ])) {
77
- $ parentAllowedAttributes = parent ::getAllowedAttributes ($ class , $ context , true );
78
- if (\is_array ($ parentAllowedAttributes ) && !\in_array ('id ' , $ parentAllowedAttributes , true )) {
79
- unset($ data ['id ' ]);
80
- }
81
- }
82
-
83
76
return parent ::denormalize ($ data , $ class , $ format , $ context );
84
77
}
85
78
86
- private function updateObjectToPopulate (array $ data , array &$ context ): void
79
+ private function updateObjectToPopulate (array $ data , array &$ context ): bool
87
80
{
88
81
try {
89
82
$ context [self ::OBJECT_TO_POPULATE ] = $ this ->iriConverter ->getResourceFromIri ((string ) $ data ['id ' ], $ context + ['fetch_data ' => true ]);
83
+
84
+ return true ;
90
85
} catch (InvalidArgumentException ) {
91
86
$ operation = $ this ->resourceMetadataCollectionFactory ?->create($ context ['resource_class ' ])->getOperation ();
92
87
if (
@@ -103,6 +98,8 @@ private function updateObjectToPopulate(array $data, array &$context): void
103
98
104
99
$ context [self ::OBJECT_TO_POPULATE ] = $ this ->iriConverter ->getResourceFromIri ($ iri , $ context + ['fetch_data ' => true ]);
105
100
}
101
+
102
+ return false ;
106
103
}
107
104
108
105
private function getContextUriVariables (array $ data , Operation $ operation , array $ context ): array
@@ -125,8 +122,9 @@ private function getContextUriVariables(array $data, Operation $operation, array
125
122
protected function getAllowedAttributes (string |object $ classOrObject , array $ context , bool $ attributesAsString = false ): array |bool
126
123
{
127
124
$ allowedAttributes = parent ::getAllowedAttributes ($ classOrObject , $ context , $ attributesAsString );
128
- if (\is_array ($ allowedAttributes ) && ($ context ['api_denormalize ' ] ?? false )) {
129
- $ allowedAttributes = array_merge ($ allowedAttributes , ['id ' ]);
125
+ // id is a special case handled above it causes issues not allowing it
126
+ if (\is_array ($ allowedAttributes ) && ($ context ['api_denormalize ' ] ?? false ) && !\in_array ('id ' , $ allowedAttributes , true )) {
127
+ $ allowedAttributes [] = 'id ' ;
130
128
}
131
129
132
130
return $ allowedAttributes ;
0 commit comments