You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The issue is, \array_unique() compares values according to their string representation. I believe this is less than idea in the case of EnumNode. The \array_unique(), unlike functions like \in_array() does not support $strict parameter (and the discussion on that died in 2013). Thus, to really solve this issue a custom strict function would need to be used:
However, this does not cover empty string case. Given the EnumNode usage of the array where keys in the array aren't important, I think the solution would be to replace \array_unique() call with a function like:
kiler129
changed the title
[Config] EnumNode removes null and empty string values
[Config][RFC] EnumNode removes values based on loose comparison
Jan 8, 2023
We do use boolean and null values in our own core bundles (EnumNode values are therefore not restricted to scalar values) so I guess the case you describe should logically work. But since it never did for the past 10 years that would be a new feature I guess?
We could maybe simply remove the array_unique call and let the duplicated values be displayed several times in messages and configuration references dumps?
This PR was merged into the 6.3 branch.
Discussion
----------
[Config] Do not array_unique EnumNode values
| Q | A
| ------------- | ---
| Branch? | 6.3
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | #48913
| License | MIT
| Doc PR | -
Using `array_unique` for the values doesn't have an impact on the core behavior of this node but it causes an issue with non-scalar values that get coerced to the same string. Since it never worked, we could apply this change safely on 6.3.
Commits
-------
c9d8b15 [Config] Do not array_unique EnumNode values
Symfony version(s) affected
v2.1.0+
Description
In my use-case I have a node with
enabled
field, which can have 3 values:true
: always enable componentfalse
: do not enable the componentnull
: enable if it's needed (dependent on rather complex logic, outside of the scope of config schema)The issue is I cannot use
booleanNode()
as it is well... boolean.How to reproduce
While the code above "works", using
bin/console config:dump-reference
reveals that in fact thenull
is not an expected values:Possible Solution
The issue stems from usage of
array_unique()
in two places:symfony/src/Symfony/Component/Config/Definition/Builder/EnumNodeDefinition.php
Lines 13 to 19 in 8308aea
symfony/src/Symfony/Component/Config/Definition/EnumNode.php
Lines 13 to 19 in 8308aea
The issue is,
\array_unique()
compares values according to their string representation. I believe this is less than idea in the case ofEnumNode
. The\array_unique()
, unlike functions like\in_array()
does not support$strict
parameter (and the discussion on that died in 2013). Thus, to really solve this issue a custom strict function would need to be used:However, this does not cover empty string case. Given the
EnumNode
usage of the array where keys in the array aren't important, I think the solution would be to replace\array_unique()
call with a function like:Additional Context
If the intention of
EnumNode
to be type-loose, then my suggested fix is obviously a no-go. In such a case I think it should be noted in the docs.The text was updated successfully, but these errors were encountered: