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 1 commit
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
26 changes: 26 additions & 0 deletions src/Illuminate/Http/Resources/Json/JsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
*/
public static $wrap = 'data';

public ?string $wrapper = null;

/**
* Create a new resource instance.
*
Expand All @@ -58,6 +60,8 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
public function __construct($resource)
{
$this->resource = $resource;

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

/**
Expand Down Expand Up @@ -211,6 +215,18 @@ public static function wrap($value)
static::$wrap = $value;
}

/**
* Set the string that should wrap the outer-most resource array.
*
* @return $this
*/
public function withWrapper(?string $value): static
{
$this->wrapper = $value;

return $this;
}

/**
* Disable wrapping of the outer-most resource array.
*
Expand All @@ -221,6 +237,16 @@ public static function withoutWrapping()
static::$wrap = null;
}

/**
* Disable wrapping of the outer-most resource array.
*
* @return $this
*/
public function withoutWrapper(): static
{
return $this->withWrapper(null);
}

/**
* Transform the resource into an HTTP response.
*
Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Http/Resources/Json/ResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ 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()
{
if ($this->resource instanceof JsonResource) {
return $this->resource->wrapper;
}

return 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