bug #41491 [Serializer] Do not allow to denormalize string with spaces only to valid a DateTime object (sidz)
This PR was squashed before being merged into the 4.4 branch.
Discussion
----------
[Serializer] Do not allow to denormalize string with spaces only to valid a DateTime object
| Q | A
| ------------- | ---
| Branch? |4.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | N/A
| License | MIT
This PR fixes an issue when `$data` variable contains only spaces. In this case DateTimeNormalizer creates a DateTime object.
Commits
-------
21c0829a25
[Serializer] Do not allow to denormalize string with spaces only to valid a DateTime object
This commit is contained in:
commit
3524cf28ac
@ -97,7 +97,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
|
|||||||
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
|
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
|
||||||
$timezone = $this->getTimezone($context);
|
$timezone = $this->getTimezone($context);
|
||||||
|
|
||||||
if ('' === $data || null === $data) {
|
if (null === $data || (\is_string($data) && '' === trim($data))) {
|
||||||
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.');
|
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.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,6 +201,7 @@ class DateTimeNormalizerTest extends TestCase
|
|||||||
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
|
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
|
||||||
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
|
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
|
||||||
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
|
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
|
||||||
|
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', \DateTime::class));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDenormalizeUsingTimezonePassedInConstructor()
|
public function testDenormalizeUsingTimezonePassedInConstructor()
|
||||||
@ -290,6 +291,20 @@ class DateTimeNormalizerTest extends TestCase
|
|||||||
$this->normalizer->denormalize('', \DateTimeInterface::class);
|
$this->normalizer->denormalize('', \DateTimeInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDenormalizeStringWithSpacesOnlyThrowsAnException()
|
||||||
|
{
|
||||||
|
$this->expectException(UnexpectedValueException::class);
|
||||||
|
$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.');
|
||||||
|
$this->normalizer->denormalize(' ', \DateTimeInterface::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDenormalizeDateTimeStringWithSpacesUsingFormatPassedInContextThrowsAnException()
|
||||||
|
{
|
||||||
|
$this->expectException(UnexpectedValueException::class);
|
||||||
|
$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");
|
||||||
|
$this->normalizer->denormalize(' 2016.01.01 ', \DateTime::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|']);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDenormalizeFormatMismatchThrowsException()
|
public function testDenormalizeFormatMismatchThrowsException()
|
||||||
{
|
{
|
||||||
$this->expectException(UnexpectedValueException::class);
|
$this->expectException(UnexpectedValueException::class);
|
||||||
|
Reference in New Issue
Block a user