Skip to content

Commit a551716

Browse files
committed
fix
1 parent 9a9a9e7 commit a551716

3 files changed

Lines changed: 39 additions & 27 deletions

File tree

src/Serializer/ItemDenormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
class ItemDenormalizer extends AbstractItemNormalizer
3434
{
3535
use ItemNormalizerTrait;
36+
private readonly LoggerInterface $logger;
3637

3738
public function __construct(
3839
PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
@@ -47,10 +48,9 @@ public function __construct(
4748
?ResourceAccessCheckerInterface $resourceAccessChecker = null,
4849
array $defaultContext = [],
4950
protected ?TagCollectorInterface $tagCollector = null,
50-
)
51-
{
52-
$this->logger = $logger ?: new NullLogger();
53-
51+
) {
5452
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker, $tagCollector);
53+
54+
$this->logger = $logger ?: new NullLogger();
5555
}
5656
}

src/Serializer/ItemNormalizer.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
namespace ApiPlatform\Serializer;
1515

16+
use ApiPlatform\Metadata\IriConverterInterface;
17+
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18+
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
19+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20+
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
21+
use ApiPlatform\Metadata\ResourceClassResolverInterface;
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;
27+
1628
/**
1729
* Generic item normalizer.
1830
*
@@ -25,4 +37,24 @@
2537
class ItemNormalizer extends AbstractItemNormalizer
2638
{
2739
use ItemNormalizerTrait;
40+
private readonly LoggerInterface $logger;
41+
42+
public function __construct(
43+
PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
44+
PropertyMetadataFactoryInterface $propertyMetadataFactory,
45+
IriConverterInterface $iriConverter,
46+
ResourceClassResolverInterface $resourceClassResolver,
47+
?PropertyAccessorInterface $propertyAccessor = null,
48+
?NameConverterInterface $nameConverter = null,
49+
?ClassMetadataFactoryInterface $classMetadataFactory = null,
50+
?LoggerInterface $logger = null,
51+
?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null,
52+
?ResourceAccessCheckerInterface $resourceAccessChecker = null,
53+
array $defaultContext = [],
54+
protected ?TagCollectorInterface $tagCollector = null,
55+
) {
56+
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker, $tagCollector);
57+
58+
$this->logger = $logger ?: new NullLogger();
59+
}
2860
}

src/Serializer/ItemNormalizerTrait.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,9 @@
1515

1616
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
1717
use ApiPlatform\Metadata\HttpOperation;
18-
use ApiPlatform\Metadata\IriConverterInterface;
1918
use ApiPlatform\Metadata\Operation;
20-
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21-
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22-
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23-
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
24-
use ApiPlatform\Metadata\ResourceClassResolverInterface;
2519
use ApiPlatform\Metadata\UrlGeneratorInterface;
26-
use Psr\Log\LoggerInterface;
27-
use Psr\Log\NullLogger;
28-
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2920
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
30-
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
31-
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
3221

3322
/**
3423
* Shared functionality for generic item normalization and denormalization.
@@ -39,15 +28,6 @@
3928
*/
4029
trait ItemNormalizerTrait
4130
{
42-
private readonly LoggerInterface $logger;
43-
44-
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, ?LoggerInterface $logger = null, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, ?ResourceAccessCheckerInterface $resourceAccessChecker = null, array $defaultContext = [], protected ?TagCollectorInterface $tagCollector = null)
45-
{
46-
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker, $tagCollector);
47-
48-
$this->logger = $logger ?: new NullLogger();
49-
}
50-
5131
/**
5232
* {@inheritdoc}
5333
*
@@ -56,7 +36,7 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
5636
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
5737
{
5838
// Avoid issues with proxies if we populated the object
59-
if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
39+
if (isset($data['id']) && !isset($context[AbstractItemNormalizer::OBJECT_TO_POPULATE])) {
6040
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
6141
throw new NotNormalizableValueException('Update is not allowed for this operation.');
6242
}
@@ -79,7 +59,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
7959
private function updateObjectToPopulate(array $data, array &$context): bool
8060
{
8161
try {
82-
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri((string) $data['id'], $context + ['fetch_data' => true]);
62+
$context[AbstractItemNormalizer::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri((string) $data['id'], $context + ['fetch_data' => true]);
8363

8464
return true;
8565
} catch (InvalidArgumentException) {
@@ -96,7 +76,7 @@ private function updateObjectToPopulate(array $data, array &$context): bool
9676
$uriVariables = $this->getContextUriVariables($data, $operation, $context);
9777
$iri = $this->iriConverter->getIriFromResource($context['resource_class'], UrlGeneratorInterface::ABS_PATH, $operation, ['uri_variables' => $uriVariables]);
9878

99-
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($iri, $context + ['fetch_data' => true]);
79+
$context[AbstractItemNormalizer::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($iri, $context + ['fetch_data' => true]);
10080
}
10181

10282
return false;

0 commit comments

Comments
 (0)