diff --git a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php index aa7988d37ed55..3a926d0ae6c5f 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php @@ -80,9 +80,6 @@ public function normalize(mixed $object, string $format = null, array $context = return $object->format($dateTimeFormat); } - /** - * @param array $context - */ public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool { return $data instanceof \DateTimeInterface; @@ -96,7 +93,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar $dateTimeFormat = $context[self::FORMAT_KEY] ?? null; $timezone = $this->getTimezone($context); - if (null === $data || !\is_string($data) || '' === trim($data)) { + if (null === $data || (!\is_string($data) && !(\is_int($data) && 'U' === $dateTimeFormat)) || '' === trim($data)) { 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); } @@ -131,9 +128,6 @@ public function denormalize(mixed $data, string $type, string $format = null, ar } } - /** - * @param array $context - */ public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool { return isset(self::SUPPORTED_TYPES[$type]); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php index 674dfaab5382d..fcd4694dd3988 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php @@ -305,4 +305,11 @@ public function testDenormalizeFormatMismatchThrowsException() $this->expectException(UnexpectedValueException::class); $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|']); } + + public function testDenormalizeDateTimeIntegerWithTimestampFormat() + { + $timestamp = time(); + $denormalizedDate = $this->normalizer->denormalize($timestamp, \DateTimeImmutable::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U']); + $this->assertSame($timestamp, $denormalizedDate->getTimestamp()); + } }