Skip to content

Commit c46bac0

Browse files
committed
Do not try do retrieve object in ItemNormalizer $context['resource_class'] is not defined
We store some document in Elasticsearch and we hydrate them with the Serializer. But the `ItemNormalizer` raise an error because the `resource_class` is not defined.
1 parent 516c9c7 commit c46bac0

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ItemNormalizer extends AbstractItemNormalizer
3232
public function denormalize($data, $class, $format = null, array $context = [])
3333
{
3434
// Avoid issues with proxies if we populated the object
35-
if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
35+
if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE]) && isset($context['resource_class'])) {
3636
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
3737
throw new InvalidArgumentException('Update is not allowed for this operation.');
3838
}

tests/Serializer/ItemNormalizerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,38 @@ public function testDenormalizeWithIdAndUpdateNotAllowed()
186186
$normalizer->setSerializer($serializerProphecy->reveal());
187187
$normalizer->denormalize(['id' => '12', 'name' => 'hello'], Dummy::class, null, $context);
188188
}
189+
190+
public function testDenormalizeWithIdAndNoResourceClass()
191+
{
192+
$context = [];
193+
194+
$propertyNameCollection = new PropertyNameCollection(['id', 'name']);
195+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
196+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
197+
198+
$propertyMetadataFactory = new PropertyMetadata(null, null, true, true);
199+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
200+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();
201+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();
202+
203+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
204+
205+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
206+
207+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
208+
$serializerProphecy->willImplement(DenormalizerInterface::class);
209+
210+
$normalizer = new ItemNormalizer(
211+
$propertyNameCollectionFactoryProphecy->reveal(),
212+
$propertyMetadataFactoryProphecy->reveal(),
213+
$iriConverterProphecy->reveal(),
214+
$resourceClassResolverProphecy->reveal()
215+
);
216+
$normalizer->setSerializer($serializerProphecy->reveal());
217+
218+
$object = $normalizer->denormalize(['id' => '42', 'name' => 'hello'], Dummy::class, null, $context);
219+
$this->assertInstanceOf(Dummy::class, $object);
220+
$this->assertSame('42', $object->getId());
221+
$this->assertSame('hello', $object->getName());
222+
}
189223
}

0 commit comments

Comments
 (0)