Skip to content

[DependencyInjection] Unable to dump a service container when autowiring with enum default value #48178

Closed
@guillaumesmo

Description

@guillaumesmo

Symfony version(s) affected

v6.2.0-BETA2

Description

When trying to autowire a service which contains an enum default value, the container build fails with the following error:

Error: Uncaught Symfony\Component\DependencyInjection\Exception\RuntimeException: Unable to dump a service container if a parameter is an object or a resource. in [...]/vendor/symfony/dependency-injection/Dumper/XmlDumper.php:417
Stack trace:
#0 [...]/vendor/symfony/dependency-injection/Dumper/XmlDumper.php(372): Symfony\Component\DependencyInjection\Dumper\XmlDumper::phpToXml(Object(class@anonymous))
#1 [...]/vendor/symfony/dependency-injection/Dumper/XmlDumper.php(160): Symfony\Component\DependencyInjection\Dumper\XmlDumper->convertParameters(Array, 'argument', Object(DOMElement))
#2 [...]/vendor/symfony/dependency-injection/Dumper/XmlDumper.php(257): Symfony\Component\DependencyInjection\Dumper\XmlDumper->addService(Object(Symfony\Component\DependencyInjection\Definition), 'test', Object(DOMElement))
#3 [...]/vendor/symfony/dependency-injection/Dumper/XmlDumper.php(50): Symfony\Component\DependencyInjection\Dumper\XmlDumper->addServices(Object(DOMElement))
#4 [...]/vendor/symfony/framework-bundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php(32): Symfony\Component\DependencyInjection\Dumper\XmlDumper->dump()
#5 [...]/vendor/symfony/dependency-injection/Compiler/Compiler.php(73): Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass->process(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#6 [...]/vendor/symfony/dependency-injection/ContainerBuilder.php(721): Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#7 [...]/vendor/symfony/http-kernel/Kernel.php(487): Symfony\Component\DependencyInjection\ContainerBuilder->compile()
#8 [...]/vendor/symfony/http-kernel/Kernel.php(709): Symfony\Component\HttpKernel\Kernel->initializeContainer()
#9 [...]/vendor/symfony/http-kernel/Kernel.php(172): Symfony\Component\HttpKernel\Kernel->preBoot()
#10 [...]/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php(35): Symfony\Component\HttpKernel\Kernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#11 [...]/vendor/autoload_runtime.php(29): Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
#12 [...]/public/index.php(5): require_once('[...]/vendo...')
#13 {main}
thrown

How to reproduce

services:
    _defaults:
        autowire: true
        autoconfigure: true

    test:
        class: App\Service\TestService
        arguments:
            $bubble: false
<?php

namespace App\Service;

use Monolog\Level;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class TestService
{
    public function __construct(
        ParameterBagInterface $parameterBag,
        int|string|Level $level = Level::Debug,
        bool $test = true,
        bool $bubble = true
    ) {
    }
}

once you change $level = Level::Debug to $level = 100, the error disappear and the container build is successful

also interesting is that removing the bool $test = true, also compiles without error, so it's more complex than just "enum default values do not work"

Possible Solution

No response

Additional Context

I experience this issue when trying to migrate a logging handler from monolog v2 to v3, where the best practice is to express default levels with enums.

workaround is to add the default value in the arguments as well:

    test:
        class: App\Service\TestService
        arguments:
            $level: 100 # or !php/enum Monolog\Level:Debug
            $bubble: false

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