Skip to content

Issue: Nullable transformable properties become empty instances #14

@ievgenii-syrotenko

Description

@ievgenii-syrotenko

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions