Skip to content

Commit

Permalink
fix: amend eager load iterator incorrectly filtering some paths
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
lindyhopchris committed Oct 13, 2024
1 parent 18559d9 commit 119ed64
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ All notable changes to this project will be documented in this file. This projec

- [#38](https://github.com/laravel-json-api/eloquent/pull/38) Added `WhereAll` and `WhereAny` filters.

### Fixed

- [#39](https://github.com/laravel-json-api/eloquent/issues/39) Fixed a bug in the eager loader iterator where include
paths starting with the same word were incorrectly removed. E.g. `car` and `carOwner` would result in just `carOwner`.

## [4.2.0] - 2024-08-26

### Added
Expand Down
11 changes: 6 additions & 5 deletions src/QueryBuilder/EagerLoading/EagerLoadIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/
class EagerLoadIterator implements IteratorAggregate
{

/**
* @var Schema
*/
Expand Down Expand Up @@ -70,11 +69,13 @@ public function __construct(Schema $schema, IncludePaths $paths)
*/
public function collect(): Collection
{
$values = collect($this);
$values = Collection::make($this);

return $values->reject(
fn($path) => $values->contains(fn($check) => $path !== $check && Str::startsWith($check, $path))
)->sort()->values();
return $values
->reject(static fn(string $path) => $values
->contains(fn(string $check) => $path !== $check && Str::startsWith($check, $path . '.')))
->sort()
->values();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/app/Models/Mechanic.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;

class Mechanic extends Model
Expand All @@ -25,6 +26,14 @@ class Mechanic extends Model
*/
protected $fillable = ['name'];

/**
* @return HasOne
*/
public function car(): HasOne
{
return $this->hasOne(Car::class);
}

/**
* @return HasOneThrough
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/app/Schemas/MechanicSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use LaravelJsonApi\Eloquent\Contracts\Paginator;
use LaravelJsonApi\Eloquent\Fields\DateTime;
use LaravelJsonApi\Eloquent\Fields\ID;
use LaravelJsonApi\Eloquent\Fields\Relations\HasOne;
use LaravelJsonApi\Eloquent\Fields\Relations\HasOneThrough;
use LaravelJsonApi\Eloquent\Fields\Str;
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
Expand All @@ -39,6 +40,7 @@ public function fields(): array
ID::make(),
DateTime::make('createdAt')->readOnly(),
Str::make('name'),
HasOne::make('car'),
HasOneThrough::make('carOwner'),
DateTime::make('updatedAt')->readOnly(),
];
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/Acceptance/EagerLoading/EagerLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public static function includePathsProvider(): array
'profile', // auto included for users
],
],
'mechanic' => [
'mechanics',
'car,carOwner,carOwner.car,carOwner.car.mechanic,car.mechanic',
['car.mechanic', 'carOwner.car.mechanic'],
],
];
}

Expand Down

0 comments on commit 119ed64

Please sign in to comment.