From 0d6cda354b3f4358cdadc4d81800ee94a5241760 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 23 May 2025 10:51:09 +0200 Subject: [PATCH] also reject \DateTime subclasses --- .../Read/DateTimeTypePropertyMetadataLoader.php | 2 +- .../DateTimeTypePropertyMetadataLoaderTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/JsonStreamer/Mapping/Read/DateTimeTypePropertyMetadataLoader.php b/src/Symfony/Component/JsonStreamer/Mapping/Read/DateTimeTypePropertyMetadataLoader.php index 11ce2b4f93962..26bc022cae2e3 100644 --- a/src/Symfony/Component/JsonStreamer/Mapping/Read/DateTimeTypePropertyMetadataLoader.php +++ b/src/Symfony/Component/JsonStreamer/Mapping/Read/DateTimeTypePropertyMetadataLoader.php @@ -38,7 +38,7 @@ public function load(string $className, array $options = [], array $context = [] $type = $metadata->getType(); if ($type instanceof ObjectType && is_a($type->getClassName(), \DateTimeInterface::class, true)) { - if (\DateTime::class === $type->getClassName()) { + if (is_a($type->getClassName(), \DateTime::class, true)) { throw new InvalidArgumentException('The "DateTime" class is not supported. Use "DateTimeImmutable" instead.'); } diff --git a/src/Symfony/Component/JsonStreamer/Tests/Mapping/Read/DateTimeTypePropertyMetadataLoaderTest.php b/src/Symfony/Component/JsonStreamer/Tests/Mapping/Read/DateTimeTypePropertyMetadataLoaderTest.php index c71189815be29..779499adf21c2 100644 --- a/src/Symfony/Component/JsonStreamer/Tests/Mapping/Read/DateTimeTypePropertyMetadataLoaderTest.php +++ b/src/Symfony/Component/JsonStreamer/Tests/Mapping/Read/DateTimeTypePropertyMetadataLoaderTest.php @@ -47,6 +47,18 @@ public function testThrowWhenDateTimeType() $loader->load(self::class); } + public function testThrowWhenDateTimeSubclassType() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The "DateTime" class is not supported. Use "DateTimeImmutable" instead.'); + + $loader = new DateTimeTypePropertyMetadataLoader(self::propertyMetadataLoader([ + 'mutable' => new PropertyMetadata('mutable', Type::object(DateTimeChild::class)), + ])); + + $loader->load(self::class); + } + /** * @param array $propertiesMetadata */ @@ -64,3 +76,7 @@ public function load(string $className, array $options = [], array $context = [] }; } } + +class DateTimeChild extends \DateTime +{ +}