Container parameters defined as arrays can't contain "%" character

Created on 1 July 2024, 2 months ago
Updated 2 July 2024, 2 months ago

Problem/Motivation

The escape function defined in Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper file is changing all occurrences of the percent sign to a double percent, as stated in the Symfony documentation.

However, if we set a parameter that is an array, the abc%de string from this array will be escaped to abc%%%%de, and passed like this to the services.

Steps to reproduce

1. Create a CompilerPass and register it:


namespace Drupal\my_module\Compiler;

use Drupal\Core\Site\Settings;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MyModuleCompilerPass implements CompilerPassInterface {

  public function process(ContainerBuilder $container): void {
    $container->setParameter('my_module.configuration', [
      'option1' => 'string',
      'option2' => 'string%',
      'option3' => 'string%%',
    ]);
  }

}
namespace Drupal\my_module;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\my_module\Compiler\MyModuleCompilerPass;

class MyModuleServiceProvider extends ServiceProviderBase {

  public function register(ContainerBuilder $container): void {
    $container->addCompilerPass(new MyModuleCompilerPass());
  }

}

2. Define a service that uses these parameters:

services:
  my_module.my_service:
    class: Drupal\my_module\MyService
    arguments:
      - '%my_module.configuration%'
class MyService {
  public function __construct(protected readonly array $configuration) {
    // Actual result:
    // $configuration['option1'] = 'string'
    // $configuration['option2'] = 'string%%%%'
    // $configuration['option3'] = 'string%%%%%%%%'

    // Expected result:
    // $configuration['option1'] = 'string'
    // $configuration['option2'] = 'string%'
    // $configuration['option3'] = 'string%%'
  }

This happens because the escape function is called twice on the same data.

In case of defining a string parameter (instead of the array), the value is properly escaped.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated 19 minutes ago

Created by

πŸ‡΅πŸ‡±Poland gpietrzak WrocΕ‚aw

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024