From 41d1a2d25aa71f7adc8d7c2d3f5386cd912b5ceb Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 27 Nov 2024 08:50:46 -0600 Subject: [PATCH] Revert "[11.x] Add non-static JsonResource wrapping (#53543)" (#53686) This reverts commit eb625fa6aa083dbae74b4f2cc7dc6dafcce5ff07. --- .../Http/Resources/Json/JsonResource.php | 32 ------- .../Http/Resources/Json/ResourceResponse.php | 6 +- tests/Integration/Http/ResourceTest.php | 88 ------------------- 3 files changed, 2 insertions(+), 124 deletions(-) diff --git a/src/Illuminate/Http/Resources/Json/JsonResource.php b/src/Illuminate/Http/Resources/Json/JsonResource.php index 2043321f4a9d..30b9425b08fb 100644 --- a/src/Illuminate/Http/Resources/Json/JsonResource.php +++ b/src/Illuminate/Http/Resources/Json/JsonResource.php @@ -42,13 +42,6 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou */ public $additional = []; - /** - * The instance level wrapper configured for the resource. - * - * @var string|null - */ - public $wrapper = null; - /** * The "data" wrapper that should be applied. * @@ -65,8 +58,6 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou public function __construct($resource) { $this->resource = $resource; - - $this->withWrapper(static::$wrap); } /** @@ -209,29 +200,6 @@ public function withResponse(Request $request, JsonResponse $response) // } - /** - * Set the string that should wrap the outer-most resource array. - * - * @param string|null $value - * @return $this - */ - public function withWrapper(?string $value) - { - $this->wrapper = $value; - - return $this; - } - - /** - * Disable wrapping of the outer-most resource array. - * - * @return $this - */ - public function withoutWrapper() - { - return $this->withWrapper(null); - } - /** * Set the string that should wrap the outer-most resource array. * diff --git a/src/Illuminate/Http/Resources/Json/ResourceResponse.php b/src/Illuminate/Http/Resources/Json/ResourceResponse.php index 64eddefbdbb0..430e41a72950 100644 --- a/src/Illuminate/Http/Resources/Json/ResourceResponse.php +++ b/src/Illuminate/Http/Resources/Json/ResourceResponse.php @@ -102,13 +102,11 @@ protected function haveAdditionalInformationAndDataIsUnwrapped($data, $with, $ad /** * Get the default data wrapper for the resource. * - * @return string|null + * @return string */ protected function wrapper() { - return $this->resource instanceof JsonResource - ? $this->resource->wrapper - : get_class($this->resource)::$wrap; + return get_class($this->resource)::$wrap; } /** diff --git a/tests/Integration/Http/ResourceTest.php b/tests/Integration/Http/ResourceTest.php index 57eae45a1247..a0ec135b26b7 100644 --- a/tests/Integration/Http/ResourceTest.php +++ b/tests/Integration/Http/ResourceTest.php @@ -145,94 +145,6 @@ public function testResourcesMayHaveNoWrap() ]); } - public function testResourcesCanSetWithoutWrapper() - { - Route::get('/', function () { - return (new PostResource(new Post([ - 'id' => 5, - 'title' => 'Test Title', - ])))->withoutWrapper(); - }); - - $response = $this->withoutExceptionHandling()->get( - '/', ['Accept' => 'application/json'] - ); - - $response->assertJson([ - 'id' => 5, - 'title' => 'Test Title', - ]); - } - - public function testResourcesCanSetWithWrapper() - { - Route::get('/', function () { - return (new PostResourceWithoutWrap(new Post([ - 'id' => 5, - 'title' => 'Test Title', - ])))->withWrapper('postData'); - }); - - $response = $this->withoutExceptionHandling()->get( - '/', ['Accept' => 'application/json'] - ); - - $response->assertJson([ - 'postData' => [ - 'id' => 5, - 'title' => 'Test Title', - ], - ]); - } - - public function testResourceCollectionCanSetWithWrapper() - { - Route::get('/', function () { - return PostResourceWithoutWrap::collection([ - new Post([ - 'id' => 5, - 'title' => 'Test Title', - ]), - ])->withWrapper('posts'); - }); - - $response = $this->withoutExceptionHandling()->get( - '/', ['Accept' => 'application/json'] - ); - - $response->assertJson([ - 'posts' => [ - [ - 'id' => 5, - 'title' => 'Test Title', - ], - ], - ]); - } - - public function testResourceCollectionCanSetWithoutWrapper() - { - Route::get('/', function () { - return PostResource::collection([ - new Post([ - 'id' => 5, - 'title' => 'Test Title', - ]), - ])->withoutWrapper(); - }); - - $response = $this->withoutExceptionHandling()->get( - '/', ['Accept' => 'application/json'] - ); - - $response->assertJson([ - [ - 'id' => 5, - 'title' => 'Test Title', - ], - ]); - } - public function testResourcesMayHaveOptionalValues() { Route::get('/', function () {