8000 bug #25287 [Serializer] DateTimeNormalizer handling of null and empty… · sroze/symfony@65b48b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65b48b5

Browse files
committed
bug symfony#25287 [Serializer] DateTimeNormalizer handling of null and empty values (returning it instead of new object) (Simperfit)
This PR was merged into the 3.3 branch. Discussion ---------- [Serializer] DateTimeNormalizer handling of null and empty values (returning it instead of new object) | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | no | Fixed tickets | symfony#23964 | License | MIT | Doc PR | I'm openning the disucussion on this as I think that should be returning null and not a new object. WDYT ? Working at home ;) ![img_2914](https://user-images.githubusercontent.com/3451634/33526107-ec2a6ce8-d83b-11e7-8949-f8d360ebb4b9.JPG) Commits ------- 74726f3 [Serializer] DateTimeNormalizer handling of null and empty values (returning null or empty instead of new object)
2 parents 2f8e1b8 + 74726f3 commit 65b48b5

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
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 a string that can be parsed with the passed format or a valid DateTime string.');
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

Lines changed: 18 additions & 0 deletions
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 a string that can be parsed with the passed format or a valid DateTime string.
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 a string that can be parsed with the passed format or a valid DateTime string.
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)
0