Skip to content

[config] Error occurs when the key attribute is removed from the child node and only a key named 'value' is left there #15270

@zerustech

Description

@zerustech

When the key attribute is removed from a child node of a prototyped array node, if there is only one key named 'value' left in the child node, the child node will be degenerated from an array into a string.
When processing the config tree, an 'Symfony\Component\Config\Definition\Exception\InvalidTypeException' is thrown.

The root cause of this issue is:
before the degeneration, the prototype of the child node is ArrayNode and the child node matches its prototype. After the degeneration, the child node has become a ScalarNode, but its prototype is still ArrayNode.
I have created PR #14082 for this issue, but didn't receive any feedback yet.

The following example may reproduce this issue:

<?php

$loader = require_once __DIR__.'/app/autoload.php';

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Parser as YamlParser;

$builder = new TreeBuilder();
$configs = array();
$parser = new YamlParser();
$files = array(__DIR__.'/attribute-as-key-left-value-only.yml');      

$root = $builder->root('app');
$root
    ->fixXmlConfig('parameter')
    ->children()
        ->arrayNode('parameters')
            ->useAttributeAsKey('name')
            ->prototype('array')
                ->children()
                    ->scalarNode('name')->end()
                    ->scalarNode('value')->end()
                ->end()
        ->end()
    ->end();

foreach($files as $file)
{
    $config = $parser->parse(file_get_contents($file));
    $configs[] = $config['app'];
}

$processor = new Processor();
$tree = $builder->buildTree();
$result = $processor->process($tree, $configs);
# attribute-as-key-left-value-only.yml
app:
    parameters:
        - name: server_name
          value: web.local
        - name: ip
          value: 192.168.1.2
        - name: username
          value: admin
        - name: password
          value: password

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