Skip to content

Fix json_params_matcher raising TypeError on nested vs scalar mismatch (#814) - #817

Closed
philsong4-ai wants to merge 1 commit into
getsentry:masterfrom
philsong4-ai:fix/814-json-params-matcher-typeerror
Closed

Fix json_params_matcher raising TypeError on nested vs scalar mismatch (#814)#817
philsong4-ai wants to merge 1 commit into
getsentry:masterfrom
philsong4-ai:fix/814-json-params-matcher-typeerror

Conversation

@philsong4-ai

Copy link
Copy Markdown

Summary

Fixes #814. json_params_matcher with strict_match=False raised TypeError: argument of type 'int' is not iterable (instead of reporting a normal mismatch) when the expected params had a scalar at a key but the actual JSON body had a nested object at the same key (and vice versa).

Root cause

responses/matchers.py::_filter_dict_recursively recursed whenever the value in the first dict was a mapping, without checking that the corresponding value in the second dict was also a mapping. The recursive call then did key in <scalar>, which raised.

Fix

Recurse only when both values are mappings:

if isinstance(val, dict) and isinstance(dict2[k], dict):
    val = _filter_dict_recursively(val, dict2[k])

A type mismatch at a key is now reported as a normal non-match (False, reason) instead of crashing.

Test

  • Added test_json_params_matcher_not_strict_scalar_vs_nested_does_not_raise in responses/tests/test_matchers.py, asserting the matcher returns (False, ...) (not raises) in both the scalar-vs-nested and nested-vs-scalar directions.
  • Full suite: 230 passed; flake8/black/isort clean.

getsentry#814)

_filtered_dict_recursively recursed whenever the value in the first dict was
a mapping, without checking that the corresponding value in the second dict was
also a mapping. When the actual JSON had a nested object but the expected
params had a scalar at the same key (e.g. expected {"page": 1}, actual
{"page": {"type": "json"}}), the recursive call did `key in <scalar>`,
raising `TypeError: argument of type 'int' is not iterable`.

Now we only recurse when BOTH values are mappings, so a type mismatch is
reported as a normal non-match (False, reason) instead of crashing.

Fixes getsentry#814.

@markstory markstory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the CHANGES file as well? You'll need to add a new section for 0.26.4

@markstory

Copy link
Copy Markdown
Member

Duplicate of #815

@markstory markstory marked this as a duplicate of #815 Sep 3, 2026
@markstory markstory closed this Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-strict json_params_matcher raises TypeError on nested object/scalar mismatch

2 participants