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.
Symfony\Component\HttpFoundation\ParameterBag supports nested query parameters, like ?foo[bar][baz]
.
For a query string like ?foo[bar]=baz
calling ParameterBag->get('foo');
will return array('bar' => 'baz)
. This will cause a PHP notice when retrieving the cache context specific to 'foo' since QueryArgsCacheContext::getContext is expected to return a string and will instead return an array.
Comment | File | Size | Author |
---|---|---|---|
#13 | interdiff-2746761-11-13.txt | 1.79 KB | gabesullice |
#13 | queryargscache_context-2746761-13.patch | 1.68 KB | gabesullice |
#11 | interdiff-2746761-10-11.txt | 835 bytes | gabesullice |
#11 | queryargscache_context-2746761-11.patch | 1.68 KB | gabesullice |
#10 | interdiff-2746761-9-10.txt | 1.17 KB | gabesullice |
Comments
Comment #2
gabesulliceThe attached patch eliminates this notice.
This patch also lets one specify 'foo[bar]' as the query arg to
getContext
. This would mean the following query string would be seen as the same as far as caching is concerned.If the query arg were simply 'foo' then:
Comment #3
gabesulliceAdded another test.
Comment #4
gabesulliceComment #5
Wim LeersThis is a bug, and should be fixed in 8.1. Thanks for filing this and working on it!
This is not ideal. #2738563: Headers cache context does not work, also needs test coverage has code to deal with nested parameters. This can use the exact same code. I think that'd be better.
Comment #6
Wim LeersComment #7
gabesullice@Wim Leers thanks for the review!
I took a deeper look at
ParameterBag::get()
and found that the 'deep' argument will be deprecated, so I've removed that from this patch.I also took a look at the HeaderCacheContext code, but I'm not sure it applies as cleanly as could be hoped. A header can have multiple values, but as far as I can tell, it cannot have nested values. I.E. one can't have an array of arrays as is the case with query parameters. For example, if one were to have the querystring
foo[bar][baz]=nested1&foo[bar][zap]=nested2
thenget('foo')
would return:['bar' => ['baz' => 'nested1, 'zap' => 'nested2']]
.I've updated the serialization to be 'human-readable' by using json_encode instead of serialize and md5. I didn't use the HeaderCacheContext code because I couldn't think of a good way to implement it recursively any more efficiently than json_encode can do for use 'out-of-the-box'.
Comment #8
gabesulliceComment #9
gabesulliceComment #10
gabesulliceSince we're not doing deep parameters, I reverted most of the function back to its original code.
Comment #11
gabesulliceCode standards
Comment #12
Wim LeersWouldn't
http_build_query()
be a better fit for this?Comment #13
gabesulliceDerp.
Comment #14
Wim LeersPerfect!
Nice to see this is able to incrementally improve upon #2729439: QueryArgsCacheContext should return a special value for ?arg (without value), which was where test coverage for this class was first introduced.
Also impressive how we first delegated to Symfony's logic, and now actually have to do a lot of gymnastics on top of it. I clearly failed to think the edge cases through originally.
Comment #15
alexpottCommitted 33cd4e3 and pushed to 8.1.x and 8.2.x. Thanks!