From f927708a20a02b404476067d4beef355721c9167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Robles?= Date: Thu, 6 Oct 2022 15:11:38 +0200 Subject: [PATCH] fixes and tests for JsonApiResponse::toArray method --- src/Http/JsonApiResponse.php | 23 +++++++- src/Http/RequestQueryObject.php | 10 ++++ .../Resources/Json/ResourceCollection.php | 12 +---- tests/Http/JsonApiResponseTest.php | 54 +++++++++++++++++++ 4 files changed, 87 insertions(+), 12 deletions(-) diff --git a/src/Http/JsonApiResponse.php b/src/Http/JsonApiResponse.php index b1ea50e..ab6615e 100644 --- a/src/Http/JsonApiResponse.php +++ b/src/Http/JsonApiResponse.php @@ -11,6 +11,7 @@ use Illuminate\Support\Traits\ForwardsCalls; use OpenSoutheners\LaravelApiable\Contracts\ViewableBuilder; use OpenSoutheners\LaravelApiable\Contracts\ViewQueryable; +use OpenSoutheners\LaravelApiable\Http\Resources\JsonApiCollection; use OpenSoutheners\LaravelApiable\Support\Facades\Apiable; use function OpenSoutheners\LaravelHelpers\Classes\class_implement; @@ -172,6 +173,10 @@ protected function getResults() */ public function toResponse($request) { + if ($request->hasMacro('inertia') && $request->inertia()) { + return $this->toArray($request); + } + return $this->getResults()->toResponse($request); } @@ -182,7 +187,23 @@ public function toResponse($request) */ public function toArray() { - return (array) $this->getResults(); + $results = $this->getResults(); + + if (! ($results instanceof JsonApiCollection)) { + return $results->toArray($this->getRequest()); + } + + $responseArray = ['data' => $results->collection->map->toArray($this->getRequest())]; + + foreach (array_filter($results->with) as $key => $value) { + $responseArray[$key] = $value; + } + + if (! empty($results->additional)) { + $responseArray = array_merge_recursive($responseArray, $results->additional); + } + + return $responseArray; } /** diff --git a/src/Http/RequestQueryObject.php b/src/Http/RequestQueryObject.php index db994cf..ff8dec9 100644 --- a/src/Http/RequestQueryObject.php +++ b/src/Http/RequestQueryObject.php @@ -47,6 +47,16 @@ public function setQuery($query) return $this; } + /** + * Get the underlying request object. + * + * @return Request|null + */ + public function getRequest() + { + return $this->request; + } + /** * Allows the following user operations. * diff --git a/src/Http/Resources/Json/ResourceCollection.php b/src/Http/Resources/Json/ResourceCollection.php index c7d1cc3..ce01351 100644 --- a/src/Http/Resources/Json/ResourceCollection.php +++ b/src/Http/Resources/Json/ResourceCollection.php @@ -102,17 +102,7 @@ public function count(): int */ public function toArray($request) { - $responseArray = ['data' => $this->collection->map->toArray($request)]; - - foreach (array_filter($this->with) as $key => $value) { - $responseArray[$key] = $value; - } - - if (! empty($this->additional)) { - $responseArray = array_merge($responseArray, array_filter($this->additional)); - } - - return $responseArray; + return $this->collection->map->toArray($request); } /** diff --git a/tests/Http/JsonApiResponseTest.php b/tests/Http/JsonApiResponseTest.php index e17c764..bc6ccaf 100644 --- a/tests/Http/JsonApiResponseTest.php +++ b/tests/Http/JsonApiResponseTest.php @@ -2,6 +2,7 @@ namespace OpenSoutheners\LaravelApiable\Tests\Http; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use OpenSoutheners\LaravelApiable\Http\AllowedAppends; use OpenSoutheners\LaravelApiable\Http\AllowedFields; @@ -294,4 +295,57 @@ public function testListPerformingFulltextSearch() $assert->hasSize(1)->hasAttribute('title', 'Y esto en espaƱol'); }); } + + public function testResponseAsArrayGetsAllContent() + { + // Yeah, we need to enforce this macro to "fake" Inertia so force toArray response behaviour + Request::macro('inertia', fn () => true); + + config(['apiable.responses.include_allowed' => true]); + + Route::get('/', function () { + return response()->json(JsonApiResponse::from(Post::with('tags'))->allowing([ + AllowedFilter::exact('status', ['Active', 'Archived']), + ])); + }); + + $response = $this->getJson('/'); + + $response->assertJsonCount(4, 'data'); + $response->assertJsonCount(1, 'meta.allowed_filters'); + $response->assertJsonFragment([ + 'allowed_filters' => [ + 'status' => [ + 'operator' => '=', + 'values' => ['Active', 'Archived'], + ], + ], + ]); + $response->assertJsonFragment([ + 'id' => '1', + 'type' => 'post', + 'relationships' => [ + 'tags' => [ + 'data' => [ + [ + 'id' => '1', + 'type' => 'label' + ], + [ + 'id' => '3', + 'type' => 'label' + ], + [ + 'id' => '4', + 'type' => 'label' + ] + ] + ] + ] + ]); + $response->assertJsonFragment([ + 'id' => '1', + 'type' => 'post', + ]); + } }