Skip to content

Commit fae3f1d

Browse files
committed
Added some log when the $context['resource_class'] is missing in ItemNormalizer
1 parent f19c5d9 commit fae3f1d

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/Serializer/ItemNormalizer.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@
1313

1414
namespace ApiPlatform\Core\Serializer;
1515

16+
use ApiPlatform\Core\Api\IriConverterInterface;
17+
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
18+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
1619
use ApiPlatform\Core\Exception\InvalidArgumentException;
20+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22+
use Psr\Log\LoggerInterface;
23+
use Psr\Log\NullLogger;
24+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
25+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
26+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1727

1828
/**
1929
* Generic item normalizer.
@@ -24,6 +34,15 @@
2434
*/
2535
class ItemNormalizer extends AbstractItemNormalizer
2636
{
37+
private $logger;
38+
39+
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)
40+
{
41+
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $itemDataProvider, $allowPlainIdentifiers);
42+
43+
$this->logger = $logger ?: new NullLogger();
44+
}
45+
2746
/**
2847
* {@inheritdoc}
2948
*
@@ -32,12 +51,19 @@ class ItemNormalizer extends AbstractItemNormalizer
3251
public function denormalize($data, $class, $format = null, array $context = [])
3352
{
3453
// Avoid issues with proxies if we populated the object
35-
if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE]) && isset($context['resource_class'])) {
54+
if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
3655
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
3756
throw new InvalidArgumentException('Update is not allowed for this operation.');
3857
}
3958

40-
$this->updateObjectToPopulate($data, $context);
59+
if (isset($context['resource_class'])) {
60+
$this->updateObjectToPopulate($data, $context);
61+
} else {
62+
// See https://github.com/api-platform/core/pull/2326 to understand this message.
63+
$this->logger->warning('The "resource_class" key is missing from the context.', [
64+
'context' => $context,
65+
]);
66+
}
4167
}
4268

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

0 commit comments

Comments
 (0)