Skip to content

Commit e6db9c2

Browse files
fix: Resolve issue when not specifying all keys for object list entries (#569)
1 parent 95b49eb commit e6db9c2

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

linodecli/baked/operation.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,20 +455,31 @@ def _handle_list_items(
455455
parsed,
456456
): # pylint: disable=too-many-locals,too-many-branches,too-many-statements
457457
lists = {}
458+
458459
# group list items as expected
459460
for arg_name, list_name in list_items:
460461
item_name = arg_name.split(list_name)[1][1:]
462+
461463
if hasattr(parsed, arg_name):
462464
val = getattr(parsed, arg_name) or []
463465
if not val:
464466
continue
467+
465468
if list_name not in lists:
466-
new_list = [{item_name: c} for c in val]
467-
lists[list_name] = new_list
468-
else:
469-
update_list = lists[list_name]
470-
for obj, item in zip(update_list, val):
471-
obj[item_name] = item
469+
lists[list_name] = []
470+
471+
target_list = lists[list_name]
472+
473+
# If there are any additional indices not accounted for
474+
# in the target list, add new objects accordingly.
475+
if len(target_list) < len(val):
476+
for _ in range(len(val) - len(target_list)):
477+
target_list.append({})
478+
479+
# Populate each entry in the target list
480+
# with each corresponding entry in val.
481+
for obj, item in zip(target_list, val):
482+
obj[item_name] = item
472483

473484
# break out list items with periods in their name into objects. This
474485
# allows supporting nested lists

tests/fixtures/api_request_test_foobar_post.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,16 @@ components:
9797
type: number
9898
nullable: true
9999
description: An arbitrary nullable float
100+
object_list:
101+
type: array
102+
description: An arbitrary list of objects.
103+
items:
104+
type: object
105+
description: An arbitrary object.
106+
properties:
107+
field_string:
108+
type: string
109+
description: An arbitrary field.
110+
field_int:
111+
type: number
112+
description: An arbitrary field.

tests/unit/test_operation.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,27 @@ def test_parse_args_nullable_float(self, create_operation):
159159
result = create_operation.parse_args(["--nullable_float", "456.123"])
160160
assert result.nullable_float == 456.123
161161

162+
def test_parse_args_object_list(self, create_operation):
163+
result = create_operation.parse_args(
164+
[
165+
"--object_list.field_string",
166+
"test1",
167+
"--object_list.field_int",
168+
"123",
169+
"--object_list.field_int",
170+
"456",
171+
]
172+
)
173+
assert result.object_list == [
174+
{
175+
"field_string": "test1",
176+
"field_int": 123,
177+
},
178+
{
179+
"field_int": 456,
180+
},
181+
]
182+
162183
def test_array_arg_action_basic(self):
163184
"""
164185
Tests a basic array argument condition..

0 commit comments

Comments
 (0)