-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
When using ClassTransformer\Hydrator::create(), nullable transformable properties (like DTOs) are incorrectly instantiated even if the input value is null.
Reproduction Steps
class ProductDTO
{
public int $id;
public string $name;
}
class DiscountDTO
{
public string $code;
public int $value;
}
class PurchaseDTO
{
public ProductDTO $product;
public ?DiscountDTO $discount;
public float $cost;
}
$data = [
'product' => ['id' => 1, 'name' => 'phone'],
'discount' => null,
'cost' => 10012.23,
];
$purchaseDTO = ClassTransformer\Hydrator::init()->create(PurchaseDTO::class, $data);
var_dump($purchaseDTO->discount);
✅ Expected
The discount property should be null.
❌ Actual
An empty DiscountDTO instance is created with uninitialized properties.
Root Cause
The bug was in PropertyTypeFactory::create() where the wrong flag was passed to TransformableType. Instead of $isNullable, it mistakenly passed $isScalar, so nullability wasn’t preserved.
✅ Fix
PR: #13 – Fix nullable transformable properties handling
Metadata
Metadata
Assignees
Labels
No labels