8000 feature #27819 [Serializer] deprecated normalizers and encoders who d… · symfony/symfony@9db435e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9db435e

Browse files
committed
feature #27819 [Serializer] deprecated normalizers and encoders who dont implement the base interfaces (rodnaph)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Serializer] deprecated normalizers and encoders who dont implement the base interfaces | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | None | License | MIT | Doc PR | None Currently the `Serializer` can be constructed with any object regardless of whether or not it implements `NormalizerInterface` or `DenormalizerInterface`. This object will then be ignored when getting a normalizer/denormalizer, so in effect silently ignored for serializer operations. This change throws an exception on construct if a given normalizer object does not implement one of these interfaces - are there use cases where this would not be true? Commits ------- cbc2be8 [Serializer] deprecated normalizers and encoders who dont implement the base interfaces
2 parents 3d2124e + cbc2be8 commit 9db435e

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ CHANGELOG
1313
the format and the context in a name converter
1414
* the `AbstractNormalizer::handleCircularReference()` method will have two new `$format`
1515
and `$context` arguments in version 5.0, not defining them is deprecated
16+
* deprecated creating a `Serializer` with normalizers which do not implement
17+
either `NormalizerInterface` or `DenormalizerInterface`
18+
* deprecated creating a `Serializer` with normalizers which do not implement
19+
either `NormalizerInterface` or `DenormalizerInterface`
20+
* deprecated creating a `Serializer` with encoders which do not implement
21+
either `EncoderInterface` or `DecoderInterface`
1622

1723
4.1.0
1824
-----

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
6464
private $denormalizerCache = array();
6565
private $normalizerCache = array();
6666

67+
/**
68+
* @param (NormalizerInterface|DenormalizerInterface)[] $normalizers
69+
* @param (EncoderInterface|DecoderInterface)[] $encoders
70+
*/
6771
public function __construct(array $normalizers = array(), array $encoders = array())
6872
{
6973
foreach ($normalizers as $normalizer) {
@@ -78,6 +82,11 @@ public function __construct(array $normalizers = array(), array $encoders = arra
7882
if ($normalizer instanceof NormalizerAwareInterface) {
7983
$normalizer->setNormalizer($this);
8084
}
85+
86+
if (!($normalizer instanceof NormalizerInterface || $normalizer instanceof DenormalizerInterface)) {
87+
@trigger_error(\sprintf('Passing normalizers ("%s") which do not implement either "%s" or "%s" has been deprecated since Symfony 4.2.', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class), E_USER_DEPRECATED);
88+
// throw new \InvalidArgumentException(\sprintf('The class "%s" does not implement "%s" or "%s".', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class));
89+
}
8190
}
8291
$this->normalizers = $normalizers;
8392

@@ -93,6 +102,11 @@ public function __construct(array $normalizers = array(), array $encoders = arra
93102
if ($encoder instanceof EncoderInterface) {
94103
$realEncoders[] = $encoder;
95104
}
105+
106+
if (!($encoder instanceof EncoderInterface || $encoder instanceof DecoderInterface)) {
107+
@trigger_error(\sprintf('Passing encoders ("%s") which do not implement either "%s" or "%s" has been deprecated since Symfony 4.2.', \get_class($encoder), EncoderInterface::class, DecoderInterface::class), E_USER_DEPRECATED);
108+
// throw new \InvalidArgumentException(\sprintf('The class "%s" does not implement "%s" or "%s".', \get_class($normalizer), EncoderInterface::class, DecoderInterface::class));
109+
}
96110
}
97111
$this->encoder = new ChainEncoder($realEncoders);
98112
$this->decoder = new ChainDecoder($decoders);

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ public function testInterface()
5555
$this->assertInstanceOf('Symfony\Component\Serializer\Encoder\DecoderInterface', $serializer);
5656
}
5757

58+
/**
59+
* @expectedDeprecation Passing normalizers ("stdClass") which do not implement either "Symfony\Component\Serializer\Normalizer\NormalizerInterface" or "Symfony\Component\Serializer\Normalizer\DenormalizerInterface" has been deprecated since Symfony 4.2.
60+
* @group legacy
61+
*/
62+
public function testDeprecationErrorOnInvalidNormalizer()
63+
{
64+
new Serializer(array(new \stdClass()));
65+
}
66+
67+
/**
68+
* @expectedDeprecation Passing encoders ("stdClass") which do not implement either "Symfony\Component\Serializer\Encoder\EncoderInterface" or "Symfony\Component\Serializer\Encoder\DecoderInterface" has been deprecated since Symfony 4.2.
69+
* @group legacy
70+
*/
71+
public function testDeprecationErrorOnInvalidEncoder()
72+
{
73+
new Serializer(array(), array(new \stdClass()));
74+
}
75+
5876
/**
5977
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
6078
*/
@@ -334,7 +352,7 @@ public function testDeserializeArray()
334352

335353
public function testNormalizerAware()
336354
{
337-
$normalizerAware = $this->getMockBuilder(NormalizerAwareInterface::class)->getMock();
355+
$normalizerAware = $this->getMockBuilder(array(NormalizerAwareInterface::class, NormalizerInterface::class))->getMock();
338356
$normalizerAware->expects($this->once())
339357
->method('setNormalizer')
340358
->with($this->isInstanceOf(NormalizerInterface::class));
@@ -344,7 +362,7 @@ public function testNormalizerAware()
344362

345363
public function testDenormalizerAware()
346364
{
347-
$denormalizerAware = $this->getMockBuilder(DenormalizerAwareInterface::class)->getMock();
365+
$denormalizerAware = $this->getMockBuilder(array(DenormalizerAwareInterface::class, DenormalizerInterface::class))->getMock();
348366
$denormalizerAware->expects($this->once())
349367
->method('setDenormalizer')
350368
->with($this->isInstanceOf(DenormalizerInterface::class));

0 commit comments

Comments
 (0)
0