Skip to content

Commit 972c068

Browse files
fix: Only use +and when filtering on multiple attributes (#503)
## 📝 Description This change alters the filter header generation logic to only use `+and` when multiple attributes are being filtered on. This is a workaround to limit the impact of inconsistencies between top-level and nested filters. Related to #500 ## ✔️ How to Test ``` make testunit ``` ## 📷 Preview **If applicable, include a screenshot or code snippet of this change. Otherwise, please remove this section.**
1 parent d360bbf commit 972c068

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

linodecli/api_request.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,15 @@ def _build_filter_header(
161161
new_filters = [{k: j} for j in v] if isinstance(v, list) else [{k: v}]
162162
filter_list.extend(new_filters)
163163

164-
if len(filter_list) > 0:
165-
return json.dumps({"+and": filter_list})
164+
if len(filter_list) < 1:
165+
return None
166166

167-
return None
167+
return json.dumps(
168+
# Only use +and if there are multiple attributes to filter on
169+
{"+and": filter_list}
170+
if len(filter_list) > 1
171+
else filter_list[0]
172+
)
168173

169174

170175
def _build_request_url(ctx, operation, parsed_args) -> str:

tests/unit/test_api_request.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,41 @@ def test_build_filter_header(self, list_operation):
125125
== result
126126
)
127127

128+
def test_build_filter_header_single(self, list_operation):
129+
result = api_request._build_filter_header(
130+
list_operation,
131+
SimpleNamespace(
132+
filterable_result="bar",
133+
),
134+
)
135+
136+
assert (
137+
json.dumps(
138+
{"filterable_result": "bar"},
139+
)
140+
== result
141+
)
142+
143+
def test_build_filter_header_single_list(self, list_operation):
144+
result = api_request._build_filter_header(
145+
list_operation,
146+
SimpleNamespace(
147+
filterable_list_result=["foo", "bar"],
148+
),
149+
)
150+
151+
assert (
152+
json.dumps(
153+
{
154+
"+and": [
155+
{"filterable_list_result": "foo"},
156+
{"filterable_list_result": "bar"},
157+
]
158+
}
159+
)
160+
== result
161+
)
162+
128163
def test_do_request_get(self, mock_cli, list_operation):
129164
mock_response = Mock(status_code=200, reason="OK")
130165

0 commit comments

Comments
 (0)