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

Skip to content

Commit 1e1d91e

Browse files
bug #50837 [DependencyInjection] Fix autocasting null env values to empty string (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Fix autocasting null env values to empty string | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | - | Deprecations? | - | Tickets | #50815 (comment) | License | MIT | Doc PR | - This PR fixes autocasting `null` env values to `''`. We continue to autocast all other values to string when there's no prefix. It doesn't revert #50517, it just fixes an unintended side effect. It doesn't fix #50815 request, it confirms the behavior: the solution for this issue would be to remove the `string:` prefix. There's still no solution to have the "keep null as null" and "cast if not null" behavior. Commits ------- 2c0815d [DependencyInjection] Fix autocasting null env values to empty string
2 parents c587137 + 2c0815d commit 1e1d91e

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,15 @@ protected function getEnv(string $name)
390390
$prefix = 'string';
391391
$localName = $name;
392392
}
393-
$processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this);
393+
394+
if ($processors->has($prefix)) {
395+
$processor = $processors->get($prefix);
396+
} else {
397+
$processor = new EnvVarProcessor($this);
398+
if (false === $i) {
399+
$prefix = '';
400+
}
401+
}
394402

395403
$this->resolving[$envName] = true;
396404
try {

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
126126
}
127127
}
128128

129+
$returnNull = false;
130+
if ('' === $prefix) {
131+
$returnNull = true;
132+
$prefix = 'string';
133+
}
134+
129135
if (false !== $i || 'string' !== $prefix) {
130136
$env = $getEnv($name);
131137
} elseif (isset($_ENV[$name])) {
@@ -177,6 +183,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
177183
}
178184

179185
if (null === $env) {
186+
if ($returnNull) {
187+
return null;
188+
}
189+
180190
if (!isset($this->getProvidedTypes()[$prefix])) {
181191
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
182192
}

src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,13 @@ public static function provideGetEnvUrlPath()
763763

764764
/**
765765
* @testWith ["", "string"]
766+
* [null, ""]
766767
* [false, "bool"]
767768
* [true, "not"]
768769
* [0, "int"]
769770
* [0.0, "float"]
770771
*/
771-
public function testGetEnvCastsNull($expected, string $prefix)
772+
public function testGetEnvCastsNullBehavior($expected, string $prefix)
772773
{
773774
$processor = new EnvVarProcessor(new Container());
774775

@@ -778,4 +779,17 @@ public function testGetEnvCastsNull($expected, string $prefix)
778779
});
779780
}));
780781
}
782+
783+
public function testGetEnvWithEmptyStringPrefixCastsToString()
784+
{
785+
$processor = new EnvVarProcessor(new Container());
786+
unset($_ENV['FOO']);
787+
$_ENV['FOO'] = 4;
788+
789+
try {
790+
$this->assertSame('4', $processor->getEnv('', 'FOO', function () { $this->fail('Should not be called'); }));
791+
} finally {
792+
unset($_ENV['FOO']);
793+
}
794+
}
781795
}

0 commit comments

Comments
 (0)