Problem/Motivation
The Drupal ArgumentsResolver component is unable to handle composite type hints such as string|\Stringable.
Steps to reproduce
/**
* Tests getArgument() with string|\Stringable.
*/
public function testGetArgumentStringable(): void {
$callable = function (string|\Stringable $string) {};
$string = 'Untranslated string';
$arguments = (new ArgumentsResolver([], ['string' => $string], []))->getArguments($callable);
$this->assertSame([$string], $arguments);
$stringable = new TranslatableMarkup('Translated string');
$arguments = (new ArgumentsResolver([], ['string' => $stringable], []))->getArguments($callable);
$this->assertSame([$stringable], $arguments);
}
This results in an error.
Proposed resolution
Introduce symfony/type-info component and use it in ArgumentsResolver.
Dependency Evaluation
Maintainership of the package
The package is maintained in the symfony source since Symfony 7.1
The package is maintained as part of the symfony monorepo.
Security policies of the package
The package is covered by the Symfony security process.
Expected release and support cycles
The package has been introduced as experimental in Symfony 7.1. Since Symfony 7.2 the component is stable and covered by the Symfony BC policy.
Code quality
Same as with other Symfony components.
Other dependencies it would add, if any (the full tree, not just direct dependencies), and evaluations for those dependencies as well
This package requires PHP 8.2, some psr/container implementation and symfony/deprecation-contracts. All of that is already required by core.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3548968
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
znerol commentedComment #4
znerol commentedAdding dependency evaluation section and required issue tags.
Comment #5
mstrelan commentedSeems like a good idea, just one question on the test
Comment #6
znerol commentedComment #7
znerol commentedComment #8
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #9
znerol commentedComment #10
geek-merlinReviewed the MR.
It looks good and does what it announces:
- It adds the library
- leverages it in ArgumentsResolver the way expected
- adds a test that elegantly covers both code paths, scalar and object
Yay, i waited a long time for this one.
I'd set this RTBC, but one check is missing:
- [ ] Test-only is red
For some reason i seem not to be permitted to start the test-only manual action (both ones, and yes, i'm logged in, and tried to re-login.)
So if that is checked, it's RTBC.
Comment #11
znerol commentedThanks, I kicked off the test-only pipeline (results).
Comment #12
geek-merlinGreat! Setting RTBC as of #10 plus test-only result.
The new test fails as expected.
(Still wondering why i could not trigger the test-only run myself...)
Note on the failing test: That test shows that the existing ArgumentResolver code throws a rather obscure error on union types.
Nice that that one is fixed by this MR too!
Comment #13
geek-merlinNo changes, still rtbc.
Comment #14
longwavecore/lib/Drupal/Component/Utility/composer.jsonalso needs to add the new dependency.This makes sense to me, we're going to need it sooner or later somewhere (if we don't already), and it's just another Symfony dependency to add to the pile - no issues from the release management side, so untagging for that.
Comment #15
znerol commentedComment #16
geek-merlinThe new commit adds the #14 composer.json line.
Nothing else changed, so still rtbc.
Comment #17
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #18
znerol commentedRebased.
Comment #19
nicxvan commentedWill this help with the closure type resolving?
Comment #20
znerol commentedArgumentsResolver::getArguments() works fine with any callable (closures included). This MR doesn't change that.
Comment #21
ghost of drupal pasttl;dr: symfony/type-info is probably the best bet indeed but I am very slightly worried about performance.
I have reviewed the problem instead of the branch and had the following thoughts:
So with nettle/utils it would start like this:
What this reveals is that you can't determine whether the parameter type allows/accepts the value without determining the type of the value. This might be obvious but I sure didn't catch this without writing it out. This certainly raises the question: would it make sense to store the types of objects/scalars/wildcards? The reason to do so would be performance to avoid recalculating the type of values but is that a legit concern? How much is this called anyways? And overall, what does the performance impact look like if anything?
And I am not sure whether storing value types would be useful with symfony/type-info: there is a
$type->accepts($value)API but is there a$type->allows($other_typeAPI?Comment #22
znerol commentedRequest routing relies on Symfony ArgumentResolver (see also these docs). This MR doesn't change anything about request routing.
Drupal
ArgumentsResolver(note the s) is used in access checks and in the rest module. Hence, this MR allows for composed types in access checks and rest resources.Regarding performance: Profiling of this change is indeed necessary. Access checks are not only performed when an incoming request is routed, but also when links are generated (AFAIR).
Comment #24
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.