8000 inject an existing instance of PropertyAccess, implement hasCacheable… · symfony/symfony@6464ffd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6464ffd

Browse files
committed
inject an existing instance of PropertyAccess, implement hasCacheableSupportsMethod
1 parent e451fb9 commit 6464ffd

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" ?>
22

33
<container xmlns="http://symfony.com/schema/dic/services"
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
88
<parameter key="serializer.mapping.cache.file">%kernel.cache_dir%/serialization.php</parameter>
@@ -60,6 +60,8 @@
6060
</service>
6161

6262
<service id="serializer.denormalizer.unwrapping" class="Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer">
63+
<argument type="service" id="serializer.property_accessor" />
64+
<!-- Run before serializer.normalizer.object -->
6365
<tag name="serializer.normalizer" priority="1000" />
6466
</service>
6567

src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
namespace Symfony\Component\Serializer\Normalizer;
1313

1414
use Symfony\Component\PropertyAccess\PropertyAccess;
15+
use Symfony\Component\PropertyAccess\PropertyAccessor;
1516
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1617
use Symfony\Component\Serializer\SerializerAwareInterface;
1718
use Symfony\Component\Serializer\SerializerInterface;
1819

19-
class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareInterface
20+
final class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
2021
{
2122
const UNWRAP_PATH = 'unwrap_path';
2223

@@ -30,20 +31,18 @@ class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareIn
3031
*/
3132
private $propertyAccess;
3233

33-
public function __construct()
34+
public function __construct(PropertyAccessor $propertyAccess)
3435
{
35-
$this->propertyAccess = PropertyAccess::createPropertyAccessor();
36+
$this->propertyAccess = $propertyAccess;
3637
}
3738

3839
/**
3940
* {@inheritdoc}
40-
*
41-
* @throws NotNormalizableValueException
4241
*/
4342
public function denormalize($data, $class, $format = null, array $context = [])
4443
{
4544
$propertyPath = $context[self::UNWRAP_PATH];
46-
unset($context[self::UNWRAP_PATH]);
45+
$context[' 8000 unwrapped'] = true;
4746

4847
if ($propertyPath) {
4948
if (!$this->propertyAccess->isReadable($data, $propertyPath)) {
@@ -61,7 +60,7 @@ public function denormalize($data, $class, $format = null, array $context = [])
6160
*/
6261
public function supportsDenormalization($data, $type, $format = null, array $context = [])
6362
{
64-
return \array_key_exists(self::UNWRAP_PATH, $context);
63+
return \array_key_exists(self::UNWRAP_PATH, $context) && !isset($context['unwrapped']);
6564
}
6665

6766
/**
@@ -71,4 +70,12 @@ public function setSerializer(SerializerInterface $serializer)
7170
{
7271
$this->serializer = $serializer;
7372
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function hasCacheableSupportsMethod(): bool
78+
{
79+
return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod();
80+
}
7481
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\PropertyAccess\PropertyAccessor;
1617
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1718
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1819
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -493,7 +494,7 @@ public function testDeserializeAndUnwrap()
493494

494495
$expectedData = Model::fromArray(['title' => 'value', 'numbers' => [5, 3]]);
495496

496-
$serializer = new Serializer([new UnwrappingDenormalizer(), new ObjectNormalizer()], ['json' => new JsonEncoder()]);
497+
$serializer = new Serializer([new UnwrappingDenormalizer(new PropertyAccessor()), new ObjectNormalizer()], ['json' => new JsonEncoder()]);
497498

498499
$this->assertEquals(
499500
$expectedData,

0 commit comments

Comments
 (0)
0