Skip to content

Commit 55be0a1

Browse files
committed
[Serializer] Deprecate using datetime construct as fallback on default format mismatch
1 parent 7aab9a1 commit 55be0a1

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.2
55
---
66

7+
* Deprecate datetime constructor as a fallback whenever the `DateTimeNormalizer` default format mismatches
78
* Add support for constructor promoted properties to `Context` attribute
89
* Add context option `PropertyNormalizer::NORMALIZE_VISIBILITY` with bitmask flags `PropertyNormalizer::NORMALIZE_PUBLIC`, `PropertyNormalizer::NORMALIZE_PROTECTED`, `PropertyNormalizer::NORMALIZE_PRIVATE`
910
* Add method `withNormalizeVisibility` to `PropertyNormalizerContextBuilder`

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
113113
if (false !== $object) {
114114
return $object;
115115
}
116+
117+
trigger_deprecation('symfony/serializer', '6.2', 'Relying on a datetime constructor as a fallback when using a specific default date format (`datetime_format`) for the DateTimeNormalizer is deprecated. Respect the "%s" default format.', $defaultDateTimeFormat);
116118
}
117119

118120
try {

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1617
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
1718
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
@@ -21,6 +22,8 @@
2122
*/
2223
class DateTimeNormalizerTest extends TestCase
2324
{
25+
use ExpectDeprecationTrait;
26+
2427
/**
2528
* @var DateTimeNormalizer
2629
*/
@@ -177,7 +180,13 @@ public function testDenormalize()
177180
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
178181
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
179182
$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));
183+
}
184+
185+
public function testDenormalizeWithoutFormat()
186+
{
187+
$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => null]);
188+
189+
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', \DateTime::class));
181190
}
182191

183192
public function testDenormalizeUsingTimezonePassedInConstructor()
@@ -203,7 +212,9 @@ public function testDenormalizeUsingFormatPassedInContext()
203212
*/
204213
public function testDenormalizeUsingTimezonePassedInContext($input, $expected, $timezone, $format = null)
205214
{
206-
$actual = $this->normalizer->denormalize($input, \DateTimeInterface::class, null, [
215+
$normalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'Y/m/d H:i:s']);
216+
217+
$actual = $normalizer->denormalize($input, \DateTimeInterface::class, null, [
207218
DateTimeNormalizer::TIMEZONE_KEY => $timezone,
208219
DateTimeNormalizer::FORMAT_KEY => $format,
209220
]);
@@ -236,13 +247,25 @@ public function denormalizeUsingTimezonePassedInContextProvider()
236247
\DateTime::RFC3339,
237248
];
238249
}
239-
250+
/**
251+
* Deprecation will be removed as of 7.0, but this test case is still legit
252+
*
253+
* @group legacy
254+
*/
240255
public function testDenormalizeInvalidDataThrowsException()
241256
{
257+
$this->expectDeprecation('Since symfony/serializer 6.2: Relying on a datetime constructor as a fallback when using a specific default date format (`datetime_format`) for the DateTimeNormalizer is deprecated. Respect the "Y-m-d\TH:i:sP" default format.');
258+
242259
$this->expectException(UnexpectedValueException::class);
243260
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
244261
}
245262

263+
public function testDenormalizeWithFormatAndInvalidDataThrowsException()
264+
{
265+
$this->expectException(UnexpectedValueException::class);
266+
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y.m.d']);
267+
}
268+
246269
public function testDenormalizeNullThrowsException()
247270
{
248271
$this->expectException(UnexpectedValueException::class);
@@ -282,8 +305,13 @@ public function testDenormalizeDateTimeStringWithDefaultContextFormat()
282305
$this->assertSame('01/10/2018', $denormalizedDate->format($format));
283306
}
284307

308+
/**
309+
* @group legacy
310+
*/
285311
public function testDenormalizeDateTimeStringWithDefaultContextAllowsErrorFormat()
286312
{
313+
$this->expectDeprecation('Since symfony/serializer 6.2: Relying on a datetime constructor as a fallback when using a specific default date format (`datetime_format`) for the DateTimeNormalizer is deprecated. Respect the "d/m/Y" default format.');
314+
287315
$format = 'd/m/Y'; // the default format
288316
$string = '2020-01-01'; // the value which is in the wrong format, but is accepted because of `new \DateTime` in DateTimeNormalizer::denormalize
289317

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ public function testDenomalizeRecursive()
705705

706706
$obj = $serializer->denormalize([
707707
'inner' => ['foo' => 'foo', 'bar' => 'bar'],
708-
'date' => '1988/01/21',
708+
'date' => '1988-01-21T00:00:00+00:00',
709709
'inners' => [['foo' => 1], ['foo' => 2]],
710710
], ObjectOuter::class);
711711

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy