Skip to content

[Config] Fix ArrayNode extra keys "ignore" and "remove" behaviors #15593

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 30, 2015
Merged

[Config] Fix ArrayNode extra keys "ignore" and "remove" behaviors #15593

merged 1 commit into from
Sep 30, 2015

Conversation

ogizanagi
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Due to #14238 , no more exception is thrown when submitting extra keys to an ArrayNode.
For instance:

$builder = new TreeBuilder();

$nodeDefinition = $builder->root('root')
    ->children()
        ->scalarNode('foo')
    ->end()
->end();

$node = $nodeDefinition->getNode(true);
$node->normalize(array(
    'foo' => 'ok',
    'bar' => 'ko',
));

will not throw a

Symfony\Component\Config\Definition\Exception\InvalidConfigurationException: Unrecognized option "bar" under "root"`

anymore, as it does in 2.7.

I think the expected behavior is:

Submitted data: ['bar' => 'ko']

Ignore Remove Expected OK Comment
true true [ ] ✔︎ Previous behavior when ignoring.
true false ['bar' => 'ko'] ✔︎ This is the result targeted by #14238.
false true exception Removing makes no sense when not ignoring extra keys.
The exception should still be thrown.
false false exception Previous behavior (2.7).
Should not have changed

@ogizanagi
Copy link
Contributor Author

So, what about this bug fix ?
cc @stof

@xelaris
Copy link
Contributor

xelaris commented Sep 22, 2015

I can not evaluate this fix for now, but can confirm the issue in 2.8. To give a practical example... since #14238, it is possible to write meaningless configuration in a Symfony Standard Edition like:

framework:
    secret:          "%secret%"
    foo:             bar
    default_locale:  "%locale%"

or more serious

security:
  providers:
    in_memory:
      memory:
        johndoe: { password: 'secet', roles: ['ROLE_USER'] }

without throwing an exception. Where the second example is missing the users key under the memory provider which leads to errors which are very hard to find.

@xelaris
Copy link
Contributor

xelaris commented Sep 26, 2015

I think testIgnoreExtraKeysNoException and testIgnoreExtraKeysNotRemoved became redundant by the new testIgnoreAndRemoveBehaviors and can be removed. Apart from that the PR looks good to me. Thank you @ogizanagi for working on this!

Status: Needs work

@ogizanagi
Copy link
Contributor Author

@xelaris : You're right. I removed these methods.
Thanks for raising this.

@xelaris
Copy link
Contributor

xelaris commented Sep 26, 2015

Looks good to me.

Status: Reviewed

@@ -29,7 +29,7 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
protected $addIfNotSet = false;
protected $performDeepMerging = true;
protected $ignoreExtraKeys = false;
protected $removeExtraKeys = true;
protected $removeExtraKeys = false;
Copy link
Member

Choose a reason for hiding this comment

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

This is not BC: if someone was ignoring values, you would not remove values anymore by default

Copy link
Contributor

Choose a reason for hiding this comment

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

When I thought about that earlier, I was of the opinion that if someone was ignoring values before, setIgnoreExtraKeys must be called to do that and the $remove parameter was added with the default value of true, which would keeps BC then. When rethinking it now, someone could inherit from ArrayNode and change $ignoreExtraKeys via other methods as it is protected, although this should be an edge case.

But I think setting the initial value of this property to true requires no other changes, as it also throws an exception on extra keys when not ignoring them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I don't remember why I changed those values at the time.
Updated.

@xelaris
Copy link
Contributor

xelaris commented Sep 28, 2015

Thanks @ogizanagi. I think this is ready to be merge now.

@fabpot
Copy link
Member

fabpot commented Sep 30, 2015

👍 ping @symfony/deciders

@xabbuh xabbuh added the Config label Sep 30, 2015
@xabbuh
Copy link
Member

xabbuh commented Sep 30, 2015

👍

@fabpot
Copy link
Member

fabpot commented Sep 30, 2015

Thank you @ogizanagi.

@fabpot fabpot merged commit d961f7f into symfony:2.8 Sep 30, 2015
fabpot added a commit that referenced this pull request Sep 30, 2015
…haviors (ogizanagi)

This PR was merged into the 2.8 branch.

Discussion
----------

[Config] Fix ArrayNode extra keys "ignore" and "remove" behaviors

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Due to #14238 , no more exception is thrown when submitting extra keys to an `ArrayNode`.
For instance:

```php
$builder = new TreeBuilder();

$nodeDefinition = $builder->root('root')
    ->children()
        ->scalarNode('foo')
    ->end()
->end();

$node = $nodeDefinition->getNode(true);
$node->normalize(array(
    'foo' => 'ok',
    'bar' => 'ko',
));
```

will not throw a
> Symfony\Component\Config\Definition\Exception\InvalidConfigurationException: Unrecognized option "bar" under "root"`

anymore, as it does in 2.7.

I think the expected behavior is:

`Submitted data: ['bar' => 'ko']`

Ignore | Remove  | Expected | OK | Comment
---------| ------------ | ------------- | ------ | ----------
true    | true          | `[ ]`                   | ✔︎ | Previous behavior when ignoring.
true    | false          | `['bar' => 'ko']` | ✔︎ | This is the result targeted by #14238.
false    | true          | exception    | ✘ | Removing makes no sense when not ignoring extra keys. <br/>The exception should still be thrown.
false    | false          | exception    | ✘ | Previous behavior (2.7). <br/>Should not have changed

Commits
-------

d961f7f [Config] Fix ArrayNode extra keys "ignore" and "remove" behaviors
@ogizanagi ogizanagi deleted the config/fix_array_node_ignore_remove branch September 30, 2015 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants