Skip to content

Commit 6bfb5a2

Browse files
author
Amrouche Hamza
committed
[Serializer] DateTimeNormalizer handling of null and empty values (returning null or empty instead of new object)
1 parent f056b4e commit 6bfb5a2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
6767
{
6868
$dateTimeFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : null;
6969

70+
if ('' === $data || null === $data) {
71+
throw new UnexpectedValueException('The data is either an empty string or null, you should pass an object implementing \DateTimeInterface.');
72+
}
73+
7074
if (null !== $dateTimeFormat) {
7175
$object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data);
7276

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

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ public function testDenormalizeInvalidDataThrowsException()
9191
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
9292
}
9393

94+
/**
95+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
96+
* @expectedExceptionMessage The data is either an empty string or null, you should pass an object implementing \DateTimeInterface.
97+
*/
98+
public function testDenormalizeNullThrowsException()
99+
{
100+
$this->normalizer->denormalize(null, \DateTimeInterface::class);
101+
}
102+
103+
/**
104+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
105+
* @expectedExceptionMessage The data is either an empty string or null, you should pass an object implementing \DateTimeInterface.
106+
*/
107+
public function testDenormalizeEmptyStringThrowsException()
108+
{
109+
$this->normalizer->denormalize('', \DateTimeInterface::class);
110+
}
111+
94112
/**
95113
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
96114
*/

0 commit comments

Comments
 (0)