Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bug #25287 [Serializer] DateTimeNormalizer handling of null and empty…
… values (returning it instead of new object) (Simperfit)

This PR was merged into the 3.3 branch.

Discussion
----------

[Serializer] DateTimeNormalizer handling of null and empty values (returning it instead of new object)

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | no
| Fixed tickets | #23964
| License       | MIT
| Doc PR        |

I'm openning the disucussion on this as I think that should be returning null and not a new object.

WDYT ?

Working at home ;)
![img_2914](https://user-images.githubusercontent.com/3451634/33526107-ec2a6ce8-d83b-11e7-8949-f8d360ebb4b9.JPG)

Commits
-------

74726f3 [Serializer] DateTimeNormalizer handling of null and empty values (returning null or empty instead of new object)
  • Loading branch information
fabpot committed Jan 17, 2018
2 parents 2f8e1b8 + 74726f3 commit 65b48b5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Expand Up @@ -67,6 +67,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
{
$dateTimeFormat = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : null;

if ('' === $data || null === $data) {
throw new UnexpectedValueException('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.');
}

if (null !== $dateTimeFormat) {
$object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data);

Expand Down
Expand Up @@ -91,6 +91,24 @@ public function testDenormalizeInvalidDataThrowsException()
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
}

/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
* @expectedExceptionMessage 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.
*/
public function testDenormalizeNullThrowsException()
{
$this->normalizer->denormalize(null, \DateTimeInterface::class);
}

/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
* @expectedExceptionMessage 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.
*/
public function testDenormalizeEmptyStringThrowsException()
{
$this->normalizer->denormalize('', \DateTimeInterface::class);
}

/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
Expand Down

0 comments on commit 65b48b5

Please sign in to comment.