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 21c0829

Browse filesBrowse files
sidznicolas-grekas
authored andcommitted
[Serializer] Do not allow to denormalize string with spaces only to valid a DateTime object
1 parent c71e9ba commit 21c0829
Copy full SHA for 21c0829

File tree

2 files changed

+16
-1
lines changed
Filter options

2 files changed

+16
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
9797
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
9898
$timezone = $this->getTimezone($context);
9999

100-
if ('' === $data || null === $data) {
100+
if (null === $data || (\is_string($data) && '' === trim($data))) {
101101
throw new NotNormalizableValueException('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.');
102102
}
103103

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public function testDenormalize()
201201
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
202202
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
203203
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
204+
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', \DateTime::class));
204205
}
205206

206207
public function testDenormalizeUsingTimezonePassedInConstructor()
@@ -290,6 +291,20 @@ public function testDenormalizeEmptyStringThrowsException()
290291
$this->normalizer->denormalize('', \DateTimeInterface::class);
291292
}
292293

294+
public function testDenormalizeStringWithSpacesOnlyThrowsAnException()
295+
{
296+
$this->expectException(UnexpectedValueException::class);
297+
$this->expectExceptionMessage('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.');
298+
$this->normalizer->denormalize(' ', \DateTimeInterface::class);
299+
}
300+
301+
public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContextThrowsAnException()
302+
{
303+
$this->expectException(UnexpectedValueException::class);
304+
$this->expectExceptionMessage("Parsing datetime string \" 2016.01.01 \" using format \"Y.m.d|\" resulted in 2 errors: \nat position 0: Unexpected data found.\nat position 12: Trailing data");
305+
$this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']);
306+
}
307+
293308
public function testDenormalizeFormatMismatchThrowsException()
294309
{
295310
$this->expectException(UnexpectedValueException::class);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.