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

Skip to content

[6.2] [DI] Node Value ignored in XMLFileLoader #48445

Closed
@BrandonlinU

Description

@BrandonlinU

Symfony version(s) affected

6.2.0

Description

After update to Symfony 6.2, the new changes to support the array tag attributes introduced in #47364 throw an exception when it load a service tag with the name in the inner content of the tag.

How to reproduce

  1. Create a new project with symfony/dependency-injection and symfony/config dependencies.
  2. Create a index.php file with the following content:
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

$containerBuilder = new ContainerBuilder();
$loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
$loader->load('services.xml');
  1. Generate a service.xml with a service description with a tag, as the following.
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services">
    <services>
        <service id="dummy_service" class="App\Service\DummyService">
            <tag>dummy_tag</tag>
        </service>
    </services>
</container>
  1. Try to run the index.php script.

Possible Solution

It looks like that in the XmlFileLoader tries to extract the tagName incorrectly, because the $tag->hasChildNodes() return a boolean, and not an string, and when tries to extract the nodeValue, it extracts the attribute name instead, causing the error.

if ('' === $tagName = $tag->hasChildNodes() || '' === $tag->nodeValue ? $tag->getAttribute('name') : $tag->nodeValue) {
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', (string) $service->getAttribute('id'), $file));
}

I think that with the following code, that tries to extract the nodeValue and if it is not possible tries to get the attribute name from the XML tag, should solve the error.

if ('' === $tagName = $tag->nodeValue ?: $tag->getAttribute('name')) {
    throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', (string) $service->getAttribute('id'), $file));
}

Additional Context

The exception throw is the following:

Symfony\Component\DependencyInjection\Exception\InvalidArgumentException : The tag name for service "instacar.extra_filters.security_expression_value_provider" in "/home/bantonio/PhpstormProjects/FilterBundle/src/DependencyInjection/../Resources/config/security.xml" must be a non-empty string.

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