Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ JSON encoded data
"""""""""""""""""

Matching JSON encoded data can be done with ``matchers.json_params_matcher()``.
It supports JSON dictionaries, lists, and scalar values.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The documentation for json_params_matcher() claims support for all scalar values, but the implementation incorrectly handles falsy scalars (0, False, ""), treating them as an empty dictionary {}.
Severity: MEDIUM

Suggested Fix

The implementation of json_params_matcher() should be updated to correctly handle falsy scalar values. Instead of relying on a truthiness check like param or {}, explicitly check if the parameter is None to decide whether to default to an empty dictionary. This will ensure that falsy values like 0, False, and "" are preserved and used for matching as documented.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: README.rst#L311

Potential issue: The documentation added at `README.rst:311` states that
`matchers.json_params_matcher()` supports scalar values. However, the implementation
uses a truthiness check that causes falsy scalar values like `0`, `False`, or `""` to be
coerced into an empty dictionary `{}`. Consequently, a user attempting to match a
request body containing a falsy scalar (e.g., `json_params_matcher(0)` for a body of
`"0"`) will experience a silent match failure, as the code will compare the parsed body
(`0`) against an empty dictionary (`{}`). This behavior contradicts the documentation.

Did we get this right? 👍 / 👎 to inform future reviews.


.. code-block:: python

Expand All @@ -333,6 +334,25 @@ Matching JSON encoded data can be done with ``matchers.json_params_matcher()``.
)


To perform partial matching where the request dictionary may contain additional keys, pass ``strict_match=False``:

.. code-block:: python

@responses.activate
def test_partial_json():
responses.post(
url="http://example.com/",
body="one",
match=[
matchers.json_params_matcher({"page": "first"}, strict_match=False)
],
)
resp = requests.post(
"http://example.com/",
json={"page": "first", "extra_key": "extra_val"},
)


Query Parameters Matcher
^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
Loading