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
-------

5ab6ad409d [Serializer] Throw exception on invalid normalizers/encoders passed to Serializer
This commit is contained in:
Nicolas Grekas 2019-05-29 18:31:17 +02:00
commit cc44d66266
3 changed files with 15 additions and 10 deletions

View File

@ -1,6 +1,12 @@
CHANGELOG
=========
5.0.0
-----
* throw an exception when creating a `Serializer` with normalizers which neither implement `NormalizerInterface` nor `DenormalizerInterface`
* throw an exception when creating a `Serializer` with encoders which neither implement `EncoderInterface` nor `DecoderInterface`
4.3.0
-----

View File

@ -17,6 +17,7 @@ use Symfony\Component\Serializer\Encoder\ContextAwareDecoderInterface;
use Symfony\Component\Serializer\Encoder\ContextAwareEncoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
@ -84,8 +85,7 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
}
if (!($normalizer instanceof NormalizerInterface || $normalizer instanceof DenormalizerInterface)) {
@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);
// throw new \InvalidArgumentException(\sprintf('The class "%s" does not implement "%s" or "%s".', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class));
throw new InvalidArgumentException(\sprintf('The class "%s" neither implements "%s" nor "%s".', \get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class));
}
}
$this->normalizers = $normalizers;
@ -104,8 +104,7 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
}
if (!($encoder instanceof EncoderInterface || $encoder instanceof DecoderInterface)) {
@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);
// throw new \InvalidArgumentException(\sprintf('The class "%s" does not implement "%s" or "%s".', \get_class($normalizer), EncoderInterface::class, DecoderInterface::class));
throw new InvalidArgumentException(\sprintf('The class "%s" neither implements "%s" nor "%s".', \get_class($encoder), EncoderInterface::class, DecoderInterface::class));
}
}
$this->encoder = new ChainEncoder($realEncoders);

View File

@ -56,19 +56,19 @@ class SerializerTest extends TestCase
}
/**
* @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.
* @group legacy
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
* @expectedExceptionMessage The class "stdClass" neither implements "Symfony\Component\Serializer\Normalizer\NormalizerInterface" nor "Symfony\Component\Serializer\Normalizer\DenormalizerInterface".
*/
public function testDeprecationErrorOnInvalidNormalizer()
public function testItThrowsExceptionOnInvalidNormalizer()
{
new Serializer([new \stdClass()]);
}
/**
* @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.
* @group legacy
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
* @expectedExceptionMessage The class "stdClass" neither implements "Symfony\Component\Serializer\Encoder\EncoderInterface" nor "Symfony\Component\Serializer\Encoder\DecoderInterface"
*/
public function testDeprecationErrorOnInvalidEncoder()
public function testItThrowsExceptionOnInvalidEncoder()
{
new Serializer([], [new \stdClass()]);
}