Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Remove default env type check on validate #27470

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

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 7 additions & 21 deletions 28 src/Symfony/Component/Config/Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,35 +507,21 @@ private static function resolvePlaceholderValue($value)
return $value;
}

private static function getType($value): string
{
switch ($type = \gettype($value)) {
case 'boolean':
return 'bool';
case 'double':
return 'float';
case 'integer':
return 'int';
}

return $type;
}

private function doValidateType($value): void
{
if (null === $this->handlingPlaceholder || null === $value) {
$this->validateType($value);

return;
}

if (!$this->allowPlaceholders()) {
if (null !== $this->handlingPlaceholder && !$this->allowPlaceholders()) {
$e = new InvalidTypeException(sprintf('A dynamic value is not compatible with a "%s" node type at path "%s".', get_class($this), $this->getPath()));
$e->setPath($this->getPath());

throw $e;
}

if (null === $this->handlingPlaceholder || null === $value) {
$this->validateType($value);

return;
}

$knownTypes = array_keys(self::$placeholders[$this->handlingPlaceholder]);
$validTypes = $this->getValidPlaceholderTypes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -42,21 +41,20 @@ public function process(ContainerBuilder $container)
return;
}

$defaultBag = new ParameterBag($container->getParameterBag()->all());
$defaultBag = new ParameterBag($resolvingBag->all());
$envTypes = $resolvingBag->getProvidedTypes();
try {
foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) {
$prefix = (false === $i = strpos($env, ':')) ? 'string' : substr($env, 0, $i);
$types = $envTypes[$prefix] ?? array('string');
$default = ($hasEnv = (false === $i && $defaultBag->has("env($env)"))) ? $defaultBag->get("env($env)") : null;

if (null !== $default && !in_array($type = self::getType($default), $types, true)) {
throw new LogicException(sprintf('Invalid type for env parameter "env(%s)". Expected "%s", but got "%s".', $env, implode('", "', $types), $type));
}

$values = array();
foreach ($types as $type) {
$values[$type] = $hasEnv ? $default : self::$typeFixtures[$type] ?? null;
if (false === $i = strpos($env, ':')) {
$default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : null;
$defaultType = null !== $default ? self::getType($default) : 'string';
$values[$defaultType] = $default;
} else {
$prefix = substr($env, 0, $i);
foreach ($envTypes[$prefix] ?? array('string') as $type) {
$values[$type] = self::$typeFixtures[$type] ?? null;
}
}
foreach ($placeholders as $placeholder) {
BaseNode::setPlaceholder($placeholder, $values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@

class ValidateEnvPlaceholdersPassTest extends TestCase
{
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
* @expectedExceptionMessage Invalid type for env parameter "env(FOO)". Expected "string", but got "bool".
*/
public function testDefaultEnvIsValidatedByType()
public function testEnvsAreValidatedInConfig()
{
$container = new ContainerBuilder();
$container->setParameter('env(FOO)', true);
$container->registerExtension(new EnvExtension());
$container->prependExtensionConfig('env_extension', array(
'scalar_node' => '%env(FOO)%',
$container->setParameter('env(NULLED)', null);
$container->setParameter('env(FLOATISH)', 3.2);
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = array(
'scalar_node' => '%env(NULLED)%',
'scalar_node_not_empty' => '%env(FLOATISH)%',
'int_node' => '%env(int:FOO)%',
'float_node' => '%env(float:BAR)%',
));

$this->doProcess($container);

$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
}

public function testEnvsAreValidatedInConfig()
public function testDefaultEnvWithoutPrefixIsValidatedInConfig()
{
$container = new ContainerBuilder();
$container->setParameter('env(NULLED)', null);
$container->setParameter('env(FLOATISH)', 3.2);
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = array(
'scalar_node' => '%env(NULLED)%',
'int_node' => '%env(int:FOO)%',
'float_node' => '%env(float:BAR)%',
'float_node' => '%env(FLOATISH)%',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently this fails, im not sure what we expect here. Env without a prefix is now assumed to be a scalar which is not allowed for float-only node.

Copy link
Member

@nicolas-grekas nicolas-grekas Jun 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we use is_numeric() somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a scalar issue in general, directly related to

if (null !== $defaultValue && !is_scalar($defaultValue)) {
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', gettype($defaultValue), $name));
}

im not sure what's the behavior we're seeking... what does "env without prefix" mean on its value? It if can be any scalar then using it for a float node is invalid, technically.

to make it work we should simply avoid the type check in config for this case, but it makes validation less strict.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to prefix a processor here? Referencing the envelope bar with float: prepended?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the thing is, without the float cast in the env var retrieval, the service would not receive a float, but a string. So float_node not accepting %env(FLOATISH)% is expected to me.

));

$this->doProcess($container);
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/DependencyInjection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
},
"conflict": {
"symfony/config": "<4.1",
"symfony/config": "<4.1.1",
"symfony/finder": "<3.4",
"symfony/proxy-manager-bridge": "<3.4",
"symfony/yaml": "<3.4"
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.