Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
aprams opened this issue Jun 21, 2022 · 1 comment · Fixed by #569
Closed

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

aprams opened this issue Jun 21, 2022 · 1 comment · Fixed by #569
Assignees
Labels

Comments

@aprams
Copy link

aprams commented Jun 21, 2022

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'

@markstory
Copy link
Member

I can provide a PR, but there are different ways to tackle this:

I would prefer to avoid adding additional dependencies. If we wanted to provide a 'diff' we could print out the mismatching sections of the tree and the path we're currently comparing 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants