Skip to content

Commit fb4b1e0

Browse files
Fix default handling
1 parent 65d565b commit fb4b1e0

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

linodecli/api_request.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -359,35 +359,40 @@ def _build_request_body(
359359

360360
return None
361361

362-
# Merge defaults into body if applicable
363-
if ctx.defaults:
364-
parsed_args = ctx.config.update(parsed_args, operation.allowed_defaults)
365-
366362
param_names = {param.name for param in operation.params}
367363

368-
expanded_json = {}
369-
370-
body_args = [
371-
(k, v)
372-
for k, v in vars(parsed_args).items()
373-
if v is not None and k not in param_names
374-
]
364+
# Returns whether the given argument should be included in the request body
365+
def __should_include(key: str, value: Any) -> bool:
366+
return value is not None and key not in param_names
375367

376368
# If the user has specified the --raw-body argument,
377369
# return it.
378370
if ctx.raw_body is not None:
379-
if len(body_args) > 0:
371+
specified_keys = [
372+
k for k, v in vars(parsed_args).items() if __should_include(k, v)
373+
]
374+
375+
if len(specified_keys) > 0:
380376
print(
381377
"--raw-body cannot be specified with action arguments: "
382-
+ ", ".join(sorted(f"--{key}" for key, _ in body_args)),
378+
+ ", ".join(sorted(f"--{key}" for key in specified_keys)),
383379
file=sys.stderr,
384380
)
385381
sys.exit(ExitCodes.ARGUMENT_ERROR)
386382

387383
return ctx.raw_body
388384

385+
# Merge defaults into body if applicable
386+
if ctx.defaults:
387+
parsed_args = ctx.config.update(parsed_args, operation.allowed_defaults)
388+
389+
expanded_json = {}
390+
389391
# Expand dotted keys into nested dictionaries
390-
for k, v in body_args:
392+
for k, v in vars(parsed_args).items():
393+
if not __should_include(k, v):
394+
continue
395+
391396
path_segments = get_path_segments(k)
392397

393398
cur = expanded_json

tests/unit/test_api_request.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_build_request_body_non_null_field(
164164
== result
165165
)
166166

167-
def test_build_request_body(self, mock_cli, create_operation):
167+
def test_build_request_body_raw(self, mock_cli, create_operation):
168168
body = {"foo": "bar"}
169169

170170
mock_cli.raw_body = json.dumps(body)
@@ -176,7 +176,24 @@ def test_build_request_body(self, mock_cli, create_operation):
176176
)
177177
assert json.loads(result) == body
178178

179-
def test_build_request_body_conflict(
179+
def test_build_request_body_raw_with_defaults(
180+
self, mock_cli, create_operation
181+
):
182+
body = {"foo": "bar"}
183+
mock_cli.raw_body = json.dumps(body)
184+
185+
mock_cli.defaults = True
186+
mock_cli.config.get = lambda user, key, **kwargs: {"foo": "baz"}
187+
create_operation.allowed_defaults = ["foo"]
188+
189+
result = api_request._build_request_body(
190+
mock_cli,
191+
create_operation,
192+
SimpleNamespace(),
193+
)
194+
assert json.loads(result) == body
195+
196+
def test_build_request_body_raw_conflict(
180197
self, mock_cli, create_operation, capsys: CaptureFixture
181198
):
182199
mock_cli.raw_body = json.dumps({"foo": "bar"})
@@ -194,7 +211,7 @@ def test_build_request_body_conflict(
194211
in capsys.readouterr().err
195212
)
196213

197-
def test_build_request_body_get(
214+
def test_build_request_body_raw_get(
198215
self, mock_cli, list_operation, capsys: CaptureFixture
199216
):
200217
mock_cli.raw_body = json.dumps({"foo": "bar"})

0 commit comments

Comments
 (0)