Skip to content
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

[Config] beforeNormalization with thenUnset leads to unhandled exception #29142

Closed
edefimov opened this issue Nov 8, 2018 · 3 comments
Closed

Comments

@edefimov
Copy link
Contributor

edefimov commented Nov 8, 2018

Symfony version(s) affected: 4.1.7 (actually all versions)

Description
Suppose we have an implementation of Symfony\Component\Config\Definition\ConfigurationInterface. If we define beforeNormalization part for some node definition and call thenUnset as a result of if part, the application will crash with Symfony\Component\Config\Definition\Exception\UnsetKeyException.

How to reproduce

<?php

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;

require_once 'vendor/autoload.php';

$configuration = new class implements ConfigurationInterface {
    public function getConfigTreeBuilder()
    {
        $builder = new TreeBuilder();
        $root = $builder->root('root');
        $root
            ->children()
                ->scalarNode('value')
                ->beforeNormalization()
                    ->ifEmpty()
                    ->thenUnset()
                ->end()
            ->end();

        return $builder;
    }
};

$config = (new Processor())->processConfiguration($configuration, [['value' => null]]);

The output:

 Fatal error: Uncaught Symfony\Component\Config\Definition\Exception\UnsetKeyException: Unsetting key in vendor/symfony/symfony/src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php on line 203

Symfony\Component\Config\Definition\Exception\UnsetKeyException: Unsetting key in vendor/symfony/symfony/src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php on line 203

Call Stack:
    0.0002     402960   1. {main}() reproduce.php:0
    0.0022     715928   2. Symfony\Component\Config\Definition\Processor->processConfiguration() reproduce.php:27
    0.0053    1086056   3. Symfony\Component\Config\Definition\Processor->process() vendor/symfony/symfony/src/Symfony/Component/Config/Definition/Processor.php:52
    0.0053    1086056   4. Symfony\Component\Config\Definition\ArrayNode->normalize() vendor/symfony/symfony/src/Symfony/Component/Config/Definition/Processor.php:35
    0.0053    1086432   5. Symfony\Component\Config\Definition\ArrayNode->normalizeValue() vendor/symfony/symfony/src/Symfony/Component/Config/Definition/BaseNode.php:363
    0.0053    1086432   6. Symfony\Component\Config\Definition\ScalarNode->normalize() vendor/symfony/symfony/src/Symfony/Component/Config/Definition/ArrayNode.php:295
    0.0053    1086432   7. Symfony\Component\Config\Definition\Builder\ExprBuilder::Symfony\Component\Config\Definition\Builder\{closure}() vendor/symfony/symfony/src/Symfony/Component/Config/Definition/BaseNode.php:335
    0.0053    1086432   8. Symfony\Component\Config\Definition\Builder\ExprBuilder->Symfony\Component\Config\Definition\Builder\{closure}() vendor/symfony/symfony/src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php:241
@ro0NL
Copy link
Contributor

ro0NL commented Nov 9, 2018

i think this is actually unsupported :D (reproduced in 2.8) Normalization doesnt handle the unset exception. Only finalization.

So instead of beforeNormalization() it should be validate()

now thinking what can be done :) not sure conceptually this needs to/can be supported.

@ro0NL
Copy link
Contributor

ro0NL commented Nov 9, 2018

See #29152

@edefimov
Copy link
Contributor Author

edefimov commented Nov 9, 2018

#29152 Works!

I know the code above perfectly works with validate, but in some cases it is necessary to drop value before validation takes place.

@fabpot fabpot closed this as completed Nov 12, 2018
fabpot added a commit that referenced this issue Nov 12, 2018
This PR was squashed before being merged into the 2.8 branch (closes #29152).

Discussion
----------

[Config] Unset key during normalization

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes-ish
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #29142
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

2.8 vs. 4.x :) let me know.

Commits
-------

e1402d4 [Config] Unset key during normalization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants