Skip to content

Commit afe4966

Browse files
committed
also reject \DateTime subclasses
1 parent cb08480 commit afe4966

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Symfony/Component/JsonStreamer/Mapping/Read/DateTimeTypePropertyMetadataLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function load(string $className, array $options = [], array $context = []
3838
$type = $metadata->getType();
3939

4040
if ($type instanceof ObjectType && is_a($type->getClassName(), \DateTimeInterface::class, true)) {
41-
if (\DateTime::class === $type->getClassName()) {
41+
if (\DateTime::class === $type->getClassName() || is_a($type->getClassName(), \DateTime::class, true)) {
4242
throw new InvalidArgumentException('The "DateTime" class is not supported. Use "DateTimeImmutable" instead.');
4343
}
4444

src/Symfony/Component/JsonStreamer/Tests/Mapping/Read/DateTimeTypePropertyMetadataLoaderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public function testThrowWhenDateTimeType()
4747
$loader->load(self::class);
4848
}
4949

50+
public function testThrowWhenDateTimeSubclassType()
51+
{
52+
$this->expectException(InvalidArgumentException::class);
53+
$this->expectExceptionMessage('The "DateTime" class is not supported. Use "DateTimeImmutable" instead.');
54+
55+
$loader = new DateTimeTypePropertyMetadataLoader(self::propertyMetadataLoader([
56+
'mutable' => new PropertyMetadata('mutable', Type::object(DateTimeChild::class)),
57+
]));
58+
59+
$loader->load(self::class);
60+
}
61+
5062
/**
5163
* @param array<string, PropertyMetadata> $propertiesMetadata
5264
*/
@@ -64,3 +76,7 @@ public function load(string $className, array $options = [], array $context = []
6476
};
6577
}
6678
}
79+
80+
class DateTimeChild extends \DateTime
81+
{
82+
}

0 commit comments

Comments
 (0)