-
-
Notifications
You must be signed in to change notification settings - Fork 360
Description
Describe the bug
The implementation of json_params_matcher does not support arbitrarily nested JSON objects, such as lists of dicts.
It says so in the signature:
def json_params_matcher(params: Optional[Dict[str, Any]]) -> Callable[..., Any]:
and the simple check of params_dict == json_body is not enough. Additionally, an Exception is raised in this section, as a dict is expected.
if not valid: reason = "request.body doesn't match: {} doesn't match {}".format( _create_key_val_str(json_body), _create_key_val_str(params_dict) )
Additional context
I can provide a PR, but there are different ways to tackle this:
- Custom function to recursively check => no dependencies
- Adopting something like https://github.com/seperman/deepdiff or https://github.com/xlwings/jsondiff to support with the extraction of reasons for JSONs being not equal
As this might entail additional dependencies, I'd like to get your opinion on this before submitting a PR.
Version of responses
0.21.0
Steps to Reproduce
import json
from unittest.mock import Mock
from responses.matchers import _create_key_val_str, json_params_matcher
json_a = [{"a": "b"}]
json_b = [{"a": "b", "c": "d"}]
mock_request = Mock(body=json.dumps(json_b))
result = json_params_matcher(json_a)(mock_request)
print(result)Expected Result
result should be (False, ""), no error is thrown
Actual Result
AttributeError is raised: 'list' object has no attribute 'keys'