Skip to content

Commit

Permalink
Revert "[11.x] Add non-static JsonResource wrapping (#53543)" (#53686)
Browse files Browse the repository at this point in the history
This reverts commit eb625fa.
  • Loading branch information
taylorotwell authored Nov 27, 2024
1 parent e7b12ff commit 41d1a2d
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 124 deletions.
32 changes: 0 additions & 32 deletions src/Illuminate/Http/Resources/Json/JsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -65,8 +58,6 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
public function __construct($resource)
{
$this->resource = $resource;

$this->withWrapper(static::$wrap);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
6 changes: 2 additions & 4 deletions src/Illuminate/Http/Resources/Json/ResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
88 changes: 0 additions & 88 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 41d1a2d

Please sign in to comment.