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

[Serializer] Do not allow to denormalize string with spaces only to valid a DateTime object #41491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
$timezone = $this->getTimezone($context);

if ('' === $data || null === $data) {
if (null === $data || (\is_string($data) && '' === trim($data))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@sidz sidz Jun 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well this change a lot but exception message says:

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.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public function testDenormalize()
$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 \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()
Expand Down Expand Up @@ -290,6 +291,20 @@ public function testDenormalizeEmptyStringThrowsException()
$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()
{
$this->expectException(UnexpectedValueException::class);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.