Skip to content

Commit cc44d66

Browse files
minor #31693 [Serializer] Throw exception on invalid normalizers/encoders passed to Serializer (ogizanagi)
This PR was merged into the 5.0-dev branch. Discussion ---------- [Serializer] Throw exception on invalid normalizers/encoders passed to Serializer | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #27819 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A <!-- required for new features --> As planned in #27819 Commits ------- 5ab6ad4 [Serializer] Throw exception on invalid normalizers/encoders passed to Serializer
2 parents 9a816b5 + 5ab6ad4 commit cc44d66

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

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

4+
5.0.0
5+
-----
6+
7+
* throw an exception when creating a `Serializer` with normalizers which neither implement `NormalizerInterface` nor `DenormalizerInterface`
8+
* throw an exception when creating a `Serializer` with encoders which neither implement `EncoderInterface` nor `DecoderInterface`
9+
410
4.3.0
511
-----
612

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Serializer\Encoder\ContextAwareEncoderInterface;
1818
use Symfony\Component\Serializer\Encoder\DecoderInterface;
1919
use Symfony\Component\Serializer\Encoder\EncoderInterface;
20+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2021
use Symfony\Component\Serializer\Exception\LogicException;
2122
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
2223
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
@@ -84,8 +85,7 @@ public function __construct(array $normalizers = [], array $encoders = [])
8485
}
8586

8687
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));
88+
throw new InvalidArgumentException(\sprintf('The class "%s" neither implements "%s" nor "%s".', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class));
8989
}
9090
}
9191
$this->normalizers = $normalizers;
@@ -104,8 +104,7 @@ public function __construct(array $normalizers = [], array $encoders = [])
104104
}
105105

106106
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));
107+
throw new InvalidArgumentException(\sprintf('The class "%s" neither implements "%s" nor "%s".', \get_class($encoder), EncoderInterface::class, DecoderInterface::class));
109108
}
110109
}
111110
$this->encoder = new ChainEncoder($realEncoders);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ public function testInterface()
5656
}
5757

5858
/**
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
59+
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
60+
* @expectedExceptionMessage The class "stdClass" neither implements "Symfony\Component\Serializer\Normalizer\NormalizerInterface" nor "Symfony\Component\Serializer\Normalizer\DenormalizerInterface".
6161
*/
62-
public function testDeprecationErrorOnInvalidNormalizer()
62+
public function testItThrowsExceptionOnInvalidNormalizer()
6363
{
6464
new Serializer([new \stdClass()]);
6565
}
6666

6767
/**
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
68+
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
69+
* @expectedExceptionMessage The class "stdClass" neither implements "Symfony\Component\Serializer\Encoder\EncoderInterface" nor "Symfony\Component\Serializer\Encoder\DecoderInterface"
7070
*/
71-
public function testDeprecationErrorOnInvalidEncoder()
71+
public function testItThrowsExceptionOnInvalidEncoder()
7272
{
7373
new Serializer([], [new \stdClass()]);
7474
}

0 commit comments

Comments
 (0)