Skip to content

Commit 2caaddc

Browse files
bug symfony#50251 [Serializer] Handle datetime deserialization in U format (tugmaks)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Handle datetime deserialization in U format | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#50236 | License | MIT | Doc PR | - Fixes symfony#50236 Commits ------- a5d1ca6 [Serializer] Handle datetime deserialization in U format
2 parents 8048ab5 + a5d1ca6 commit 2caaddc

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ public function denormalize($data, string $type, string $format = null, array $c
9191
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
9292
$timezone = $this->getTimezone($context);
9393

94-
if (null === $data || !\is_string($data) || '' === trim($data)) {
94+
if (\is_int($data) || \is_float($data)) {
95+
switch ($dateTimeFormat) {
96+
case 'U': $data = sprintf('%d', $data); break;
97+
case 'U.u': $data = sprintf('%.6F', $data); break;
98+
}
99+
}
100+
101+
if (!\is_string($data) || '' === trim($data)) {
95102
throw NotNormalizableValueException::createForUnexpectedDataType('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
96103
}
97104

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ public function testDenormalize()
178178
$this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
179179
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
180180
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize(' 2016-01-01T00:00:00+00:00 ', \DateTime::class));
181+
$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']));
182+
$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']));
181183
}
182184

183185
public function testDenormalizeUsingTimezonePassedInConstructor()

0 commit comments

Comments
 (0)