Skip to content

Commit 2a5902c

Browse files
sidznicolas-grekas
authored andcommitted
[Serializer] Do not allow to denormalize string with spaces only to valid a DateTime object
1 parent 6db3eb4 commit 2a5902c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Normalizer/DateTimeNormalizer.php

Lines 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

Tests/Normalizer/DateTimeNormalizerTest.php

Lines 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)