8000 [HttpKernel] Do not attempt to register enum arguments in controller service locator by ogizanagi · Pull Request #44826 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Do not attempt to register enum arguments in controller service locator #44826

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

Merged
merged 1 commit into from
Dec 28, 2021

Conversation

ogizanagi
Copy link
Contributor
@ogizanagi ogizanagi commented Dec 28, 2021
Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets N/A
License MIT
Doc PR N/A

Given:

namespace App\Model;

enum Suit: string 
{
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

and the controller:

class FooController extends AbstractController
{
    #[Route('/foo')]
    public function foo(Suit $suit = Suit::Hearts): Response
    {
        dd($suit);
    }
}

you get:

Cannot autowire argument $suit of "App\Controller\FooController::foo()": it references class "App\Model\Suit" but no such service exists.
in vendor/symfony/dependency-injection/Container.php (line 416)
in vendor/symfony/dependency-injection/Argument/ServiceLocator.php -> getService (line 42)
in vendor/symfony/http-kernel/Controller/ArgumentResolver/ServiceValueResolver.php -> get (line 84)

which would be annoying in case you have a resolver registered to resolve this argument, but won't support this specific request, hence expect to let the DefaultValueResolver use the default value (or another resolver to try).
Currently, the DefaultValueResolver wouldn't be reached since the ServiceValueResolver would throw the above exception.

@ogizanagi ogizanagi force-pushed the service-value-resolver-enum branch from 33e16e2 to 89232ee Compare December 28, 2021 14:26
@nicolas-grekas
Copy link
Member

Thank you @ogizanagi.

@nicolas-grekas nicolas-grekas merged commit c1c6687 into symfony:4.4 Dec 28, 2021
@ogizanagi ogizanagi deleted the service-value-resolver-enum branch December 28, 2021 14:43
nicolas-grekas added a commit that referenced this pull request Dec 29, 2021
…(ogizanagi)

This PR was merged into the 4.4 branch.

Discussion
----------

[DependencyInjection][HttpKernel] Fix enum typed bindings

| Q             | A
| ------------- | ---
| Branch?       | 4.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | N/A <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Relates to #44834

While:

```yml
# services.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true
        bind:
            $myParam: !php/const App\Status::Deleted
```

is working for me, the following isn't:

```diff
# services.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true
        bind:
-            $myParam: !php/const App\Status::Deleted
+            App\Status $myParam: !php/const App\Status::Deleted
```

->

> Invalid value for binding key "App\Status $myParam" for service […]: expected "Symfony\Component\DependencyInjection\Reference", "Symfony\Component\DependencyInjection\Definition", "Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument", "Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument" or null, "App\Status" given.

This attempts to fix it by accounting for `\UnitEnum` in the `ResolveBindingPass`,
as well as re-allowing enum types already present in bindings after #44826.

Commits
-------

c32d749 [DependencyInjection][HttpKernel] Fix enum typed bindings
ogizanagi added a commit to Elao/PhpEnums that referenced this pull request Dec 30, 2021
…ed enums (ogizanagi)

This PR was squashed before being merged into the 2.x-dev branch.

Discussion
----------

[HttpKernel] Add a controller argument resolver for backed enums

- [x] Add tests
- [x] Docs
- [x] Rework to streamline the resolver with Symfony 6.1 core PR (only attributes, aims route params, 404, …) + alternative query/body resolver specific to this library.

Fixed (symfony/symfony#44826):

An issue with Symfony's `ServiceValueResolver` prevents from letting the `DefaultValueResolver` to use the default enum value if the argument is not nullable.

Perhaps add:

```php
        if (enum_exists($argument->getType())) {
            return false;
        }
```

in `\Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver::supports` as a bug fix? Fix in `RegisterControllerArgumentLocatorsPass` ?

Commits
-------

564058a Streamline with future Symfony 6.1 resolver + query/body resolvers
3e04d71 [HttpKernel] Add V2 BackedEnumValueResolver to resolve controllers args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0