Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TaggedLocator attribute does not work in controller constructor #49082

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
lyrixx opened this issue Jan 24, 2023 · 9 comments
Closed

TaggedLocator attribute does not work in controller constructor #49082

lyrixx opened this issue Jan 24, 2023 · 9 comments

Comments

@lyrixx
Copy link
Member

lyrixx commented Jan 24, 2023

Symfony version(s) affected

6.2.* (and maybe other)

Description

A wrong Container is injected when using #[TaggedLocator('XXX')] in an argument of a controller constructor

How to reproduce

add this controller

class AController extends AbstractController
{
    public function __construct(
        #[TaggedLocator('foobar')]
        private readonly ContainerInterface $foo,
    ) {
    }

    #[Route('/', name: 'app_a')]
    public function index(
    ): JsonResponse
    {
        dd($this->foo);
        return $this->json([
            'message' => 'Welcome to your new controller!',
            'path' => 'src/Controller/AController.php',
        ]);
    }
}

Possible Solution

We can work-arround this issue with the following constructor:

     public function __construct(
         #[TaggedLocator('foobar')]
-        private readonly ContainerInterface $foo,
+        private readonly ServiceLocator $foo,
     ) {
     }

but ...

Additional Context

I guess - there is a conflict with the "ServiceSubscriberInterface"

image

@lyrixx lyrixx added the Bug label Jan 24, 2023
@lyrixx lyrixx changed the title TaggedLocator attribute does not work on constructor controller TaggedLocator attribute does not work in controller constructor Jan 24, 2023
@alexandre-daubois

This comment was marked as off-topic.

@lyrixx

This comment was marked as off-topic.

@alexandre-daubois

This comment was marked as off-topic.

@lyrixx

This comment was marked as off-topic.

@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?

@ruudk
Copy link
Contributor

ruudk commented Sep 4, 2023

Yes, it's still relevant. I experienced this problem as well, it was very weird to suddenly have other services in my locator.

@carsonbot carsonbot removed the Stalled label Sep 4, 2023
@HypeMC
Copy link
Contributor

HypeMC commented Sep 4, 2023

@ruudk Which version of Symfony are you using, cause I'm pretty sure this was fixed, see #49500.

@ruudk
Copy link
Contributor

ruudk commented Sep 4, 2023

Yes, v6.3.4

#[\Symfony\Component\HttpKernel\Attribute\AsController]
final class MyController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
{
    public function __construct(
        #[\Symfony\Component\DependencyInjection\Attribute\TaggedLocator(SomeType::class)]
        private \Symfony\Contracts\Service\ServiceProviderInterface $provider,
    ) {
        dd($this->provider->getProvidedServices());
    }

This gives:

array:10 [▼
  "form.factory" => "?"
  "http_kernel" => "?"
  "parameter_bag" => "?"
  "request_stack" => "?"
  "router" => "?"
  "security.authorization_checker" => "?"
  "security.csrf.token_manager" => "?"
  "security.token_storage" => "?"
  "serializer" => "?"
  "twig" => "?"
]

@HypeMC
Copy link
Contributor

HypeMC commented Sep 4, 2023

Oh, this happens in the constructor, sorry I've missed that part.

nicolas-grekas added a commit that referenced this issue Jan 30, 2024
…precedence over bindings (HypeMC)

This PR was merged into the 6.3 branch.

Discussion
----------

[DependencyInjection] `#[Autowire]` attribute should have precedence over bindings

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #49082
| License       | MIT
| Doc PR        | -

It seems logical (I think) that the `#[Autowire]` attribute has precedence over any bindings.

Commits
-------

c429fe5 [DependencyInjection] #[Autowire] attribute should have precedence over bindings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants