8000 feature #39577 [Serializer] Migrate ArrayDenormalizer to Denormalizer… · symfony/symfony@b308458 · GitHub
[go: up one dir, main page]

Skip to content

Commit b308458

Browse files
committed
feature #39577 [Serializer] Migrate ArrayDenormalizer to DenormalizerAwareInterface (derrabus)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Serializer] Migrate ArrayDenormalizer to DenormalizerAwareInterface | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | N/A | License | MIT | Doc PR | N/A `ArrayDenormalizer` currently asks for a full-blown serializer while a simple denormalizer would be enough. I'd like to change this in Symfony 6. This PR prepares the removal of `SerializerAwareInterface` from this class. Commits ------- 90f6d30 [Serializer] Migrate ArrayDenormalizer to DenormalizerAwareInterface.
2 parents 61aa8fd + 90f6d30 commit b308458

File tree

5 files changed

+86
-23
lines changed

5 files changed

+86
-23
lines changed

UPGRADE-5.3.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ Security
4242
--------
4343

4444
* Deprecated voters that do not return a valid decision when calling the `vote` method.
45+
46+
Serializer
47+
----------
48+
49+
* Deprecated `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead.

UPGRADE-6.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ Security
167167
* Removed the `AbstractRememberMeServices::$providerKey` property in favor of `AbstractRememberMeServices::$firewallName`
168168
* `AccessDecisionManager` now throw an exception when a voter does not return a valid decision.
169169

170+
Serializer
171+
----------
172+
173+
* Removed `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead.
174+
* `ArrayDenormalizer` does not implement `SerializerAwareInterface` anymore.
175+
170176
TwigBundle
171177
----------
172178

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* deprecated `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead.
8+
49
5.2.0
510
-----
611

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Serializer\Exception\BadMethodCallException;
1515
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1616
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
17+
use Symfony\Component\Serializer\Serializer;
1718
use Symfony\Component\Serializer\SerializerAwareInterface;
1819
use Symfony\Component\Serializer\SerializerInterface;
1920

@@ -24,24 +25,19 @@
2425
*
2526
* @final
2627
*/
27-
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
28+
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
2829
{
29-
/**
30-
* @var SerializerInterface|DenormalizerInterface
31-
*/
32-
private $serializer;
30+
use DenormalizerAwareTrait;
3331

3432
/**
3533
* {@inheritdoc}
3634
*
3735
* @throws NotNormalizableValueException
38-
*
39-
* @return array
4036
*/
41-
public function denormalize($data, string $type, string $format = null, array $context = [])
37+
public function denormalize($data, string $type, string $format = null, array $context = []): array
4238
{
43-
if (null === $this->serializer) {
44-
throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
39+
if (null === $this->denormalizer) {
40+
throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!');
4541
}
4642
if (!\is_array($data)) {
4743
throw new InvalidArgumentException('Data expected to be an array, '.get_debug_type($data).' given.');
@@ -50,7 +46,6 @@ public function denormalize($data, string $type, string $format = null, array $c
5046
throw new InvalidArgumentException('Unsupported class: '.$type);
5147
}
5248

53-
$serializer = $this->serializer;
5449
$type = substr($type, 0, -2);
5550

5651
$builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
@@ -59,7 +54,7 @@ public function denormalize($data, string $type, string $format = null, array $c
5954
throw new NotNormalizableValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, get_debug_type($key)));
6055
}
6156

62-
$data[$key] = $serializer->denormalize($value, $type, $format, $context);
57+
$data[$key] = $this->denormalizer->denormalize($value, $type, $format, $context);
6358
}
6459

6560
return $data;
@@ -70,31 +65,37 @@ public function denormalize($data, string $type, string $format = null, array $c
7065
*/
7166
public function supportsDenormalization($data, string $type, string $format = null, array $context = []): bool
7267
{
73-
if (null === $this->serializer) {
74-
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow "%s()" to be used.', __METHOD__));
68+
if (null === $this->denormalizer) {
69+
throw new BadMethodCallException(sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.', __METHOD__));
7570
}
7671

7772
return '[]' === substr($type, -2)
78-
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
73+
&& $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
7974
}
8075

8176
/**
8277
* {@inheritdoc}
78+
*
79+
* @deprecated call setDenormalizer() instead
8380
*/
8481
public function setSerializer(SerializerInterface $serializer)
8582
{
8683
if (!$serializer instanceof DenormalizerInterface) {
8784
throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.');
8885
}
8986

90-
$this->serializer = $serializer;
87+
if (Serializer::class !== debug_backtrace()[1]['class'] ?? null) {
88+
trigger_deprecation('symfony/serializer', '5.3', 'Calling "%s" is deprecated. Please call setDenormalizer() instead.');
89+
}
90+
91+
$this->setDenormalizer($serializer);
9192
}
9293

9394
/**
9495
* {@inheritdoc}
9596
*/
9697
public function hasCacheableSupportsMethod(): bool
9798
{
98-
return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod();
99+
return $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod();
99100
}
100101
}

src/Symfony/Component/Serializer/Tests/Normalizer/ArrayDenormalizerTest.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,30 @@
1313

1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use PHPUnit\Framework\TestCase;
16+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1617
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
17-
use Symfony\Component\Serializer\SerializerInterface;
18+
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
19+
use Symfony\Component\Serializer\Serializer;
1820

1921
class ArrayDenormalizerTest extends TestCase
2022
{
23+
use ExpectDeprecationTrait;
24+
2125
/**
2226
* @var ArrayDenormalizer
2327
*/
2428
private $denormalizer;
2529

2630
/**
27-
* @var SerializerInterface|MockObject
31+
* @var ContextAwareDenormalizerInterface|MockObject
2832
*/
2933
private $serializer;
3034

3135
protected function setUp(): void
3236
{
33-
$this->serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')->getMock();
37+
$this->serializer = $this->getMockBuilder(ContextAwareDenormalizerInterface::class)->getMock();
3438
$this->denormalizer = new ArrayDenormalizer();
35-
$this->denormalizer->setSerializer($this->serializer);
39+
$this->denormalizer->setDenormalizer($this->serializer);
3640
}
3741

3842
public function testDenormalize()
@@ -65,11 +69,51 @@ public function testDenormalize()
6569
);
6670
}
6771

72+
/**
73+
* @group legacy
74+
*/
75+
public function testDenormalizeLegacy()
76+
{
77+
$serializer = $this->getMockBuilder(Serializer::class)->getMock();
78+
79+
$serializer->expects($this->exactly(2))
80+
->method('denormalize')
81+
->withConsecutive(
82+
[['foo' => 'one', 'bar' => 'two']],
83+
[['foo' => 'three', 'bar' => 'four']]
84+
)
85+
->willReturnOnConsecutiveCalls(
86+
new ArrayDummy('one', 'two'),
87+
new ArrayDummy('three', 'four')
88+
);
89+
90+
$denormalizer = new ArrayDenormalizer();
91+
92+
$this->expectDeprecation('Since symfony/serializer 5.3: Calling "%s" is deprecated. Please call setDenormalizer() instead.');
93+
$denormalizer->setSerializer($serializer);
94+
95+
$result = $denormalizer->denormalize(
96+
[
97+
['foo' => 'one', 'bar' => 'two'],
98+
['foo' => 'three', 'bar' => 'four'],
99+
],
100+
__NAMESPACE__.'\ArrayDummy[]'
101+
);
102+
103+
$this->assertEquals(
104+
[
105+
new ArrayDummy('one', 'two'),
106+
new ArrayDummy('three', 'four'),
107+
],
108+
$result
109+
);
110+
}
111+
68112
public function testSupportsValidArray()
69113
{
70114
$this->serializer->expects($this->once())
71115
->method('supportsDenormalization')
72-
->with($this->anything(), ArrayDummy::class, $this->anything())
116+
->with($this->anything(), ArrayDummy::class, 'json', ['con' => 'text'])
73117
->willReturn(true);
74118

75119
$this->assertTrue(
@@ -78,7 +122,9 @@ public function testSupportsValidArray()
78122
['foo' => 'one', 'bar' => 'two'],
79123
['foo' => 'three', 'bar' => 'four'],
80124
],
81-
__NAMESPACE__.'\ArrayDummy[]'
125+
__NAMESPACE__.'\ArrayDummy[]',
126+
'json',
127+
['con' => 'text']
82128
)
83129
);
84130
}

0 commit comments

Comments
 (0)
0