Skip to content

Conversation

@mengidd
Copy link
Contributor

@mengidd mengidd commented Sep 25, 2025

When using the ResponseFromApiResource attribute, scribe will crash if you overwrite the API resources __construct method to add proper type. Example:

class UserResource extends JsonResource
{
    public function __construct(User $resource) // Passing in User here will crash scribe
    {
        parent::__construct($resource);
    }

    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
        ];
    }
}

This will give the following error

TypeError App\Http\Resources\UserResource::__construct(): Argument #1 ($resource) must be of type App\Models\User, Illuminate\Http\Resources\MissingValue given, called in /var/www/vendor/knuckleswtf/scribe/src/Attributes/ResponseFromApiResource.php on line 48

This PR fixes this issue, as scribe no longer instantiates a new resource class instance with MissingValue as first parameter to check if it's an instance of ResourceCollection.

Will check if the given resource is a collection or not without instantiating a new instance of the given class with MissingValue as first parameter.

Allows for overwriting the resources __contruct() to ensure type safety in resources, example:
```
class UserResource extends JsonResource
{
    public function __construct(User $resource)
    {
        parent::__construct($resource);
    }

    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
        ];
    }
}
```
@mengidd mengidd changed the title ResponseFromApiResource better isCollection check fix: crash when API resource __construct enforces type in ResponseFromApiResource Sep 25, 2025
@shalvah
Copy link
Contributor

shalvah commented Oct 20, 2025

Awesome, thank you 👏 👏

@shalvah shalvah merged commit 71f61a8 into knuckleswtf:v5 Oct 20, 2025
6 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