Skip to content

[DependencyInjection] Multiple Autoconfigure attributes are accepted, but the default values of the last one are applied #56841

@GromNaN

Description

@GromNaN

Symfony version(s) affected

5.4 - 7.1.0-rc1

Description

When the attribute Autoconfigure is used multiple times on the same class, only the last instance is used, other seems to be ignored.

How to reproduce

use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;

#[Autoconfigure(lazy: true)]
#[Autoconfigure(shared: false)]
interface MyInterface {}
class MyClass implements MyInterface {}
$ bin/console debug:container MyClass

Information for Service "App\MyClass"
=====================================

 ---------------- ----------------- 
  Option           Value            
 ---------------- ----------------- 
  Service ID       App\MyClass      
  Class            App\MyClass      
  Tags             -  
  Public           no               
  Synthetic        no               
  Lazy             no               <- Default value from the 2nd attribute, ignoring the value set in the 1st attribute
  Shared           no               <- Value set in the 2nd attribute
  Abstract         no               
  Autowired        yes              
  Autoconfigured   yes              
  Usages           App\MyInterface  
 ---------------- ----------------- 

If the order of the attribute is modified:

use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;

#[Autoconfigure(shared: false)]
#[Autoconfigure(lazy: true)]
interface MyInterface {}
bin/console debug:container MyClass

Information for Service "App\MyClass"
=====================================

 ---------------- ----------------- 
  Option           Value            
 ---------------- ----------------- 
  Service ID       App\MyClass      
  Class            App\MyClass      
  Tags             -                
  Public           no               
  Synthetic        no               
  Lazy             yes              <- Value set in the 2nd attribute
  Shared           yes              <- Default value from the 2nd attribute, ignoring the value set in the 1st attribute
  Abstract         no               
  Autowired        yes              
  Autoconfigured   yes              
  Usages           App\MyInterface  
 ---------------- ----------------- 

Possible Solution

Remove null values when the attribute is loaded:

$class->name => [$container->registerForAutoconfiguration($class->name)] + $attribute,

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions