Skip to content

Commit

Permalink
Fix issue where exception was sometimes catched while it shouldnt
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jul 25, 2024
1 parent ec3351f commit 2b63d72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/DataPipes/CastPropertiesDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Spatie\LaravelData\Support\DataClass;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\DataProperty;
use Spatie\LaravelData\Support\Types\CombinationType;

class CastPropertiesDataPipe implements DataPipe
{
Expand Down Expand Up @@ -97,8 +98,16 @@ protected function cast(
$creationContext->previous();

return $data;
} catch (CannotCreateData) {
return $value;
} catch (CannotCreateData $exception) {
$creationContext->previous();

if ($property->type->type instanceof CombinationType) {
// Try another type in the union (which will need to be a simple type like string, int)
// In the future we should deterministically choose the correct type to cast to
return $value;
}

throw $exception;
}
}

Expand Down Expand Up @@ -208,7 +217,7 @@ protected function findCastForIterableItems(
}
}

if(in_array($property->type->iterableItemType, ['bool', 'int', 'float', 'array', 'string'])) {
if (in_array($property->type->iterableItemType, ['bool', 'int', 'float', 'array', 'string'])) {
return new BuiltinTypeCast($property->type->iterableItemType);
}

Expand Down
11 changes: 8 additions & 3 deletions tests/CreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\Validation\Min;
use Spatie\LaravelData\Attributes\WithCast;

use Spatie\LaravelData\Attributes\WithCastable;

use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
use Spatie\LaravelData\Casts\Uncastable;
use Spatie\LaravelData\Concerns\WithDeprecatedCollectionMethod;
Expand Down Expand Up @@ -51,6 +49,7 @@
use Spatie\LaravelData\Tests\Fakes\ModelData;
use Spatie\LaravelData\Tests\Fakes\Models\DummyModel;
use Spatie\LaravelData\Tests\Fakes\MultiData;
use Spatie\LaravelData\Tests\Fakes\NestedData;
use Spatie\LaravelData\Tests\Fakes\NestedLazyData;
use Spatie\LaravelData\Tests\Fakes\NestedModelCollectionData;
use Spatie\LaravelData\Tests\Fakes\NestedModelData;
Expand Down Expand Up @@ -264,7 +263,6 @@
});



it('can create a data object from a stdClass object', function () {
$object = (object) [
'string' => 'test',
Expand Down Expand Up @@ -753,6 +751,12 @@ public function __construct(
yield 'one param' => [['first' => 'First'], 'Could not create `Spatie\LaravelData\Tests\Fakes\MultiData`: the constructor requires 2 parameters, 1 given. Parameters given: first. Parameters missing: second.'],
]);

it('throws a readable exception message when the constructor of a nested data object fails', function () {
expect(fn () => NestedData::from([
'simple' => [],
]))->toThrow(CannotCreateData::class, 'Could not create `Spatie\LaravelData\Tests\Fakes\SimpleData`: the constructor requires 1 parameters, 0 given. Parameters missing: string.');
});

it('a can create a collection of data objects', function () {
$collectionA = new DataCollection(SimpleData::class, [
SimpleData::from('A'),
Expand Down Expand Up @@ -1058,6 +1062,7 @@ public function __invoke(SimpleData $data)
it('will cast iterables into the correct type', function () {
$dataClass = new class () extends Data {
public EloquentCollection $collection;

public CustomCollection $customCollection;

public array $array;
Expand Down

0 comments on commit 2b63d72

Please sign in to comment.