Skip to content

#[CurrentUser] : Unable to guess how to get a Doctrine instance from the request information for parameter "user". #40333

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

Closed
seb-jean opened this issue Mar 1, 2021 · 13 comments
Labels
Bug Help wanted Issues and PRs which are looking for volunteers to complete them. Security Status: Reviewed Status: Waiting feedback

Comments

@seb-jean
Copy link
Contributor

seb-jean commented Mar 1, 2021

Symfony version(s) affected: 5.2.3
sensio/framework-extra-bundle: 6.1.1

Description
Hi,
I have an error when use #[CurrentUser]:
Unable to guess how to get a Doctrine instance from the request information for parameter "user".

How to reproduce

<?php

namespace App\Controller\User;

use App\Entity\User;
use App\Repository\NotificationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;

class NotificationController extends AbstractController
{
    #[Route('/notifications', name: 'user_notification_index', methods: ['GET'])]
    public function index(#[CurrentUser] User $user): Response
    {
        ...
    }
}
@derrabus
Copy link
Member

derrabus commented Mar 1, 2021

Confirmed: The CurrentUser attribute currently does not work if your user is also a Doctrine entity and you have FrameworkExtraBundle's param converters enabled.

@derrabus derrabus added the Help wanted Issues and PRs which are looking for volunteers to complete them. label Mar 1, 2021
@seb-jean
Copy link
Contributor Author

seb-jean commented Mar 1, 2021

Confirmed: The CurrentUser attribute currently does not work if your user is also a Doctrine entity and you have FrameworkExtraBundle's param converters enabled.

My user is a Doctrine entity and I enabled param converters.

@chalasr
Copy link
Member

chalasr commented Mar 1, 2021

As a quick fix, we could make ParamConverterListener skip autoconfiguring arguments that have the CurrentUser attribute, but that would not fix the root cause.
Any userland argument value resolver targeting doctrine entities and relying on attributes is broken if you have auto_convert: true in your sensio/framework-extra-bundle configuration.

To fix this once for all, we would need to either make argument value resolvers run before param converters (I've no idea how much use cases this could break), or to fully replace param converters by argument value resolvers.
/cc @linaori @fabpot

@stof
Copy link
Member

stof commented Mar 1, 2021

to fully replace param converters by argument value resolvers.

this is the way to go. That was the plan that has never been completed.

@danfraticiu
Copy link

danfraticiu commented Apr 14, 2021

As a work around it's possible to type hint the argument as \Symfony\Component\Security\Core\User\UserInterface.

Optionally add an assert($currentUser instanceof MyUserClass) in the method body.

@derrabus
Copy link
Member

@danfraticiu In that case, you don't need the attribute. Injecting the user into a controller action as always worked by declaring UserInterface as parameter type. The whole purpose of the attribute is that I can use my actual user class as parameter type. 😃

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@seb-jean
Copy link
Contributor Author

Hi,
Yes this bug is still relevant.

@jonathantullett
Copy link

Are we anywhere closer to having this resolved? Using the functionality will improve code immensely.

Thank you.

@derrabus
Copy link
Member

This will probably be a won't fix. The solution that is being worked on is abandoning FrameworkExtraBundle, see #44705.

@jonathantullett
Copy link

Happy to see them moved to the core. With no ETA, I'll do the workaround of:

$user = $this->getUser();
assert($user instanceof MyUser);

which keeps the static analysers happy.

Thanks for responding!

nicolas-grekas added a commit that referenced this issue May 1, 2022
…nts (mbabker)

This PR was merged into the 6.1 branch.

Discussion
----------

[HttpKernel] Handle previously converted `DateTime` arguments

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix sensiolabs/SensioFrameworkExtraBundle#770
| License       | MIT
| Doc PR        | N/A

I'm not sure I like this fix, but given the order of operations between the param converters in SensioFrameworkExtraBundle and the controller argument resolvers in the HttpKernel component, this is probably going to cause the least number of headaches for users when upgrading.  And in some ways, this is the same problem as #40333 but in reverse.

Because the param converters are triggered on the `kernel.controller` event, they will handle converting values before the controller argument resolvers are fired.  This means that a typehinted `DateTimeInterface` will potentially be handled twice (once by the param converter, once by the argument resolver).  To avoid the `TypeError` noted in the issue, a practical fix is to gracefully handle a previously converted value in this resolver.  As the other options aren't great (removing services from the container or turning off the param converters in full, which has other side effects), this is probably the most practical fix.

Commits
-------

a48ecb6 Handle previously converted DateTime arguments
fabpot added a commit that referenced this issue Jul 20, 2022
…usse, nicolas-grekas)

This PR was merged into the 6.2 branch.

Discussion
----------

[DoctrineBridge] Add an Entity Argument Resolver

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | part of #40333
| License       | MIT
| Doc PR        | todo

This PR provides an Argument Resolver for Doctrine entities.

This would replace the SensioFramework's DoctrineParamConverter (in fact most of the code is copy/pasted from here) and helps users to disable the paramConverter and fix the related issue.

usage:
```yaml
sensio_framework_extra:
    request:
        converters: false
services:
    Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver: ~
```

```php
#[Route('/blog/{slug}')]
public function show(Post $post)
{
}
```

or with custom options
```php
#[Route('/blog/{id}')]
public function show(
  #[MapEntity(entityManager: 'foo', expr: 'repository.findNotDeletedById(id)')]
  Post $post
)
{
}
```

Commits
-------

5a3df5e Improve EntityValueResolver (#3)
4524083 Add an Entity Argument Resolver
@xabbuh
Copy link
Member

xabbuh commented Aug 2, 2022

Can we close here now that #44705 is finished?

@chalasr
Copy link
Member

chalasr commented Aug 2, 2022

Yes, that's not something we can tackle as a bug on stable versions. As of 6.2, one will be able to drop FrameworkExtraBundle and the issue will go away.

@chalasr chalasr closed this as completed Aug 2, 2022
jasperweyne added a commit to jasperweyne/helpless-kiwi that referenced this issue Nov 23, 2022
This reverts commit d92865d. This is
necessary per symfony/symfony#40333, since
LocalAccount is both a Doctrine entity and the CurrentUser class, which
is a won'tfix for the FrameworkExtraBundle. With Symfony 6.2, this will
be fixed by the EntityArgumentResolver
(symfony/symfony#43854). Until then, let's stick
to what we have.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Help wanted Issues and PRs which are looking for volunteers to complete them. Security Status: Reviewed Status: Waiting feedback
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants