Skip to content
Draft
Show file tree
Hide file tree
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
9 changes: 1 addition & 8 deletions meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,7 @@ def send_request(
data=body,
)
else:
serialize_body = isinstance(body, dict) or body
data = (
json.dumps(body, cls=serializer)
if isinstance(body, bool) or serialize_body
else ""
if body == ""
else "null"
)
data = "" if body == "" else json.dumps(body, cls=serializer)

request = http_method(
request_path, timeout=self.config.timeout, headers=self.headers, data=data
Expand Down
44 changes: 44 additions & 0 deletions tests/client/test_http_requests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import requests

from meilisearch._httprequests import HttpRequests
Expand Down Expand Up @@ -43,3 +44,46 @@ def test_reset_content_type_header():
http.send_request(http_method=requests.get, path="health")

assert "Content-Type" not in http.headers


@pytest.mark.parametrize(
"setting", ["searchable_attributes", "displayed_attributes", "ranking_rules"]
)
def test_empty_list_setting_is_not_reset(client, empty_index, setting):
"""An empty list configures a setting; None still resets its server default."""
index = empty_index()
update = getattr(index, f"update_{setting}")
read = getattr(index, f"get_{setting}")
default = read()
assert default

task = update([])
client.wait_for_task(task.task_uid)
assert read() == []

task = update(None)
client.wait_for_task(task.task_uid)
assert read() == default


@pytest.mark.parametrize(
("body", "expected"),
[([], "[]"), ({}, "{}"), (0, "0"), (False, "false"), (None, "null"), ("", ""), (b"[]", b"[]")],
)
def test_falsey_request_bodies_keep_their_wire_value(monkeypatch, body, expected):
"""Falsey JSON values must not be confused with a reset body."""
captured = []

def post(url, **kwargs):
captured.append(kwargs["data"])
response = requests.Response()
response.status_code = 200
response._content = b"{}"
return response

with monkeypatch.context() as patch:
patch.setattr(requests, "post", post)
http = HttpRequests(Config(BASE_URL, MASTER_KEY, timeout=None))
http.post("example", body)

assert captured == [expected]