From ef614d43f65f92bd358e9b929c6855e846cd3f77 Mon Sep 17 00:00:00 2001 From: Thomas Poirey Date: Thu, 9 Oct 2025 09:58:45 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20Fix=20deep=20selects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/Response.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 97a19b6..37eb293 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -109,7 +109,7 @@ public function modelToResponse(Model $model, Resource $resource, array $request }) ->toArray(), collect($model->getRelations()) - ->mapWithKeys(function ($modelRelation, $relationName) use ($requestArray, $relation, $resource) { + ->mapWithKeys(function ($modelRelation, $relationName) use ($currentRequestArray, $relation, $resource) { $key = Str::snake($relationName); if (is_null($modelRelation)) { @@ -132,7 +132,7 @@ public function modelToResponse(Model $model, Resource $resource, array $request $key => $this->modelToResponse( $modelRelation, $relationResource, - $requestArray, + $currentRequestArray, $relationConcrete ), ]; @@ -140,7 +140,7 @@ public function modelToResponse(Model $model, Resource $resource, array $request return [ $key => $modelRelation - ->map(fn ($collectionRelation) => $this->modelToResponse($collectionRelation, $relationResource, $requestArray, $relationConcrete)) + ->map(fn ($collectionRelation) => $this->modelToResponse($collectionRelation, $relationResource, $currentRequestArray, $relationConcrete)) ->toArray(), ]; }) From 09866d067536524260ab5b275a493b79012cd0d5 Mon Sep 17 00:00:00 2001 From: Thomas Poirey Date: Mon, 20 Oct 2025 13:47:13 +0200 Subject: [PATCH 2/5] Add test --- ...chIncludingRelationshipsOperationsTest.php | 2 +- .../SearchSelectingOperationsTest.php | 61 +++++++++++++++++++ .../Rest/Resources/HasManyResource.php | 12 +++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php index 890aadc..aa1e96d 100644 --- a/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php +++ b/tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php @@ -90,7 +90,7 @@ public function test_getting_a_list_of_resources_including_relation_with_unautho 'search' => [ 'includes' => [ [ - 'relation' => 'hasManyRelation.model', + 'relation' => 'hasManyRelation.belongsToRelation', ], [ 'relation' => 'hasManyRelation', diff --git a/tests/Feature/Controllers/SearchSelectingOperationsTest.php b/tests/Feature/Controllers/SearchSelectingOperationsTest.php index 128416d..c073a72 100644 --- a/tests/Feature/Controllers/SearchSelectingOperationsTest.php +++ b/tests/Feature/Controllers/SearchSelectingOperationsTest.php @@ -4,7 +4,9 @@ use Illuminate\Support\Facades\Gate; use Lomkit\Rest\Tests\Feature\TestCase; +use Lomkit\Rest\Tests\Support\Database\Factories\HasManyRelationFactory; use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory; +use Lomkit\Rest\Tests\Support\Models\HasManyRelation; use Lomkit\Rest\Tests\Support\Models\Model; use Lomkit\Rest\Tests\Support\Policies\GreenPolicy; use Lomkit\Rest\Tests\Support\Rest\Resources\ModelResource; @@ -89,4 +91,63 @@ public function test_getting_a_list_of_resources_selecting_two_fields(): void ['id', 'number'] ); } + + public function test_getting_a_list_of_resources_deep_selecting_fields(): void + { + $matchingModel = ModelFactory::new()->has(HasManyRelationFactory::new()->count(2), 'hasManyRelation')->create()->fresh(); + $matchingModel2 = ModelFactory::new()->create()->fresh(); + + Gate::policy(Model::class, GreenPolicy::class); + Gate::policy(HasManyRelation::class, GreenPolicy::class); + + $response = $this->post( + '/api/models/search', + [ + 'search' => [ + 'selects' => [ + ['field' => 'id'], + ], + 'includes' => [ + [ + 'relation' => 'hasManyRelation', + 'selects' => [ + ['field' => 'id'] + ], + 'includes' => [ + [ + 'relation' => 'model', + 'selects' => [ + ['field' => 'id'], + ] + ] + ] + ], + ] + ], + ], + ['Accept' => 'application/json'] + ); + + $this->assertResourcePaginated( + $response, + [$matchingModel, $matchingModel2], + new ModelResource(), + [ + [ + 'has_many_relation' => $matchingModel->hasManyRelation() + ->orderBy('id') + ->with('model:id') + ->get() + ->map(function ($relation) { + $relation->model = $relation->model->toArray(); + return $relation->only(['id', 'model']); + })->toArray(), + ], + [ + 'has_many_relation' => [], + ], + ], + ['id', 'has_many_relation'] + ); + } } diff --git a/tests/Support/Rest/Resources/HasManyResource.php b/tests/Support/Rest/Resources/HasManyResource.php index 5275ebf..d33006b 100644 --- a/tests/Support/Rest/Resources/HasManyResource.php +++ b/tests/Support/Rest/Resources/HasManyResource.php @@ -5,6 +5,7 @@ use Lomkit\Rest\Concerns\Resource\DisableGates; use Lomkit\Rest\Http\Requests\RestRequest; use Lomkit\Rest\Http\Resource; +use Lomkit\Rest\Relations\BelongsTo; use Lomkit\Rest\Tests\Support\Models\HasManyRelation; class HasManyResource extends Resource @@ -14,7 +15,9 @@ class HasManyResource extends Resource public function relations(RestRequest $request): array { - return []; + return [ + BelongsTo::make('model', ModelResource::class), + ]; } public function fields(RestRequest $request): array @@ -24,4 +27,11 @@ public function fields(RestRequest $request): array 'number', ]; } + + public function defaultOrderBy(RestRequest $request): array + { + return [ + 'id' => 'asc', + ]; + } } From de48e652c0fe5593a66a55bdb1298b35f580eeb5 Mon Sep 17 00:00:00 2001 From: Thomas Poirey Date: Mon, 20 Oct 2025 13:50:23 +0200 Subject: [PATCH 3/5] Fix CI --- .../Controllers/SearchSelectingOperationsTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Feature/Controllers/SearchSelectingOperationsTest.php b/tests/Feature/Controllers/SearchSelectingOperationsTest.php index c073a72..e904f8c 100644 --- a/tests/Feature/Controllers/SearchSelectingOperationsTest.php +++ b/tests/Feature/Controllers/SearchSelectingOperationsTest.php @@ -110,19 +110,19 @@ public function test_getting_a_list_of_resources_deep_selecting_fields(): void 'includes' => [ [ 'relation' => 'hasManyRelation', - 'selects' => [ - ['field' => 'id'] + 'selects' => [ + ['field' => 'id'], ], 'includes' => [ [ 'relation' => 'model', - 'selects' => [ + 'selects' => [ ['field' => 'id'], ] - ] - ] + ], + ], ], - ] + ], ], ], ['Accept' => 'application/json'] From ec1ebadd8bb4ca901018071f06a8a3ff4883d43a Mon Sep 17 00:00:00 2001 From: Thomas Poirey Date: Mon, 20 Oct 2025 13:51:51 +0200 Subject: [PATCH 4/5] Fix CI --- tests/Feature/Controllers/SearchSelectingOperationsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Controllers/SearchSelectingOperationsTest.php b/tests/Feature/Controllers/SearchSelectingOperationsTest.php index e904f8c..8674d57 100644 --- a/tests/Feature/Controllers/SearchSelectingOperationsTest.php +++ b/tests/Feature/Controllers/SearchSelectingOperationsTest.php @@ -118,7 +118,7 @@ public function test_getting_a_list_of_resources_deep_selecting_fields(): void 'relation' => 'model', 'selects' => [ ['field' => 'id'], - ] + ], ], ], ], From 723ab779ad0c69745892836a6f7295ed4ba68a09 Mon Sep 17 00:00:00 2001 From: Thomas Poirey Date: Mon, 20 Oct 2025 13:53:14 +0200 Subject: [PATCH 5/5] Fix CI --- tests/Feature/Controllers/SearchSelectingOperationsTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Feature/Controllers/SearchSelectingOperationsTest.php b/tests/Feature/Controllers/SearchSelectingOperationsTest.php index 8674d57..ef03a71 100644 --- a/tests/Feature/Controllers/SearchSelectingOperationsTest.php +++ b/tests/Feature/Controllers/SearchSelectingOperationsTest.php @@ -140,6 +140,7 @@ public function test_getting_a_list_of_resources_deep_selecting_fields(): void ->get() ->map(function ($relation) { $relation->model = $relation->model->toArray(); + return $relation->only(['id', 'model']); })->toArray(), ],