Skip to content

Commit e1b4b8f

Browse files
committed
Pass JSON value parameter as is to the API call
1 parent 9ebee7f commit e1b4b8f

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

linodecli/api_request.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .baked.operation import (
2121
ExplicitEmptyDictValue,
2222
ExplicitEmptyListValue,
23+
ExplicitJsonValue,
2324
ExplicitNullValue,
2425
OpenAPIOperation,
2526
)
@@ -322,6 +323,10 @@ def _traverse_request_body(o: Any) -> Any:
322323
result[k] = None
323324
continue
324325

326+
if isinstance(v, ExplicitJsonValue):
327+
result[k] = v.json_value
328+
continue
329+
325330
value = _traverse_request_body(v)
326331

327332
# We should exclude implicit empty lists

linodecli/baked/operation.py

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import re
1111
import sys
1212
from collections import defaultdict
13+
from collections.abc import Callable
14+
from dataclasses import dataclass
1315
from getpass import getpass
1416
from os import environ, path
15-
from typing import Any, Dict, List, Optional, Tuple, Union
17+
from typing import Any, Dict, List, Optional, Tuple
1618
from urllib.parse import urlparse
1719

1820
import openapi3.paths
@@ -49,46 +51,12 @@ def parse_boolean(value: str) -> bool:
4951
raise argparse.ArgumentTypeError("Expected a boolean value")
5052

5153

52-
def parse_dict(
53-
value: str,
54-
) -> Union[Dict[str, Any], "ExplicitEmptyDictValue", "ExplicitEmptyListValue"]:
55-
"""
56-
A helper function to decode incoming JSON data as python dicts. This is
57-
intended to be passed to the `type=` kwarg for ArgumentParaser.add_argument.
58-
59-
:param value: The json string to be parsed into dict.
60-
:type value: str
61-
62-
:returns: The dict value of the input.
63-
:rtype: dict, ExplicitEmptyDictValue, or ExplicitEmptyListValue
64-
"""
65-
if not isinstance(value, str):
66-
raise argparse.ArgumentTypeError("Expected a JSON string")
67-
68-
try:
69-
result = json.loads(value)
70-
except Exception as e:
71-
raise argparse.ArgumentTypeError("Expected a JSON string") from e
72-
73-
# This is necessary because empty dicts and lists are excluded from requests
74-
# by default, but we still want to support user-defined empty dict
75-
# strings. This is particularly helpful when updating LKE node pool
76-
# labels and taints.
77-
if isinstance(result, dict) and result == {}:
78-
return ExplicitEmptyDictValue()
79-
80-
if isinstance(result, list) and result == []:
81-
return ExplicitEmptyListValue()
82-
83-
return result
84-
85-
8654
TYPES = {
8755
"string": str,
8856
"integer": int,
8957
"boolean": parse_boolean,
9058
"array": list,
91-
"object": parse_dict,
59+
"object": lambda value: ExplicitJsonValue(json_value=json.loads(value)),
9260
"number": float,
9361
}
9462

@@ -112,7 +80,16 @@ class ExplicitEmptyDictValue:
11280
"""
11381

11482

115-
def wrap_parse_nullable_value(arg_type: str) -> TYPES:
83+
@dataclass
84+
class ExplicitJsonValue:
85+
"""
86+
A special type used to explicitly pass raw JSON from user input as is.
87+
"""
88+
89+
json_value: Any
90+
91+
92+
def wrap_parse_nullable_value(arg_type: str) -> Callable[[Any], Any]:
11693
"""
11794
A helper function to parse `null` as None for nullable CLI args.
11895
This is intended to be called and passed to the `type=` kwarg for ArgumentParser.add_argument.

0 commit comments

Comments
 (0)