Repository navigation
fix: match sequence-valued params in query_param_matcher - #816
dylanpulver wants to merge 4 commits into
Conversation
3c2cb47 to
0c371a5
Compare
| values = [ | ||
| str(item) if isinstance(item, (int, float)) else item for item in value | ||
| ] | ||
| return values[0] if len(values) == 1 else values |
There was a problem hiding this comment.
Don't you also need to recurse into nested structures? Some one could make multi-level query parameter string data that won't have the same normalization applied?
d86b2a8 to
717cb62
Compare
| {"ids": ["a", 2], "page": 1}, | ||
| {"ids": [1, 2], "attrs": {"counter": 3, "favorites": [1, 2]}}, | ||
| {"ids": [1]}, | ||
| {"ids": ("a",)}, |
There was a problem hiding this comment.
Should we also have a case for
| {"ids": ("a",)}, | |
| {"ids": None}, |
Currently this fails with
E Request:
E - GET https://example.com/
E
E Available matches:
E - GET https://example.com/ Parameters do not match. {} doesn't match {'ids': None}
a4a1f30 to
4002c10
Compare
|
There is still outstanding feedback on missed edge cases that needs to be addressed. |
query_param_matcher converted int/float values to strings only at the top
level of the params mapping, so a list- or tuple-valued param never matched.
responses parses the request's query string into strings and groups a
repeated key into a list (collapsing a key seen once to a bare value), so
{"ids": [1, 2]} was compared against {'ids': ['1', '2']} and failed, as did
any tuple and any single-item sequence.
Normalize sequence values the same way the request parser produces them.
An empty sequence still does not match a request that omits the key.
requests builds the query string by iterating a sequence value once and then
handing the result to urlencode(..., doseq=True), which splices a sequence
nested inside that value in one further level. It also drops a None element.
So {"ids": [[1, 2], [3]]} is sent as ids=1&ids=2&ids=3 and {"ids": [1, None]}
as ids=1, neither of which the matcher's expectation reproduced.
Mirror both rules. Nesting deeper than one level is url-encoded by requests as
its str(), and a dict value is emitted by requests as its keys alone (the
values never reach the wire), so neither is normalized here.
Co-Authored-By: Claude <noreply@anthropic.com>
requests hands params to urlencode(..., doseq=True), which iterates the
value. Iterating a mapping yields its keys, so {"attrs": {"counter": 3}}
goes out as attrs=counter and the values never reach the query string.
_normalize left a mapping alone, so the expectation kept the mapping
while the request carried its keys and the two could not match. A
mapping is now treated as the sequence of its keys, at the top level and
nested inside a sequence, which is what requests does with it.
4002c10 to
aa6550c
Compare
Review asked whether `{"ids": None}` should match, and it should: `requests`
omits a `None`-valued key from the query string outright rather than sending it
empty, so the matcher has to drop it too. The same holds for an empty sequence,
a sequence of nothing but `None`, and an empty mapping. Emptiness here is not
truthiness, since `""` is sent as `a=` and `0` and `False` are sent as
themselves.
`_stringify` now renders every value the way it comes back out of the query
string. `bytes` are decoded rather than `str()`-ed, because `urlencode`
percent-encodes the bytes and `b"x"` returns as `"x"`. That also fixes the level
below a spliced sequence, where anything left is sent as its `str()`.
On recursing further: `requests` iterates a sequence value once and hands the
result to `urlencode(doseq=True)`, which splices one more level and `str()`s
whatever is below. Recursing past that would make the matcher accept shapes
`requests` cannot send, so the tests pin the boundary instead.
Note the asymmetry those two levels have: `requests` drops a `None` in the
sequence it iterates, while `urlencode` renders a `None` one level deeper as the
string "None".
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo
|
All three addressed in
So the matcher now drops a key whose value yields nothing, and emptiness is deliberately not truthiness: On recursing into nested structures: it would be wrong to go deeper, and that is worth pinning. Recursing past that would make the matcher accept shapes Both are now covered, so the boundary is tested rather than assumed. Your two suggested cases. Numbers: Written with AI assistance (Claude Opus 5). Every arrow above is from a local |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit f5ce59d. Configure here.
| if isinstance(item, str): | ||
| return item | ||
| if isinstance(item, bytes): | ||
| return item.decode() |
There was a problem hiding this comment.
Non-UTF-8 bytes crash the matcher
Low Severity
_stringify decodes bytes with strict UTF-8, so a non-UTF-8 sequence value raises UnicodeDecodeError while the matcher is being built. parse_qsl uses errors='replace' on the same bytes after urlencode, so a real request would parse instead of crashing.
Reviewed by Cursor Bugbot for commit f5ce59d. Configure here.


What type of PR is this? (check all applicable)
Description
query_param_matcherstringifiedint/floatvalues only at the top level of themapping, so a sequence-valued param never matched:
responsesparses the request's query string into strings and groups a repeated keyinto a list, collapsing a key seen once to a bare value, so the matcher compared
{"ids": [1, 2]}against{'ids': ['1', '2']}. Tuples and single-item sequencesfailed the same way even with string items. Sequences are now normalized the way that
parser produces them.
Deliberately unchanged: an empty sequence, and
None, still fail to match a requestthat omits the key.
requestsdrops both, but dropping the key here would also changewhat
strict_match=Falsefilters on.Checked with an oracle that reuses one dict for both sides across 78 param shapes and
never reads
matchers.py: 56 failures on master, 2 after, both the empty-sequence caseabove. Reverting only
matchers.pyfails all 5 new cases, so none of them is a pin.Suite goes 233 → 238 passed; coverage and
mypyfailure sets are identical to master.Related Issues
PR checklist
Before submitting this pull request, I have done the following:
toxandpre-commitchecks locally —pre-commit run(all hooks, pinnedversions) is green and both
mypyinvocations report the same errors as cleanmaster; pytest ran on 3.13 only, not the full tox matrix.
Added/updated tests?
Disclosure: prepared with AI assistance (Claude Code, Claude Opus 5,
claude-opus-5). I have reviewed the change and the measurements above.