Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
The Drupal\Tests\media_library\Kernel\MediaLibraryStateTest fails for Symfony 5.4 with the following deprecation warning:
16x: Since symfony/http-foundation 5.1: Passing a non-scalar value as 2nd argument to "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, pass a scalar or null instead.
14x in MediaLibraryStateTest::testFromRequest from Drupal\Tests\media_library\Kernel
2x in MediaLibraryStateTest::testFromRequestQueryLess from Drupal\Tests\media_library\Kernel
Proposed resolution
Update the class Drupal\media_library\MediaLibraryState.
Remaining tasks
TBD
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
TBD
Comment | File | Size | Author |
---|---|---|---|
#17 | 3248454-17.patch | 3.31 KB | Spokje |
#17 | interdiff_12-17.txt | 1.02 KB | Spokje |
#12 | 3248454-12.patch | 3.15 KB | longwave |
Comments
Comment #2
daffie CreditAttribution: daffie commentedThe fix.
Comment #3
larowlanIsn't the default NULL? If so can we just use ?: Instead of calling ::all?
Comment #4
daffie CreditAttribution: daffie commentedThe problem is that the method
get()
needs to return a string, int or float value (only scalar values) and the methodall()
only whats to return array values. Drupal likes to mix array values with string values and therefor we cannot use the methodget()
any more. The key is to use the methodall()
to get all values and then to select the value you need:all()['media_library_opener_parameters']
. It then works for string values as for array values. The methodall()
does not have a default return value. See: https://www.drupal.org/node/3213138Comment #5
larowlanSorry, I was asking is there a reason we can't write this as
And avoid calling ::all
But I get it now, its not about the 'null' case its about the 'value exists' case
should we store the return from the first call to :all and avoid calling it twice?
In fact, could we re-write this using one call to :all and array_intersect_key and remove the ::get calls too?
Comment #6
daffie CreditAttribution: daffie commentedGood idea and fixed.
Comment #8
daffie CreditAttribution: daffie commentedThe default value for the method Symfony\Component\HttpFoundation\ParameterBag::get() is NULL and not an empty string as used in the previous patch. See: https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/HttpFo...
Comment #9
larowlanLooks good to me
Comment #10
longwaveThe reason we use
all()
instead ofall('key')
orget('key')
is when what we are retrieving really could either be a scalar or an array, and subsequent code is set up to handle both cases. Is it really the case that we don't know this for each of these values? Surely e.g. themedia_library_opener_id
can never be an array?Comment #11
longwaveThis was solved a cleaner way back in https://git.drupalcode.org/project/drupal/-/merge_requests/462/diffs#dif... - this was then reverted for other reasons but I think we can reintroduce the same fix for MediaLibraryState at least.
Comment #12
longwaveAlternative approach that uses
all()
andget()
as Symfony intends, this provides extra safety against a malicious user sending an array instead of a string or vice versa.The InputBag replacement in the test is done at runtime in bootstrap.php, this will be removed when we no longer need to support both versions of Symfony.
Comment #13
daffie CreditAttribution: daffie commentedI am giving this current patch a RTBC, because it is a correct solution.
The solution from comment #12 looks like it is copying code from Symfony and it is needing a temporary fix in the test MediaLibraryStateTest.
Personally I like the solution from comment #8 more. To me, it is more in line with how Symfony thinks it should be used.
I leave it up to the committer to decide which solution should land.
Comment #14
alexpottWe should add an @todo to remove this. I agree with @longwave - I think using all(PARAM_NAME) when we expect an array and get(PARAM_NAME) when we expect a scalar is how this is intended to be used.
I would be great to have an InputBag clean up issue that we can link to.
This could link to the inputbag clean up issue.
Comment #15
catchInputbag clean-up issue should probably be this one #3162981: [Symfony 6] Use Symfony\Component\HttpFoundation\InputBag where appropriate instead of ParameterBag.
Comment #16
catchAdding #3254250: [Symfony 6] Retrieving a non-string value from "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated to related issues, was previously duplicating the fix here.
Comment #17
SpokjeTried to address #14.1 and .2.
Comment #18
daffie CreditAttribution: daffie commentedThe points of @alexpott from comment #14 are addressed.
Back to RTBC.
@Spokje: Thank you for working on this!
Comment #21
catchCommitted/pushed to 10.0.x, also cherry-picked to 9.4.x to keep the tests in sync.