Skip to content

Commit f0fdf21

Browse files
authored
Fix but deprecate parameter aliases in body parameter (#2427)
1 parent 7d4a34b commit f0fdf21

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

elasticsearch/_sync/client/utils.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ def _merge_kwargs_no_duplicates(kwargs: Dict[str, Any], values: Dict[str, Any])
298298

299299
def _merge_body_fields_no_duplicates(
300300
body: _TYPE_BODY, kwargs: Dict[str, Any], body_fields: Tuple[str, ...]
301-
) -> None:
301+
) -> bool:
302+
mixed_body_and_params = False
302303
for key in list(kwargs.keys()):
303304
if key in body_fields:
304305
if isinstance(body, (str, bytes)):
@@ -315,11 +316,13 @@ def _merge_body_fields_no_duplicates(
315316
warnings.warn(
316317
f"Received '{key}' via a specific parameter in the presence of a "
317318
"'body' parameter, which is deprecated and will be removed in a future "
318-
"version. Instead, use only 'body' or only specific paremeters.",
319+
"version. Instead, use only 'body' or only specific parameters.",
319320
category=DeprecationWarning,
320321
stacklevel=warn_stacklevel(),
321322
)
322323
body[key] = kwargs.pop(key)
324+
mixed_body_and_params = True
325+
return mixed_body_and_params
323326

324327

325328
def _rewrite_parameters(
@@ -401,6 +404,7 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
401404
not ignore_deprecated_options or "body" not in ignore_deprecated_options
402405
):
403406
body: Optional[_TYPE_BODY] = kwargs.pop("body")
407+
mixed_body_and_params = False
404408
if body is not None:
405409
if body_name:
406410
if body_name in kwargs:
@@ -411,11 +415,27 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
411415
"issues/1698 for more information"
412416
)
413417
kwargs[body_name] = body
414-
415418
elif body_fields is not None:
416-
_merge_body_fields_no_duplicates(body, kwargs, body_fields)
419+
mixed_body_and_params = _merge_body_fields_no_duplicates(
420+
body, kwargs, body_fields
421+
)
417422
kwargs["body"] = body
418423

424+
if parameter_aliases and not isinstance(body, (str, bytes)):
425+
for alias, rename_to in parameter_aliases.items():
426+
if rename_to in body:
427+
body[alias] = body.pop(rename_to)
428+
# If body and params are mixed, the alias may come from a param,
429+
# in which case the warning below will not make sense.
430+
if not mixed_body_and_params:
431+
warnings.warn(
432+
f"Using '{rename_to}' alias in 'body' is deprecated and will be removed "
433+
f"in a future version of elasticsearch-py. Use '{alias}' directly instead. "
434+
"See https://github.com/elastic/elasticsearch-py/issues/1698 for more information",
435+
category=DeprecationWarning,
436+
stacklevel=2,
437+
)
438+
419439
if parameter_aliases:
420440
for alias, rename_to in parameter_aliases.items():
421441
try:

test_elasticsearch/test_client/test_rewrite_parameters.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def test_body_fields_merge(self):
191191
assert str(w[0].message) == (
192192
"Received 'source' via a specific parameter in the presence of a "
193193
"'body' parameter, which is deprecated and will be removed in a future "
194-
"version. Instead, use only 'body' or only specific paremeters."
194+
"version. Instead, use only 'body' or only specific parameters."
195195
)
196196

197197
def test_body_fields_conflict(self):
@@ -238,6 +238,41 @@ def test_parameter_aliases(self):
238238
self.wrapped_func_aliases(source=["key3"])
239239
assert self.calls[-1] == ((), {"source": ["key3"]})
240240

241+
def test_parameter_aliases_body(self):
242+
with pytest.warns(
243+
DeprecationWarning,
244+
match=(
245+
"Using 'source' alias in 'body' is deprecated and will be removed in a future version of elasticsearch-py. "
246+
"Use '_source' directly instead."
247+
),
248+
):
249+
self.wrapped_func_aliases(body={"source": ["key4"]})
250+
251+
# using the correct name does not warn
252+
with warnings.catch_warnings():
253+
warnings.simplefilter("error")
254+
self.wrapped_func_aliases(body={"_source": ["key4"]})
255+
256+
def test_parameter_aliases_body_param(self):
257+
with pytest.warns(
258+
DeprecationWarning,
259+
match=(
260+
"Received 'source' via a specific parameter in the presence of a "
261+
"'body' parameter, which is deprecated and will be removed in a future "
262+
"version. Instead, use only 'body' or only specific parameters."
263+
),
264+
):
265+
self.wrapped_func_aliases(
266+
source=["key4"], body={"query": {"match_all": {}}}
267+
)
268+
269+
# using the correct name does not warn
270+
with warnings.catch_warnings():
271+
warnings.simplefilter("error")
272+
self.wrapped_func_aliases(
273+
body={"query": {"match_all": {}}, "_source": ["key4"]}
274+
)
275+
241276
@pytest.mark.parametrize("client_cls", [Elasticsearch, AsyncElasticsearch])
242277
def test_positional_argument_error(self, client_cls):
243278
client = client_cls("https://localhost:9200")

0 commit comments

Comments
 (0)