diff --git a/CHANGES b/CHANGES index 66bc5f17..b7f7ef61 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,8 @@ * Fixed `fragment_identifier_matcher` treating opaque fragments (those without ``=``, e.g. ``/users/5``) as always equal, so a required fragment matched a different one or none at all. See #806 +* Fixed non-strict `json_params_matcher` raising `TypeError` when an actual + nested object is compared with an expected scalar value. 0.26.2 ------ diff --git a/responses/matchers.py b/responses/matchers.py index cc04e693..002afac2 100644 --- a/responses/matchers.py +++ b/responses/matchers.py @@ -31,7 +31,7 @@ def _filter_dict_recursively( filtered_dict = {} for k, val in dict1.items(): if k in dict2: - if isinstance(val, dict): + if isinstance(val, dict) and isinstance(dict2[k], dict): val = _filter_dict_recursively(val, dict2[k]) filtered_dict[k] = val diff --git a/responses/tests/test_matchers.py b/responses/tests/test_matchers.py index 8eefce69..2026708f 100644 --- a/responses/tests/test_matchers.py +++ b/responses/tests/test_matchers.py @@ -210,6 +210,25 @@ def run(): assert_reset() +def test_json_params_matcher_not_strict_nested_type_mismatch(): + mock_request = Mock(body='{"page": {"type": "json"}}') + + result = matchers.json_params_matcher( + {"page": 1}, + strict_match=False, + )(mock_request) + + assert result == ( + False, + ( + "request.body doesn't match: {'page': {'type': 'json'}} " + "doesn't match {'page': 1}\n" + "Note: You use non-strict parameters check, " + "to change it use `strict_match=True`." + ), + ) + + def test_failed_matchers_dont_modify_inputs_order_in_error_message(): json_a = {"array": ["C", "B", "A"]} json_b = '{"array" : ["B", "A", "C"]}'