Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add non-static JsonResource wrapping #53543

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Illuminate/Http/Resources/Json/JsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ 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 @@ -58,6 +65,8 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
public function __construct($resource)
{
$this->resource = $resource;

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

/**
Expand Down Expand Up @@ -200,6 +209,29 @@ 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: 4 additions & 2 deletions src/Illuminate/Http/Resources/Json/ResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ protected function haveAdditionalInformationAndDataIsUnwrapped($data, $with, $ad
/**
* Get the default data wrapper for the resource.
*
* @return string
* @return string|null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function would return null before this PR already in cases where ::withoutWrapping() was used

*/
protected function wrapper()
{
return get_class($this->resource)::$wrap;
return $this->resource instanceof JsonResource
? $this->resource->wrapper
: get_class($this->resource)::$wrap;
}

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