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 creation context issue #749

Merged
merged 6 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Laravel-data 4.0.0 was released 5 hours ago, time for an update!

- Make ValidationPath Stringable
- Fix PHPStan
- Improve performance when optional peoperty exists (#612)
- Improve performance when optional property exists (#612)

## 3.10.0 - 2023-12-01

Expand Down
12 changes: 7 additions & 5 deletions src/DataPipes/CastPropertiesDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ protected function cast(
$property->type->kind->isDataObject()
|| $property->type->kind->isDataCollectable()
) {
$context = $creationContext->next($property->type->dataClass, $property->name);

try {
return $property->type->kind->isDataObject()
? $context->from($value)
: $context->collect($value, $property->type->iterableClass);
$context = $creationContext->next($property->type->dataClass, $property->name);

$data = $property->type->kind->isDataObject()
? $context->from($value)
: $context->collect($value, $property->type->iterableClass);

$creationContext->previous();
} catch (CannotCreateData) {
Tofandel marked this conversation as resolved.
Show resolved Hide resolved
return $value;
}
Expand Down
62 changes: 62 additions & 0 deletions tests/CreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;

use Spatie\LaravelData\DataPipeline;
use Spatie\LaravelData\DataPipes\DataPipe;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataClass;
use function Pest\Laravel\postJson;

use Spatie\LaravelData\Attributes\Computed;
Expand Down Expand Up @@ -1068,6 +1072,63 @@ public function __invoke(SimpleData $data)
->toEqual(['a', 'collection']);
})->skip(fn () => config('data.features.cast_and_transform_iterables') === false);

it('keeps the creation context path up to date', function () {
global $testCreationContexts;
$testCreationContexts = [];
class TestDataPipe implements DataPipe
{
public function handle(mixed $payload, DataClass $class, array $properties, CreationContext $creationContext): array
{
global $testCreationContexts;
$testCreationContexts[] = clone $creationContext;

return $properties;
}
}
class TestNestedDataPipe implements DataPipe
{
public function handle(mixed $payload, DataClass $class, array $properties, CreationContext $creationContext): array
{
global $testCreationContexts;
$testCreationContexts[] = clone $creationContext;

return $properties;
}
}

class SimpleDataWithTestPipe extends SimpleData
{
public static function pipeline(): DataPipeline
{
return parent::pipeline()
->through(TestNestedDataPipe::class);
}
}
Tofandel marked this conversation as resolved.
Show resolved Hide resolved

$dataClass = new class () extends Data {
#[DataCollectionOf(SimpleDataWithTestPipe::class)]
public Collection $collection;

public static function pipeline(): DataPipeline
{
return parent::pipeline()
->through(TestDataPipe::class);
}
};

$dataClass::from([
'collection' => [['string' => 'no'], 'models', ['string' => 'here']],
]);

expect($testCreationContexts)->toHaveCount(3);
expect($testCreationContexts[0])->toBeInstanceOf(CreationContext::class);

expect($testCreationContexts[0]->currentPath)->toBe([0 => 'collection', 1 => 0]);
expect($testCreationContexts[1]->currentPath)->toBe([0 => 'collection', 1 => 2]);
expect($testCreationContexts[2]->currentPath)->toHaveCount(0);

});

it('is possible to create an union type data object', function () {
$dataClass = new class () extends Data {
public string|SimpleData $property;
Expand Down Expand Up @@ -1102,3 +1163,4 @@ public function __invoke(SimpleData $data)
[10, SimpleData::from('Hello World')]
);
})->todo();

26 changes: 26 additions & 0 deletions tests/PartialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,32 @@
]);
});

it('can conditionally include nested collection', function () {
$dataClass = new class () extends Data {
#[DataCollectionOf(MultiLazyData::class)]
public Collection $nested;
};

$data = $dataClass::collect([
[
'nested' => [DummyDto::rick()],
], [
'nested' => [DummyDto::bon()],
]
], DataCollection::class);

expect($data->toArray())->toMatchArray([
['nested' => [[]]],
['nested' => [[]]]
]);

expect($data->include('nested.{artist,year}')->toArray())
->toMatchArray([
['nested' => [['artist' => DummyDto::rick()->artist, 'year' => DummyDto::rick()->year]]],
['nested' => [['artist' => DummyDto::bon()->artist, 'year' => DummyDto::bon()->year]]]
]);
});

it('can conditionally include using class defaults', function () {
PartialClassConditionalData::setDefinitions(includeDefinitions: [
'string' => fn (PartialClassConditionalData $data) => $data->enabled,
Expand Down
Loading