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] "Model::preventAccessingMissingAttributes()" Causes Exception During Pagination with ResourceCollection #52305

Merged
merged 2 commits into from
Jul 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function toResponse($request)
$this->resource->jsonOptions()
), function ($response) use ($request) {
$response->original = $this->resource->resource->map(function ($item) {
return is_array($item) ? Arr::get($item, 'resource') : $item->resource;
return is_array($item) ? Arr::get($item, 'resource') : optional($item)->resource;
Katalam marked this conversation as resolved.
Show resolved Hide resolved
});

$this->resource->withResponse($request, $response);
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,30 @@ public function testItWontKeysIfAnyOfThemAreStrings()
], ['data' => [0 => 10, 1 => 20, 'total' => 30]]);
}

public function testItThrowsNoErrorInStrictModeWhenResourceIsPaginated()
{
$originalMode = Model::preventsAccessingMissingAttributes();
Model::preventAccessingMissingAttributes();
try {
Route::get('/', function () {
$paginator = new LengthAwarePaginator(
collect([new Post(['id' => 5, 'title' => 'Test Title', 'reading_time' => 3.0])]),
10, 15, 1
);

return PostResourceWithJsonOptions::collection($paginator);
});

$response = $this->withoutExceptionHandling()->get(
'/', ['Accept' => 'application/json']
);

$response->assertStatus(200);
} finally {
Model::preventAccessingMissingAttributes($originalMode);
}
}

private function assertJsonResourceResponse($data, $expectedJson)
{
Route::get('/', function () use ($data) {
Expand Down