Skip to content

Commit 1813a5f

Browse files
authored
Merge pull request #20 from Rafikooo/fix/serializer-propagation
Fix serializer not propagated to decorated normalizer
2 parents 01515d4 + 162edc5 commit 1813a5f

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
decorates="api_platform.serializer.normalizer.item"
149149
parent="api_platform.serializer.normalizer.item"
150150
>
151-
<argument type="service" id=".inner" />
151+
<argument key="$decorated" type="service" id=".inner" />
152152
</service>
153153

154154
<service id="sylius_import_export.twig.component.import_resource" class="Sylius\ImportExport\Twig\Component\ImportResourceFormComponent">

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
bootstrap="vendor/sylius/test-application/config/bootstrap.php"
88
>
99
<testsuites>
10+
<testsuite name="unit">
11+
<directory>tests/Unit</directory>
12+
</testsuite>
1013
<testsuite name="functional">
1114
<directory>tests/Functional</directory>
1215
</testsuite>

src/Serializer/ExportAwareItemNormalizer.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2727
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2828
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
29+
use Symfony\Component\Serializer\SerializerAwareInterface;
30+
use Symfony\Component\Serializer\SerializerInterface;
2931

3032
/**
3133
* @see ItemNormalizer
@@ -49,7 +51,7 @@ public function __construct(
4951
?ResourceAccessCheckerInterface $resourceAccessChecker = null,
5052
array $defaultContext = [],
5153
?TagCollectorInterface $tagCollector = null,
52-
private AbstractItemNormalizer $decorated,
54+
private ?AbstractItemNormalizer $decorated = null,
5355
) {
5456
parent::__construct(
5557
$propertyNameCollectionFactory,
@@ -93,4 +95,13 @@ public function supportsDenormalization(
9395

9496
return $this->decorated->supportsDenormalization($data, $type, $format, $context);
9597
}
98+
99+
public function setSerializer(SerializerInterface $serializer): void
100+
{
101+
parent::setSerializer($serializer);
102+
103+
if ($this->decorated instanceof SerializerAwareInterface) {
104+
$this->decorated->setSerializer($serializer);
105+
}
106+
}
96107
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\ImportExport\Tests\Unit\Serializer;
15+
16+
use ApiPlatform\Metadata\IriConverterInterface;
17+
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18+
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
19+
use ApiPlatform\Metadata\ResourceClassResolverInterface;
20+
use ApiPlatform\Serializer\AbstractItemNormalizer;
21+
use PHPUnit\Framework\TestCase;
22+
use Sylius\ImportExport\Serializer\ExportAwareItemNormalizer;
23+
use Symfony\Component\Serializer\SerializerInterface;
24+
25+
final class ExportAwareItemNormalizerTest extends TestCase
26+
{
27+
public function testSetSerializerPropagatesToDecoratedService(): void
28+
{
29+
$decorated = $this->createMock(AbstractItemNormalizer::class);
30+
$serializer = $this->createMock(SerializerInterface::class);
31+
32+
$decorated->expects($this->once())
33+
->method('setSerializer')
34+
->with($serializer);
35+
36+
$normalizer = new ExportAwareItemNormalizer(
37+
propertyNameCollectionFactory: $this->createMock(PropertyNameCollectionFactoryInterface::class),
38+
propertyMetadataFactory: $this->createMock(PropertyMetadataFactoryInterface::class),
39+
iriConverter: $this->createMock(IriConverterInterface::class),
40+
resourceClassResolver: $this->createMock(ResourceClassResolverInterface::class),
41+
decorated: $decorated,
42+
);
43+
44+
$normalizer->setSerializer($serializer);
45+
}
46+
}

0 commit comments

Comments
 (0)