Skip to content

json_params_matcher does not support general JSONs (e.g. lists of dicts) #559

@aprams

Description

@aprams

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'

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions