Skip to content

Commit 2f1bb7c

Browse files
Fixed improperly nested engine_config arguments in Postgres DB creation (#778)
1 parent 79f71db commit 2f1bb7c

File tree

5 files changed

+104
-16
lines changed

5 files changed

+104
-16
lines changed

linodecli/api_request.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ExplicitNullValue,
2424
OpenAPIOperation,
2525
)
26+
from .baked.util import get_path_segments
2627
from .helpers import handle_url_overrides
2728

2829
if TYPE_CHECKING:
@@ -364,13 +365,15 @@ def _build_request_body(
364365
if v is None or k in param_names:
365366
continue
366367

368+
path_segments = get_path_segments(k)
369+
367370
cur = expanded_json
368-
for part in k.split(".")[:-1]:
371+
for part in path_segments[:-1]:
369372
if part not in cur:
370373
cur[part] = {}
371374
cur = cur[part]
372375

373-
cur[k.split(".")[-1]] = v
376+
cur[path_segments[-1]] = v
374377

375378
return json.dumps(_traverse_request_body(expanded_json))
376379

linodecli/baked/operation.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
OpenAPIRequestArg,
2626
)
2727
from linodecli.baked.response import OpenAPIResponse
28+
from linodecli.baked.util import unescape_arg_segment
2829
from linodecli.exit_codes import ExitCodes
2930
from linodecli.output.output_handler import OutputHandler
3031
from linodecli.overrides import OUTPUT_OVERRIDES
@@ -649,6 +650,9 @@ def _add_args_post_put(
649650
if arg.read_only:
650651
continue
651652

653+
arg_name_unescaped = unescape_arg_segment(arg.name)
654+
arg_path_unescaped = unescape_arg_segment(arg.path)
655+
652656
arg_type = (
653657
arg.item_type if arg.datatype == "array" else arg.datatype
654658
)
@@ -660,15 +664,17 @@ def _add_args_post_put(
660664
if arg.datatype == "array":
661665
# special handling for input arrays
662666
parser.add_argument(
663-
"--" + arg.path,
664-
metavar=arg.name,
667+
"--" + arg_path_unescaped,
668+
dest=arg.path,
669+
metavar=arg_name_unescaped,
665670
action=ArrayAction,
666671
type=arg_type_handler,
667672
)
668673
elif arg.is_child:
669674
parser.add_argument(
670-
"--" + arg.path,
671-
metavar=arg.name,
675+
"--" + arg_path_unescaped,
676+
dest=arg.path,
677+
metavar=arg_name_unescaped,
672678
action=ListArgumentAction,
673679
type=arg_type_handler,
674680
)
@@ -677,7 +683,7 @@ def _add_args_post_put(
677683
if arg.datatype == "string" and arg.format == "password":
678684
# special case - password input
679685
parser.add_argument(
680-
"--" + arg.path,
686+
"--" + arg_path_unescaped,
681687
nargs="?",
682688
action=PasswordPromptAction,
683689
)
@@ -687,15 +693,17 @@ def _add_args_post_put(
687693
"ssl-key",
688694
):
689695
parser.add_argument(
690-
"--" + arg.path,
691-
metavar=arg.name,
696+
"--" + arg_path_unescaped,
697+
dest=arg.path,
698+
metavar=arg_name_unescaped,
692699
action=OptionalFromFileAction,
693700
type=arg_type_handler,
694701
)
695702
else:
696703
parser.add_argument(
697-
"--" + arg.path,
698-
metavar=arg.name,
704+
"--" + arg_path_unescaped,
705+
dest=arg.path,
706+
metavar=arg_name_unescaped,
699707
type=arg_type_handler,
700708
)
701709

linodecli/baked/request.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
from linodecli.baked.parsing import simplify_description
1111
from linodecli.baked.response import OpenAPIResponse
12-
from linodecli.baked.util import _aggregate_schema_properties
12+
from linodecli.baked.util import (
13+
_aggregate_schema_properties,
14+
escape_arg_segment,
15+
)
1316

1417

1518
class OpenAPIRequestArg:
@@ -152,6 +155,8 @@ def _parse_request_model(
152155
return args
153156

154157
for k, v in properties.items():
158+
k = escape_arg_segment(k)
159+
155160
# Handle nested objects which aren't read-only and have properties
156161
if (
157162
v.type == "object"

linodecli/baked/util.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Provides various utility functions for use in baking logic.
33
"""
44

5+
import re
56
from collections import defaultdict
6-
from typing import Any, Dict, Set, Tuple
7+
from typing import Any, Dict, List, Set, Tuple
78

89
from openapi3.schemas import Schema
910

@@ -51,3 +52,58 @@ def _handle_schema(_schema: Schema):
5152
# We only want to mark fields that are required by ALL subschema as required
5253
set(key for key, count in required.items() if count == schema_count),
5354
)
55+
56+
57+
ESCAPED_PATH_DELIMITER_PATTERN = re.compile(r"(?<!\\)\.")
58+
59+
60+
def escape_arg_segment(segment: str) -> str:
61+
"""
62+
Escapes periods in a segment by prefixing them with a backslash.
63+
64+
:param segment: The input string segment to escape.
65+
:return: The escaped segment with periods replaced by '\\.'.
66+
"""
67+
return segment.replace(".", "\\.")
68+
69+
70+
def unescape_arg_segment(segment: str) -> str:
71+
"""
72+
Reverses the escaping of periods in a segment, turning '\\.' back into '.'.
73+
74+
:param segment: The input string segment to unescape.
75+
:return: The unescaped segment with '\\.' replaced by '.'.
76+
"""
77+
return segment.replace("\\.", ".")
78+
79+
80+
def get_path_segments(path: str) -> List[str]:
81+
"""
82+
Splits a path string into segments using a delimiter pattern,
83+
and unescapes any escaped delimiters in the resulting segments.
84+
85+
:param path: The full path string to split and unescape.
86+
:return: A list of unescaped path segments.
87+
"""
88+
return [
89+
unescape_arg_segment(seg)
90+
for seg in ESCAPED_PATH_DELIMITER_PATTERN.split(path)
91+
]
92+
93+
94+
def get_terminal_keys(data: Dict[str, Any]) -> List[str]:
95+
"""
96+
Recursively retrieves all terminal (non-dict) keys from a nested dictionary.
97+
98+
:param data: The input dictionary, possibly nested.
99+
:return: A list of all terminal keys (keys whose values are not dictionaries).
100+
"""
101+
ret = []
102+
103+
for k, v in data.items():
104+
if isinstance(v, dict):
105+
ret.extend(get_terminal_keys(v)) # recurse into nested dicts
106+
else:
107+
ret.append(k) # terminal key
108+
109+
return ret

linodecli/output/output_handler.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from rich.table import Column, Table
1717

1818
from linodecli.baked.response import OpenAPIResponse, OpenAPIResponseAttr
19+
from linodecli.baked.util import get_terminal_keys
1920

2021

2122
class OutputMode(Enum):
@@ -328,15 +329,30 @@ def _json_output(self, header, data, to):
328329
Prints data in JSON format
329330
"""
330331
# Special handling for JSON headers.
331-
# We're only interested in the last part of the column name.
332-
header = [v.split(".")[-1] for v in header]
332+
# We're only interested in the last part of the column name, unless the last
333+
# part is a dotted key. If the last part is a dotted key, include the entire dotted key.
333334

334335
content = []
335336
if len(data) and isinstance(data[0], dict): # we got delimited json in
337+
parsed_header = []
338+
terminal_keys = get_terminal_keys(data[0])
339+
340+
for v in header:
341+
parts = v.split(".")
342+
if (
343+
len(parts) >= 2
344+
and ".".join([parts[-2], parts[-1]]) in terminal_keys
345+
):
346+
parsed_header.append(".".join([parts[-2], parts[-1]]))
347+
else:
348+
parsed_header.append(parts[-1])
349+
336350
# parse down to the value we display
337351
for row in data:
338-
content.append(self._select_json_elements(header, row))
352+
content.append(self._select_json_elements(parsed_header, row))
339353
else: # this is a list
354+
header = [v.split(".")[-1] for v in header]
355+
340356
for row in data:
341357
content.append(dict(zip(header, row)))
342358

0 commit comments

Comments
 (0)