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:
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'
Describe the bug
The implementation of
json_params_matcherdoes 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_bodyis 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:
As this might entail additional dependencies, I'd like to get your opinion on this before submitting a PR.
Version of
responses0.21.0
Steps to Reproduce
Expected Result
resultshould be (False, ""), no error is thrownActual Result
AttributeError is raised: 'list' object has no attribute 'keys'