diff --git a/rest_tools/server/arghandler.py b/rest_tools/server/arghandler.py index 9fac7160..568d94dc 100644 --- a/rest_tools/server/arghandler.py +++ b/rest_tools/server/arghandler.py @@ -10,7 +10,7 @@ import sys import time import traceback -from typing import Any, List, TypeVar, Union +from typing import Any, List, TypeVar, Union, cast import tornado.web from tornado.escape import to_unicode @@ -76,12 +76,13 @@ def retrieve_json_body_arg(parsed_val: Any) -> Any: if parsed_val != USE_CACHED_VALUE_PLACEHOLDER: return parsed_val # this must be the **default** value # replace placeholder value with actual value + key = cast(str, name or kwargs.get("dest")) try: - return self.rest_handler.json_body_arguments[name] + return self.rest_handler.json_body_arguments[key] except KeyError as e: # just in case, intercept so the user doesn't get a 400 raise RuntimeError( - f"key '{name}' should exist in json body arguments" + f"key '{key}' should exist in json body arguments" ) from e # TYPE diff --git a/tests/unit_server/arghandler_test.py b/tests/unit_server/arghandler_test.py index 19b092b5..5e4bf5f4 100644 --- a/tests/unit_server/arghandler_test.py +++ b/tests/unit_server/arghandler_test.py @@ -388,3 +388,44 @@ def test_141__extra_argument_with_duplicates(argument_source: str) -> None: with pytest.raises(tornado.web.HTTPError) as e: arghand.parse_args() assert str(e.value) == "HTTP 400: unrecognized arguments: xtra, another" + + +@pytest.mark.parametrize( + "argument_source", + [QUERY_ARGUMENTS, JSON_BODY_ARGUMENTS], +) +def test_200__argparse_dest(argument_source: str) -> None: + """Test `argument_source` arguments using argparse's advanced options.""" + args: Dict[str, Any] = { + "old_name": "-10", + "bar": "True", + } + if argument_source == JSON_BODY_ARGUMENTS: + args.update( + { + "dicto": {"abc": 123}, + "listo": [1, 2, 3], + } + ) + + # set up ArgumentHandler + arghand = setup_argument_handler(argument_source, args) + + for arg, _ in args.items(): + print() + print(arg) + if arg == "old_name": + arghand.add_argument("new_name") + else: + arghand.add_argument(arg) + outargs = arghand.parse_args() + + # grab each + for arg, val in args.items(): + print() + print(arg) + print(val) + if arg == "old_name": + assert val == getattr(outargs, "new_name") + else: + assert val == getattr(outargs, arg.replace("-", "_"))