diff --git a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php index ff121deb80..f789011329 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php @@ -67,6 +67,10 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface { $dateTimeFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : null; + if ('' === $data || null === $data) { + throw new UnexpectedValueException('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.'); + } + if (null !== $dateTimeFormat) { $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php index 6d622bbcc0..73d277b190 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php @@ -91,6 +91,24 @@ class DateTimeNormalizerTest extends TestCase $this->normalizer->denormalize('invalid date', \DateTimeInterface::class); } + /** + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException + * @expectedExceptionMessage The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string. + */ + public function testDenormalizeNullThrowsException() + { + $this->normalizer->denormalize(null, \DateTimeInterface::class); + } + + /** + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException + * @expectedExceptionMessage The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string. + */ + public function testDenormalizeEmptyStringThrowsException() + { + $this->normalizer->denormalize('', \DateTimeInterface::class); + } + /** * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException */