Skip to content

Commit

Permalink
Merge branch '2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Dec 20, 2018
2 parents 3305fc5 + e51024c commit 9a5b6f0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Serializer/ItemNormalizer.php
Expand Up @@ -13,7 +13,17 @@

namespace ApiPlatform\Core\Serializer;

use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
* Generic item normalizer.
Expand All @@ -24,6 +34,15 @@
*/
class ItemNormalizer extends AbstractItemNormalizer
{
private $logger;

public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, ItemDataProviderInterface $itemDataProvider = null, bool $allowPlainIdentifiers = false, LoggerInterface $logger = null)
{
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $itemDataProvider, $allowPlainIdentifiers);

$this->logger = $logger ?: new NullLogger();
}

/**
* {@inheritdoc}
*
Expand All @@ -37,7 +56,14 @@ public function denormalize($data, $class, $format = null, array $context = [])
throw new InvalidArgumentException('Update is not allowed for this operation.');
}

$this->updateObjectToPopulate($data, $context);
if (isset($context['resource_class'])) {
$this->updateObjectToPopulate($data, $context);
} else {
// See https://github.com/api-platform/core/pull/2326 to understand this message.
$this->logger->warning('The "resource_class" key is missing from the context.', [
'context' => $context,
]);
}
}

return parent::denormalize($data, $class, $format, $context);
Expand Down
34 changes: 34 additions & 0 deletions tests/Serializer/ItemNormalizerTest.php
Expand Up @@ -186,4 +186,38 @@ public function testDenormalizeWithIdAndUpdateNotAllowed()
$normalizer->setSerializer($serializerProphecy->reveal());
$normalizer->denormalize(['id' => '12', 'name' => 'hello'], Dummy::class, null, $context);
}

public function testDenormalizeWithIdAndNoResourceClass()
{
$context = [];

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

$propertyMetadataFactory = new PropertyMetadata(null, null, true, true);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(DenormalizerInterface::class);

$normalizer = new ItemNormalizer(
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$iriConverterProphecy->reveal(),
$resourceClassResolverProphecy->reveal()
);
$normalizer->setSerializer($serializerProphecy->reveal());

$object = $normalizer->denormalize(['id' => '42', 'name' => 'hello'], Dummy::class, null, $context);
$this->assertInstanceOf(Dummy::class, $object);
$this->assertSame('42', $object->getId());
$this->assertSame('hello', $object->getName());
}
}

0 comments on commit 9a5b6f0

Please sign in to comment.