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

Skip to content

[DependencyInjection] Target Attribute must fail if the target does not exist #48555

Closed
@lyrixx

Description

@lyrixx

Description

see: https://symfony.com/blog/new-in-symfony-5-3-service-autowiring-with-attributes#selecting-autowire-alias-with-attributes

If I create an http client:

    http_client:
        scoped_clients:
            githubApi:
                scope: 'https://api\.github\.com'
                headers:
                    Accept: 'application/vnd.github.v3+json'

And I want to inject it into my service, I can do:

    public function __construct(
        #[Target('githubApi2')]
        private HttpClientInterface $something,
    ) {
    }

But oups, I made a typo. I wrote githubApi2 instead of githubApi. But for Symfony it's OK. Instead it injects a new "blank" HttpClient (with no sane default configuration - no timeout for example).

IMHO, if one want to inject a specific target, the target must be validated. And if the target does not exist, because another developer renamed it, you want an exception. for this.

see this comments for a previous discussion.

But IMHO, if the naming is not exactly the same as the autowire alias, it's fine to not raise an exception. But in this very use case, you really want this alias. Not another one.

So I would expect an exception.

WDYT?

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DependencyInjectionHelp wantedIssues and PRs which are looking for volunteers to complete them.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions