Bug: using grouped dependencies with duplicate arguments causes overrides in unexpected ways #2559
Open
2 of 4 tasks
Labels
Bug 🐛
This is something that is not working as expected
Dependency Injection
This is related to our DI feature
Description
Using multiple dependencies that have the same function arguments, like in litestar-fullstack's provide_created_filter and provide_updated_filter filter dependencies, and passing in one of those arguments as query params causes overrides in seemingly unexpected ways.
For example, if you have the following dependencies:
and use them in a route handler like
then pass in the query parameters
{'valueA': 'a', 'valueB': 'b'}
, the filters will be set toFilterA(field='b', value='b')
andFilterB(field='b', value='b')
. The values here could change, as noted in the MCVE test code. Thefield
andvalue
values in the filters can be mix and matched, but always duplicated in some way between them (sofield=a field=a, value=b, value=b
,field=b field=b value=a value=a
, etc.).Using the
query
argument inParameter
in dependency function arguments doesn't seem to help, though it seems like maybe it should, as this requires you to pass in the query param via thequery
value used, so it should make them "unique". This would allow you to keep the function arguments duplicated across dependencies (which seems to be behavior that makes sense, since the dependency functions shouldn't have to care/know what other function arguments are set to).Default values when query params aren't passed in also have this behavior (as you can see in the tests). For example, in the
test_duplicated_dependency_arguments__only_passing_values__default_field_values
test,field
isn't passed in as a query param, and bothfield
values have a default value of'a'
and'b'
, respectively. When the test is run, you'll see that bothfield
values will be set to the same value ('a'
or'b'
, but never separate, as defined in their function). If you run the tests multiple times, you'll see that the order isn't deterministic. In some instances, for example, thefield
value will be'a'
for both, and others it'll be'b'
.I haven't tested this, but I assume this only happens when you're using dependencies in the way that's being done here, where like filters (e.g.
BeforeAfter
) are grouped in a list. I'm not sure if a bug ticket is correct here, or if it should be an enhancement request for Litestar to warn you when you don't have unique names for dependency arguments (maybe only when grouped in a list, if that's possible and actually the root cause here).URL to code causing the issue
https://github.com/rseeley/litestar-mcves/blob/mcve/filter-names/tests/test_filters.py
MCVE
No response
Steps to reproduce
Screenshots
No response
Logs
No response
Litestar Version
2.2.1
Platform
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh Litestar dashboard
The text was updated successfully, but these errors were encountered: