Skip to content

[13.x] Remove the unused Request import from the JSON:API resource stub - #61434

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
Bosun18:remove-unused-request-import-json-api-stub
Sep 4, 2026
Merged

[13.x] Remove the unused Request import from the JSON:API resource stub#61434
taylorotwell merged 1 commit into
laravel:13.xfrom
Bosun18:remove-unused-request-import-json-api-stub

Conversation

@Bosun18

@Bosun18 Bosun18 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

make:resource --json-api generates a class that imports Illuminate\Http\Request and never uses it. The stub has no toArray(), only the $attributes and $relationships properties, so nothing in the generated file refers to Request at all. With Pint in CI that file fails no_unused_imports immediately after generation, before anyone has written a line into it — the rule is on in the default laravel preset. The framework's own style pass does not reach it, which is presumably how it survived: pint.json here turns no_unused_imports on as well, but Pint walks *.php, and all sixty-one files in that stubs directory end in .stub.

The import looks like it came along for the ride: resource.stub and resource-collection.stub next to it both declare public function toArray(Request $request): array, and the JSON:API stub was given the same header without the method that needed it. The file has not changed since #57571 added it.

The other way to fix this would be to give the stub a body instead, so the import earns its place. I don't think that is the right one here: JsonApiResource does not declare toArray() — it resolves through resolve() and reads the $attributes and $relationships properties, and the per-resource hooks it does declare are toAttributes(), toRelationships(), toId(), toType(), toLinks() and toMeta(). A generated toArray() would point people at the wrong extension point, and the properties already in the stub are the intended one. So this is a one-line removal.

There were no generator tests covering --json-api, so I added one alongside the two that were already there. It asserts the generated class extends JsonApiResource and that the Request import is not in it, which is what would have caught this in the first place.

@taylorotwell
taylorotwell merged commit 2009b31 into laravel:13.x Sep 4, 2026
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants