8000 Merge branch '4.4' into 5.2 · symfony/symfony@bb26053 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb26053

Browse files
committed
Merge branch '4.4' into 5.2
* 4.4: [Serializer] Do not allow to denormalize string with spaces only to valid a DateTime object
2 parents 5e609da + 3524cf2 commit bb26053

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function denormalize($data, string $type, string $format = null, array $c
8585
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
8686
$timezone = $this->getTimezone($context);
8787

88-
if ('' === $data || null === $data) {
88+
if (null === $data || (\is_string($data) && '' === trim($data))) {
8989
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.');
9090
}
9191

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public function testDenormalize()
177177
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
178178
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
179179
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
180+
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', \DateTime::class));
180181
}
181182

182183
public function testDenormalizeUsingTimezonePassedInConstructor()
@@ -256,6 +257,20 @@ public function testDenormalizeEmptyStringThrowsException()
256257
$this->normalizer->denormalize('', \DateTimeInterface::class);
257258
}
258259

260+
public function testDenormalizeStringWithSpacesOnlyThrowsAnException()
261+
{
262+
$this->expectException(UnexpectedValueException::class);
263+
$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.');
264+
$this->normalizer->denormalize(' ', \DateTimeInterface::class);
265+
}
266+
267+
public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContextThrowsAnException()
268+
{
269+
$this->expectException(UnexpectedValueException::class);
270+
$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");
271+
$this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']);
272+
}
273+
259274
public function testDenormalizeFormatMismatchThrowsException()
260275
{
261276
$this->expectException(UnexpectedValueException::class);

0 commit comments

Comments
 (0)
0