Skip to content

Commit

Permalink
fix: Resolve issue when not specifying all keys for object list entri…
Browse files Browse the repository at this point in the history
…es (#569)
  • Loading branch information
lgarber-akamai authored Jan 16, 2024
1 parent 95b49eb commit e6db9c2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
23 changes: 17 additions & 6 deletions linodecli/baked/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/api_request_test_foobar_post.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
21 changes: 21 additions & 0 deletions tests/unit/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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..
Expand Down

0 comments on commit e6db9c2

Please sign in to comment.