Skip to content

Commit cc3fcba

Browse files
committed
Fix body fields handling with str/bytes
1 parent 16a47fe commit cc3fcba

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

elasticsearch/_sync/client/utils.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
str, Sequence[Union[str, Mapping[str, Union[str, int]], NodeConfig]]
7575
]
7676

77+
_TYPE_BODY = Union[bytes, str, Dict[str, Any]]
78+
7779
_TYPE_ASYNC_SNIFF_CALLBACK = Callable[
7880
[AsyncTransport, SniffOptions], Awaitable[List[NodeConfig]]
7981
]
@@ -295,15 +297,21 @@ def _merge_kwargs_no_duplicates(kwargs: Dict[str, Any], values: Dict[str, Any])
295297

296298

297299
def _merge_body_fields_no_duplicates(
298-
body: Dict[str, Any], kwargs: Dict[str, Any], body_fields: Tuple[str, ...]
300+
body: _TYPE_BODY, kwargs: Dict[str, Any], body_fields: Tuple[str, ...]
299301
) -> None:
300302
for key in list(kwargs.keys()):
301-
if key in body:
302-
raise ValueError(
303-
f"Received multiple values for '{key}', specify parameters "
304-
"using either body or parameters, not both."
305-
)
306303
if key in body_fields:
304+
if isinstance(body, (str, bytes)):
305+
raise ValueError(
306+
"Couldn't merge 'body' with other parameters as it wasn't a mapping."
307+
)
308+
309+
if key in body:
310+
raise ValueError(
311+
f"Received multiple values for '{key}', specify parameters "
312+
"using either body or parameters, not both."
313+
)
314+
307315
warnings.warn(
308316
f"Received '{key}' via a specific parameter in the presence of a "
309317
"'body' parameter, which is deprecated and will be removed in a future "
@@ -392,7 +400,7 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
392400
if "body" in kwargs and (
393401
not ignore_deprecated_options or "body" not in ignore_deprecated_options
394402
):
395-
body = kwargs.pop("body")
403+
body: Optional[_TYPE_BODY] = kwargs.pop("body")
396404
if body is not None:
397405
if body_name:
398406
if body_name in kwargs:
@@ -405,11 +413,6 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
405413
kwargs[body_name] = body
406414

407415
elif body_fields is not None:
408-
if not hasattr(body, "items"):
409-
raise ValueError(
410-
"Couldn't merge 'body' with other parameters as it wasn't a mapping. "
411-
"Instead of using 'body' use individual API parameters"
412-
)
413416
_merge_body_fields_no_duplicates(body, kwargs, body_fields)
414417
kwargs["body"] = body
415418

test_elasticsearch/test_client/test_rewrite_parameters.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,17 @@ def test_body_fields(self):
158158
]
159159

160160
@pytest.mark.parametrize(
161-
"body", ['{"query": {"match_all": {}}}', b'{"query": {"match_all": {}}}']
161+
"body, kwargs",
162+
[
163+
('{"query": {"match_all": {}}}', {"query": {"match_all": {}}}),
164+
(b'{"query": {"match_all": {}}}', {"query": {"match_all": {}}}),
165+
],
162166
)
163-
def test_error_on_body_merge(self, body):
167+
def test_error_on_body_merge(self, body, kwargs):
164168
with pytest.raises(ValueError) as e:
165-
self.wrapped_func_body_fields(body=body)
169+
self.wrapped_func_body_fields(body=body, **kwargs)
166170
assert str(e.value) == (
167-
"Couldn't merge 'body' with other parameters as it wasn't a mapping. Instead of "
168-
"using 'body' use individual API parameters"
171+
"Couldn't merge 'body' with other parameters as it wasn't a mapping."
169172
)
170173

171174
@pytest.mark.parametrize(
@@ -186,7 +189,7 @@ def test_body_fields_merge(self):
186189
assert len(w) == 1
187190
assert w[0].category == DeprecationWarning
188191
assert str(w[0].message) == (
189-
f"Received 'source' via a specific parameter in the presence of a "
192+
"Received 'source' via a specific parameter in the presence of a "
190193
"'body' parameter, which is deprecated and will be removed in a future "
191194
"version. Instead, use only 'body' or only specific paremeters."
192195
)

0 commit comments

Comments
 (0)