diff --git a/linodecli/baked/operation.py b/linodecli/baked/operation.py index 5a5ec7c0..d802d4ed 100644 --- a/linodecli/baked/operation.py +++ b/linodecli/baked/operation.py @@ -455,20 +455,31 @@ def _handle_list_items( parsed, ): # pylint: disable=too-many-locals,too-many-branches,too-many-statements lists = {} + # group list items as expected for arg_name, list_name in list_items: item_name = arg_name.split(list_name)[1][1:] + if hasattr(parsed, arg_name): val = getattr(parsed, arg_name) or [] if not val: continue + if list_name not in lists: - new_list = [{item_name: c} for c in val] - lists[list_name] = new_list - else: - update_list = lists[list_name] - for obj, item in zip(update_list, val): - obj[item_name] = item + lists[list_name] = [] + + target_list = lists[list_name] + + # If there are any additional indices not accounted for + # in the target list, add new objects accordingly. + if len(target_list) < len(val): + for _ in range(len(val) - len(target_list)): + target_list.append({}) + + # Populate each entry in the target list + # with each corresponding entry in val. + for obj, item in zip(target_list, val): + obj[item_name] = item # break out list items with periods in their name into objects. This # allows supporting nested lists diff --git a/tests/fixtures/api_request_test_foobar_post.yaml b/tests/fixtures/api_request_test_foobar_post.yaml index 5a1284ab..f1001eb8 100644 --- a/tests/fixtures/api_request_test_foobar_post.yaml +++ b/tests/fixtures/api_request_test_foobar_post.yaml @@ -97,3 +97,16 @@ components: type: number nullable: true description: An arbitrary nullable float + object_list: + type: array + description: An arbitrary list of objects. + items: + type: object + description: An arbitrary object. + properties: + field_string: + type: string + description: An arbitrary field. + field_int: + type: number + description: An arbitrary field. diff --git a/tests/unit/test_operation.py b/tests/unit/test_operation.py index 8bb9fa8a..ed047417 100644 --- a/tests/unit/test_operation.py +++ b/tests/unit/test_operation.py @@ -159,6 +159,27 @@ def test_parse_args_nullable_float(self, create_operation): result = create_operation.parse_args(["--nullable_float", "456.123"]) assert result.nullable_float == 456.123 + def test_parse_args_object_list(self, create_operation): + result = create_operation.parse_args( + [ + "--object_list.field_string", + "test1", + "--object_list.field_int", + "123", + "--object_list.field_int", + "456", + ] + ) + assert result.object_list == [ + { + "field_string": "test1", + "field_int": 123, + }, + { + "field_int": 456, + }, + ] + def test_array_arg_action_basic(self): """ Tests a basic array argument condition..