Skip to content

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Aug 4, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

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.

@maxbeckers

This comment was marked as outdated.

@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
if (isset($this->normalization)) {
$node->setNormalizationClosures($this->normalization->before);
$node->setNormalizedTypes($this->normalization->declaredTypes);
$node->setNormalizedTypes($this->allowedTypes ?? $this->normalization->declaredTypes);
Copy link
Member

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) ?

Copy link
Member Author

@nicolas-grekas nicolas-grekas Sep 17, 2025

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.

Copy link
Member

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.

Copy link
Member Author

@nicolas-grekas nicolas-grekas Sep 18, 2025

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.

@nicolas-grekas nicolas-grekas changed the title [Config] Improve casting config nodes to array [Config] Add ArrayNodeDefinition::acceptAndWrap() to list alternative types that should be accepted and wrapped in an array Sep 18, 2025
@nicolas-grekas nicolas-grekas force-pushed the config-cast-to-array branch 3 times, most recently from 37153cf to 73b4c37 Compare September 18, 2025 13:10
@nicolas-grekas
Copy link
Member Author

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,
Copy link
Member

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.

GromNaN added a commit to GromNaN/DoctrineMongoDBBundle that referenced this pull request Sep 22, 2025
@fabpot
Copy link
Member

fabpot commented Sep 23, 2025

Thank you @nicolas-grekas.

@fabpot fabpot merged commit a30f387 into symfony:7.4 Sep 23, 2025
7 of 12 checks passed
@nicolas-grekas nicolas-grekas deleted the config-cast-to-array branch September 23, 2025 07:14
Kocal added a commit to Kocal/symfony-ux that referenced this pull request Sep 25, 2025
Kocal added a commit to Kocal/symfony-ux that referenced this pull request Sep 25, 2025
Kocal added a commit to symfony/ux that referenced this pull request Sep 25, 2025
… 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
symfony-splitter pushed a commit to symfony/ux-notify that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-chartjs that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-lazy-image that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-map that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-vue that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-cropperjs that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-svelte that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-react that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-toggle-password that referenced this pull request Sep 25, 2025
symfony-splitter pushed a commit to symfony/ux-dropzone that referenced this pull request Sep 25, 2025
fabpot added a commit that referenced this pull request Sep 29, 2025
…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
This was referenced Oct 27, 2025
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.

7 participants