Skip to content

Commit b56bab1

Browse files
Merge branch '5.4' into 6.2
* 5.4: [Serializer] Handle datetime deserialization in U format [HttpFoundation] Fix file streaming after connection aborted Fix param type annotation [HttpClient] Fix setting duplicate-name headers when redirecting with AmpHttpClient
2 parents 2a5b899 + 12535bb commit b56bab1

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Normalizer/DateTimeNormalizer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
8383
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
8484
$timezone = $this->getTimezone($context);
8585

86-
if (null === $data || !\is_string($data) || '' === trim($data)) {
86+
if (\is_int($data) || \is_float($data)) {
87+
switch ($dateTimeFormat) {
88+
case 'U': $data = sprintf('%d', $data); break;
89+
case 'U.u': $data = sprintf('%.6F', $data); break;
90+
}
91+
}
92+
93+
if (!\is_string($data) || '' === trim($data)) {
8794
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);
8895
}
8996

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)