Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

CurrentUser incorrectly resolves empty User instance. #53267

Closed
anned20 opened this issue Oct 22, 2024 · 1 comment
Closed

CurrentUser incorrectly resolves empty User instance. #53267

anned20 opened this issue Oct 22, 2024 · 1 comment

Comments

@anned20
Copy link

anned20 commented Oct 22, 2024

Laravel Version

11.22.0

PHP Version

8.2.16

Database Driver & Version

No response

Description

We are using the new ContextualAttribute to get the CurrentUser in our controller methods. This works fine, until there is no user in the guard.

The function resolve in the attribute correctly returns null, but the Container then resolves the parameter to a new User instance if there is a type-hint for a User on the parameter. This even happens when the parameter type is indicated as optional.

Steps To Reproduce

class SomeController
{
    public function someMethod(
        #[CurrentUser]
        ?User $user
    ): void {
        dd($user);
    }
}

When executing this code without being logged in (having no available user), the $user will contain an empty model.

@EranNL
Copy link
Contributor

EranNL commented Oct 22, 2024

This could also potentially "break" some edge cases in the new features from this PR: #53080
When you have an optional parameter in your URL, I guess you would not want an empty model when trying to resolve it, it would be better to resolve to null in my opinion.

@laravel laravel locked and limited conversation to collaborators Oct 22, 2024
@crynobone crynobone converted this issue into discussion #53274 Oct 22, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants