-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Config] Add ArrayNodeDefinition::acceptAndWrap() to list alternative types that should be accepted and wrapped in an array
#51273
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
Conversation
7b1952c to
e6694e6
Compare
This comment was marked as outdated.
This comment was marked as outdated.
src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php
Outdated
Show resolved
Hide resolved
e6694e6 to
1b84880
Compare
| if (isset($this->normalization)) { | ||
| $node->setNormalizationClosures($this->normalization->before); | ||
| $node->setNormalizedTypes($this->normalization->declaredTypes); | ||
| $node->setNormalizedTypes($this->allowedTypes ?? $this->normalization->declaredTypes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is $this->normalization->declaredTypes filled normally ? and why do we need to add this new $this->allowedTypes property instead of using the existing feature (which gets ignored entirely) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering the same until I (re)discovered #44166, which is based on that: sniffing for allowed types in what I'd call an indirect approach. I'm here proposing a more explicit approach, which wins over the implicit one when used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overriding entirely the types inferred from the normalization looks wrong to me (unless we want to deprecate that behavior).
And the suggested API does not play well with nodes supporting multiple normalization rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has to be this way, see L1532 in the Configuration class for an example: implicit types are loose and too many times we have to fallback to mixed (->always(), ->ifTrue($fn), etc.). Being explicit need to override the implicit collection of possible types - as not all types managed by normalizers have to be accepted at the config builder level (because of the XML loader mainly).
Which means this has to be the way to go.
I renamed the new method ->acceptAndWrap() instead of ->castToArray(). This is both more explicit and more accurate.
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
Outdated
Show resolved
Hide resolved
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php
Outdated
Show resolved
Hide resolved
1b84880 to
83c0b30
Compare
83c0b30 to
48a78ed
Compare
ArrayNodeDefinition::acceptAndWrap() to list alternative types that should be accepted and wrapped in an array
37153cf to
73b4c37
Compare
|
Thanks for the review @GromNaN - PR updated. |
| ExprBuilder::TYPE_STRING => is_string(...), | ||
| ExprBuilder::TYPE_BOOL => is_bool(...), | ||
| ExprBuilder::TYPE_NULL => is_null(...), | ||
| ExprBuilder::TYPE_BACKED_ENUM => static fn ($v) => $v instanceof \BackedEnum, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right, let's keep that for a future improvement.
Enable more specific types Requires symfony/symfony#51273
|
Thank you @nicolas-grekas. |
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… from Kernel for testing (Kocal) This PR was merged into the 2.x branch. Discussion ---------- Remove explicit configuration `twig.exception_controller` from Kernel for testing | Q | A | -------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Documentation? | no <!-- required for new features, or documentation updates --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT Related to symfony/symfony#51273 (comment) Fix https://github.com/symfony/ux/actions/runs/17999302594/job/51204828176?pr=3102 Commits ------- f46109a Remove explicit configuration `twig.exception_controller` from Kernel for testing
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
… for testing Related to symfony/symfony#51273 (comment)
…nodes that can be enabled or disabled (nicolas-grekas) This PR was merged into the 7.4 branch. Discussion ---------- [Config] Config builders should accept booleans for array nodes that can be enabled or disabled | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT Continuing my journey on the config component, after #51273 That could be considered as a bugfix, but it's not worth fixing on lower branches because nobody is using this missing capability - by definition. So: this allows doing eg `->lock(false)` to disable locks, as expected when the node definition has `->treatFalseLike(['enabled' => false])` Note that I didn't turn `->treatNullLike(['enabled' => true])` into accepting `null` as I don't see the point of allowing `->lock(null)` when one can use `->lock(true)`. Commits ------- 633169f [Config] Config builders should accept booleans for array nodes that can be enabled or disabled
With #58450, we lost some type info when generating config builders, by preventing #44166 to work correctly.
This PR improves the description of config trees by adding an
ArrayNodeDefinition::acceptAndWrap()method that allows one to declare which alternative types should be accepted at array-node levels. This declaration then triggers the wrapping of such cases into arrays, and also hints the config builder generator so that it can generate more accurate types for configurator methods.