Skip to content

Commit c84c974

Browse files
author
Amrouche Hamza
committed
fix #857 - fix on put when both @id and id are present
1 parent 63c880f commit c84c974

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ final class ItemNormalizer extends AbstractItemNormalizer
2828
public function denormalize($data, $class, $format = null, array $context = [])
2929
{
3030
// Avoid issues with proxies if we populated the object
31-
if (isset($data['id']) && !isset($context['object_to_populate'])) {
31+
if (isset($data['@id']) && !isset($context['object_to_populate'])) {
3232
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
3333
throw new InvalidArgumentException('Update is not allowed for this operation.');
3434
}
3535

36-
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
36+
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => false]);
3737
}
3838

3939
return parent::denormalize($data, $class, $format, $context);

tests/Serializer/ItemNormalizerTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,39 @@ public function testDenormalize()
125125
$this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['name' => 'hello'], Dummy::class, null, $context));
126126
}
127127

128+
129+
public function testDenormalizeWithIri()
130+
{
131+
$context = ['resource_class' => Dummy::class, 'api_allow_update' => true];
132+
133+
$propertyNameCollection = new PropertyNameCollection(['name']);
134+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
135+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
136+
137+
$propertyMetadataFactory = new PropertyMetadata(null, null, true);
138+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
139+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();
140+
141+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
142+
$iriConverterProphecy->getItemFromIri("/dummies/12", ["resource_class" => Dummy::class, 'api_allow_update' => true, "fetch_data" => false])->shouldBeCalled();
143+
144+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
145+
146+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
147+
$serializerProphecy->willImplement(DenormalizerInterface::class);
148+
149+
$normalizer = new ItemNormalizer(
150+
$propertyNameCollectionFactoryProphecy->reveal(),
151+
$propertyMetadataFactoryProphecy->reveal(),
152+
$iriConverterProphecy->reveal(),
153+
$resourceClassResolverProphecy->reveal()
154+
);
155+
$normalizer->setSerializer($serializerProphecy->reveal());
156+
157+
$this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['@id' => '/dummies/12', 'id' => '12', 'name' => 'hello'], Dummy::class, null, $context));
158+
}
159+
160+
128161
/**
129162
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
130163
* @expectedExceptionMessage Update is not allowed for this operation.
@@ -151,6 +184,6 @@ public function testDenormalizeWithIdAndUpdateNotAllowed()
151184
$resourceClassResolverProphecy->reveal()
152185
);
153186
$normalizer->setSerializer($serializerProphecy->reveal());
154-
$normalizer->denormalize(['id' => '/dummies/12', 'name' => 'hello'], Dummy::class, null, $context);
187+
$normalizer->denormalize(['@id' => '/dummies/12', 'id' => '12', 'name' => 'hello'], Dummy::class, null, $context);
155188
}
156189
}

0 commit comments

Comments
 (0)