Skip to content

[bug] Moph property of morphable class always has default value #1100

@litvintroll

Description

@litvintroll

✏️ Describe the bug
Issue with PropertyMorphableData Data class
The nullable enum type field (default: null) is always null inside morph() method eventhough property was correctly resolved.
DataMorphClassResolver is not handling this case in a proper way.

↪️ To Reproduce

it('can resolve morph class with backed enum type ignoring default value', function () {
    abstract class TestAbstractMorphableDataWithNullableBackedEnum extends Data implements PropertyMorphableData
    {
        #[PropertyForMorph]
        public ?DummyBackedEnum $type = null;

        public static function morph(array $properties): ?string
        {
            return $properties['type']?->value;
        }
    };

    $morph = app(DataMorphClassResolver::class)->execute(
        app(DataConfig::class)->getDataClass(TestAbstractMorphableDataWithNullableBackedEnum::class),
        [['type' => DummyBackedEnum::FOO]]
    );

    expect($morph)->toBe(DummyBackedEnum::FOO->value);
});

Debuging \Spatie\LaravelData\Resolvers\DataMorphClassResolver::execute:
Resolved property value
Image

replaced with default 'null'
Image

✅ Expected behavior
The default value must not be applied in case the field was correctly resolved.

🖥️ Versions

Laravel: 12
Laravel Data: 4.18
PHP: 8.4.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions