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

Skip to content

[DependencyInjection] ResolveChildDefinitionsPass with decorator stack #59186

Open
@mtarld

Description

@mtarld

Symfony version(s) affected

6.4

Description

When defining services using stacks:

->stack('json_encoder.encode.property_metadata_loader', [
    inline_service(EncodeAttributePropertyMetadataLoader::class)
        ->args([
            service('.inner'),
            tagged_locator('json_encoder.normalizer'),
            service('type_info.resolver'),
        ]),
    // ...
])

And running Symfony CI in low deps, some tests using the ResolveChildDefinitionsPass are falling.

How to reproduce

Revert the https://github.com/symfony/symfony/pull/59179/files PR and run "PHP8.2 low-deps" tests.
This must end like https://github.com/symfony/symfony/actions/runs/12280275223/job/34266294785?pr=59177

Here is an example of the failure message:

1) Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\PhpFrameworkExtensionTest::testLockWithService
Symfony\Component\DependencyInjection\Exception\RuntimeException: Service "json_encoder.encode.property_metadata_loader": Parent definition "" does not exist.

Possible Solution

Seems like the bug is because the parent of the ChildDefinition is "".

A quick fix could be to change the following in ResolveChildDefinitionsPass:

protected function processValue(mixed $value, bool $isRoot = false): mixed
{
    if (!$value instanceof Definition) {
        return parent::processValue($value, $isRoot);
    }
    if ($isRoot) {
        // yes, we are specifically fetching the definition from the
        // container to ensure we are not operating on stale data
        $value = $this->container->getDefinition($this->currentId);
    }
-    if ($value instanceof ChildDefinition) {
+    if ($value instanceof ChildDefinition && $value->getParent()) {

        $this->currentPath = [];
        $value = $this->resolveDefinition($value);
        if ($isRoot) {
            $this->container->setDefinition($this->currentId, $value);
        }
    }

    return parent::processValue($value, $isRoot);
}

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