Skip to content

Commit f027c50

Browse files
committed
fix(serializer): improve api-platform#7270 by reducing inconsistencies
1 parent 2d501b3 commit f027c50

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

src/Serializer/ItemNormalizer.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
/**
3333
* Generic item normalizer.
3434
*
35+
* TODO: do not hardcode "id"
36+
*
3537
* @author Kévin Dunglas <[email protected]>
3638
*/
3739
class ItemNormalizer extends AbstractItemNormalizer
@@ -68,17 +70,6 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
6870
}
6971
}
7072

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-
8273
return parent::denormalize($data, $class, $format, $context);
8374
}
8475

@@ -122,8 +113,9 @@ private function getContextUriVariables(array $data, $operation, array $context)
122113
protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
123114
{
124115
$allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
125-
if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false)) {
126-
$allowedAttributes = array_merge($allowedAttributes, ['id']);
116+
// id is a special case handled above it causes issues not allowing it
117+
if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false) && !in_array('id', $allowedAttributes)) {
118+
$allowedAttributes[] = 'id';
127119
}
128120

129121
return $allowedAttributes;

src/Serializer/Tests/ItemNormalizerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,14 @@ public function testDenormalizeWithWrongId(): void
316316
$operation = new Get(uriVariables: ['id' => new Link(identifiers: ['id'], parameterName: 'id')]);
317317
$obj = new Dummy();
318318

319-
$propertyNameCollection = new PropertyNameCollection(['name']);
319+
$propertyNameCollection = new PropertyNameCollection(['id', 'name']);
320320
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
321321
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
322322

323323
$propertyMetadata = (new ApiProperty())->withReadable(true)->withWritable(true);
324324
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
325325
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadata)->shouldBeCalled();
326+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id', [])->willReturn((new ApiProperty)->withIdentifier(true))->shouldBeCalled();
326327

327328
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
328329
$iriConverterProphecy->getResourceFromIri('fail', $context + ['fetch_data' => true])->willThrow(new InvalidArgumentException());

0 commit comments

Comments
 (0)