Skip to content

Commit 8727fe7

Browse files
Merge branch '4.0'
* 4.0: [HttpKernel] DebugHandlersListener should always replace the existing exception handler fix the Composer API being used [Security] Notify that symfony/expression-language is not installed if ExpressionLanguage and ExpressionLanguagePrivider are used [Debug] Always decorate existing exception handlers to deal with fatal errors Enableable ArrayNodeDefinition is disabled for empty configuration Fixing a bug where the dump() function depended on bundle ordering [Cache] Fix handling of apcu_fetch() edgy behavior Add nn (Norwegian Nynorsk) translation files, and improve existing file Problem in phar see mergerequest #25579 [Form] Disallow transform dates beyond the year 9999 Avoid button label translation when it's set to false Copied NO language files to the new NB locale. [Serializer] DateTimeNormalizer handling of null and empty values (returning null or empty instead of new object) Fix options resolver with array allowed types [Console] Improve phpdoc on StyleInterface::ask() [TwigBridge][WIP] Pass the form-check-inline in parent
2 parents af092fd + 35ba226 commit 8727fe7

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Normalizer/DateTimeNormalizer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
7979
$dateTimeFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : null;
8080
$timezone = $this->getTimezone($context);
8181

82+
if ('' === $data || null === $data) {
83+
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.');
84+
}
85+
8286
if (null !== $dateTimeFormat) {
8387
$object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone);
8488

Tests/Normalizer/DateTimeNormalizerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ public function testDenormalizeInvalidDataThrowsException()
167167
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
168168
}
169169

170+
/**
171+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
172+
* @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.
173+
*/
174+
public function testDenormalizeNullThrowsException()
175+
{
176+
$this->normalizer->denormalize(null, \DateTimeInterface::class);
177+
}
178+
179+
/**
180+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
181+
* @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.
182+
*/
183+
public function testDenormalizeEmptyStringThrowsException()
184+
{
185+
$this->normalizer->denormalize('', \DateTimeInterface::class);
186+
}
187+
170188
/**
171189
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
172190
*/

0 commit comments

Comments
 (0)