Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix edge case when transforming union collectible type #673

Closed
wants to merge 1 commit into from

Conversation

rasmuscnielsen
Copy link

@rasmuscnielsen rasmuscnielsen commented Feb 21, 2024

This fixes an edge case where a property may both be Collection<int, Product>|Collection<int, array>.

Steps for reproduction

use Spatie\LaravelData\Data;

class Person extends Data
{
    public function __construct(
        /** @var array<int, Pet>|array<int, array> */
        public array $pets,
    ) {
    }
}

class Pet extends Data
{
    public function __construct(
        public string $name,
    ) {
    }
}

$person = new Person(
    pets: [
        ['name' => 'Fido']
    ]
);

dd(json_encode($person));

This will produce the following error

TypeError: Spatie\LaravelData\Resolvers\TransformedDataCollectableResolver::Spatie\LaravelData\Resolvers{closure}(): Argument #1 ($data) must be of type Spatie\LaravelData\Contracts\BaseData, array given, called in vendor/spatie/laravel-data/src/Resolvers/TransformedDataCollectableResolver.php on line 72

vendor/spatie/laravel-data/src/Resolvers/TransformedDataCollectableResolver.php:108
vendor/spatie/laravel-data/src/Resolvers/TransformedDataCollectableResolver.php:72
vendor/spatie/laravel-data/src/Resolvers/TransformedDataCollectableResolver.php:49
vendor/spatie/laravel-data/src/Resolvers/TransformedDataResolver.php:164
vendor/spatie/laravel-data/src/Resolvers/TransformedDataResolver.php:66
vendor/spatie/laravel-data/src/Resolvers/TransformedDataResolver.php:37
vendor/spatie/laravel-data/src/Concerns/TransformableData.php:35
vendor/spatie/laravel-data/src/Concerns/TransformableData.php:55

Fix
Simply removing the typehint will return the original $data and fix the issue.

@rasmuscnielsen rasmuscnielsen changed the title Remove BaseData typehint in closure Fix edge case when transforming union collectible type Feb 21, 2024
@rubenvanassche
Copy link
Member

I'm not going to merge this in since we currently do not support multiple types like this. Though it seems like a simple fix, it actually removes a lot of context of these methods, there should always be a data object at this point.

In the future, when we support types like these, another path for transformation will be chosen, for now we keep it as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants