10000 [DependencyInjection] Fix autocasting `null` env values to empty string with `container.env_var_processors_locator` by fancyweb · Pull Request #51198 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Fix autocasting null env values to empty string with container.env_var_processors_locator #51198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,9 @@ protected function getEnv(string $name)
$localName = $name;
}

if ($processors->has($prefix)) {
$processor = $processors->get($prefix);
} else {
$processor = new EnvVarProcessor($this);
if (false === $i) {
$prefix = '';
}
$processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this);
if (false === $i) {
$prefix = '';
}

$this->resolving[$envName] = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface EnvVarProcessorInterface
/**
* Returns the value of the given variable as managed by the current instance.
*
* @param string $prefix The namespace of the variable
* @param string $prefix The namespace of the variable; when the empty string is passed, null values should be kept as is
* @param string $name The name of the variable within the namespace
* @param \Closure $getEnv A closure that allows fetching more env vars
*
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\EnvVarProcessor;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Contracts\Service\ResetInterface;

class ContainerTest extends TestCase
Expand Down Expand Up @@ -405,6 +407,33 @@ public function testRequestAnInternalSharedPrivateService()
$c->get('internal_dependency');
$c->get('internal');
}

public function testGetEnvDoesNotAutoCastNullWithDefaultEnvVarProcessor()
{
$container = new Container();
$container->setParameter('env(FOO)', null);
$container->compile();

$r = new \ReflectionMethod($container, 'getEnv');
$r->setAccessible(true);
$this->assertNull($r->invoke($container, 'FOO'));
}

public function testGetEnvDoesNotAutoCastNullWithEnvVarProcessorsLocatorReturningDefaultEnvVarProcessor()
{
$container = new Container();
$container->setParameter('env(FOO)', null);
$container->set('container.env_var_processors_locator', new ServiceLocator([
'string' => static function () use ($container): EnvVarProcessor {
return new EnvVarProcessor($container);
},
]));
$container->compile();

$r = new \ReflectionMethod($container, 'getEnv');
$r->setAccessible(true);
$this->assertNull($r->invoke($container, 'FOO'));
}
}

class ProjectServiceContainer extends Container
Expand Down
0