Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6bfb5a2

Browse filesBrowse 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
Copy full SHA for 6bfb5a2

File tree

2 files changed

+22
-0
lines changed
Filter options

2 files changed

+22
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
+4Lines 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 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php
+18Lines 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 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.