32
32
/**
33
33
* Generic item normalizer.
34
34
*
35
+ * TODO: do not hardcode "id"
36
+ *
35
37
* @author Kévin Dunglas <dunglas@gmail.com>
36
38
*/
37
39
class ItemNormalizer extends AbstractItemNormalizer
@@ -59,7 +61,9 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
59
61
}
60
62
61
63
if (isset ($ context ['resource_class ' ])) {
62
- $ this ->updateObjectToPopulate ($ data , $ context );
64
+ if ($ this ->updateObjectToPopulate ($ data , $ context )) {
65
+ unset($ data ['id ' ]);
66
+ }
63
67
} else {
64
68
// See https://github.com/api-platform/core/pull/2326 to understand this message.
65
69
$ 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,
68
72
}
69
73
}
70
74
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
-
82
75
return parent ::denormalize ($ data , $ class , $ format , $ context );
83
76
}
84
77
85
- private function updateObjectToPopulate (array $ data , array &$ context ): void
78
+ private function updateObjectToPopulate (array $ data , array &$ context ): bool
86
79
{
87
80
try {
88
81
$ context [self ::OBJECT_TO_POPULATE ] = $ this ->iriConverter ->getResourceFromIri ((string ) $ data ['id ' ], $ context + ['fetch_data ' => true ]);
82
+
83
+ return true ;
89
84
} catch (InvalidArgumentException ) {
90
85
$ operation = $ this ->resourceMetadataCollectionFactory ?->create($ context ['resource_class ' ])->getOperation ();
91
86
if (
@@ -102,6 +97,8 @@ private function updateObjectToPopulate(array $data, array &$context): void
102
97
103
98
$ context [self ::OBJECT_TO_POPULATE ] = $ this ->iriConverter ->getResourceFromIri ($ iri , $ context + ['fetch_data ' => true ]);
104
99
}
100
+
101
+ return false ;
105
102
}
106
103
107
104
private function getContextUriVariables (array $ data , $ operation , array $ context ): array
@@ -122,8 +119,9 @@ private function getContextUriVariables(array $data, $operation, array $context)
122
119
protected function getAllowedAttributes (string |object $ classOrObject , array $ context , bool $ attributesAsString = false ): array |bool
123
120
{
124
121
$ 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 ' ;
127
125
}
128
126
129
127
return $ allowedAttributes ;
0 commit comments