Skip to content

[Serializer] Trigger deprecation when could not parse date with default format #58966

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

Open
wants to merge 7 commits into
base: 7.3
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions UPGRADE-7.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Serializer
----------

* Deprecate the `CompiledClassMetadataFactory` and `CompiledClassMetadataCacheWarmer` classes
* Deprecate datetime constructor as a fallback, in version 8.0 a `Symfony\Component\Serializer\Exception\NotNormalizableValueException` will be thrown when a date could not be parsed using the default format.

VarDumper
---------
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate the `CompiledClassMetadataFactory` and `CompiledClassMetadataCacheWarmer` classes
* Deprecate datetime constructor as a fallback, in version 8.0 a `Symfony\Component\Serializer\Exception\NotNormalizableValueException` will be thrown when a date could not be parsed using the default format

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
if (false !== $object = $type::createFromFormat($defaultDateTimeFormat, $data, $timezone)) {
return $object;
}

trigger_deprecation('symfony/serializer', '7.3', \sprintf('A "%s" will be thrown when a date could not be parsed using the default format "%s".', NotNormalizableValueException::class, $defaultDateTimeFormat));
Copy link
Member

Choose a reason for hiding this comment

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

I think we should trigger a deprecation here only if the fallback manages to instantiate it. The case where the fallback throws an exception (already turned into a NotNormalizableValueException below) should not trigger a deprecation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understand, in 8.0, the fallback should be removed, right?

Copy link
Member

Choose a reason for hiding this comment

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

yes. And that's my point: if the fallback also fails, you already have the same behavior than what would happen in 8.0 and we should not trigger a deprecation in that case as the behavior will not change. Try submitting foobar as value to reproduce that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have rechecked and now realise that fallback should not be removed in 8.0. Fallback should still be used when datetime_format is set to null.

}

return new $type($data, $timezone);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Tests\Normalizer;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
Expand All @@ -21,6 +22,8 @@
*/
class DateTimeNormalizerTest extends TestCase
{
use ExpectDeprecationTrait;

private DateTimeNormalizer $normalizer;

protected function setUp(): void
Expand Down Expand Up @@ -242,15 +245,25 @@ 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));
$this->assertEquals(new DateTimeImmutableChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', DateTimeImmutableChild::class));
$this->assertEquals(new DateTimeImmutableChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', DateTimeImmutableChild::class));
$this->assertEquals(new DateTimeChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', DateTimeChild::class));
$this->assertEquals(new DateTimeChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', DateTimeChild::class));
$this->assertEquals(new \DateTimeImmutable('2023-05-06T17:35:34.000000+0000', new \DateTimeZone('UTC')), $this->normalizer->denormalize(1683394534, \DateTimeImmutable::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U']));
$this->assertEquals(new \DateTimeImmutable('2023-05-06T17:35:34.123400+0000', new \DateTimeZone('UTC')), $this->normalizer->denormalize(1683394534.1234, \DateTimeImmutable::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U.u']));
}

/**
* @group legacy
*/
public function testDenormalizeAndShowDeprecation()
{
$this->expectDeprecation('Since symfony/serializer 7.3: A "Symfony\Component\Serializer\Exception\NotNormalizableValueException" will be thrown when a date could not be parsed using the default format "Y-m-d\TH:i:sP".');
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', \DateTime::class));

$this->expectDeprecation('Since symfony/serializer 7.3: A "Symfony\Component\Serializer\Exception\NotNormalizableValueException" will be thrown when a date could not be parsed using the default format "Y-m-d\TH:i:sP".');
$this->assertEquals(new DateTimeChild('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', DateTimeChild::class, null, [DateTimeNormalizer::FORMAT_KEY => null]));
}

public function testDenormalizeUsingTimezonePassedInConstructor()
{
$timezone = new \DateTimeZone('Japan');
Expand Down Expand Up @@ -284,15 +297,11 @@ public function testDenormalizeUsingTimezonePassedInContext($input, $expected, $

public static function denormalizeUsingTimezonePassedInContextProvider()
{
yield 'with timezone' => [
'2016/12/01 17:35:00',
new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('Japan')),
new \DateTimeZone('Japan'),
];
yield 'with timezone as string' => [
'2016/12/01 17:35:00',
new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('Japan')),
'Japan',
'Y/m/d H:i:s',
];
yield 'with format without timezone information' => [
'2016.12.01 17:35:00',
Expand All @@ -308,8 +317,12 @@ public static function denormalizeUsingTimezonePassedInContextProvider()
];
}

/**
* @group legacy
*/
public function testDenormalizeInvalidDataThrowsException()
{
$this->expectDeprecation('Since symfony/serializer 7.3: A "Symfony\Component\Serializer\Exception\NotNormalizableValueException" will be thrown when a date could not be parsed using the default format "Y-m-d\TH:i:sP".');
$this->expectException(UnexpectedValueException::class);
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
}
Expand Down Expand Up @@ -376,10 +389,14 @@ public function testDenormalizeDateTimeStringWithDefaultContextFormat()
$this->assertSame('01/10/2018', $denormalizedDate->format($format));
}

/**
* @group legacy
*/
public function testDenormalizeDateTimeStringWithDefaultContextAllowsErrorFormat()
{
$format = 'd/m/Y'; // the default format
$string = '2020-01-01'; // the value which is in the wrong format, but is accepted because of `new \DateTimeImmutable` in DateTimeNormalizer::denormalize
$this->expectDeprecation('Since symfony/serializer 7.3: A "Symfony\Component\Serializer\Exception\NotNormalizableValueException" will be thrown when a date could not be parsed using the default format "d/m/Y".');

$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => $format]);
$denormalizedDate = $normalizer->denormalize($string, \DateTimeInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public function testDenomalizeRecursive()
'inner' => ['foo' => 'foo', 'bar' => 'bar'],
'date' => '1988/01/21',
'inners' => [['foo' => 1], ['foo' => 2]],
], ObjectOuter::class);
], ObjectOuter::class, null, ['datetime_format' => 'Y/m/d']);

$this->assertSame('foo', $obj->getInner()->foo);
$this->assertSame('bar', $obj->getInner()->bar);
Expand Down