Allow JSON boolean when parsing query filters

This will avoid having to explicitly transform booleans in the client when building the query
This commit is contained in:
davelopez
2022-04-05 17:26:11 +02:00
parent 38d687747a
commit 356ef0fae3
5 changed files with 11 additions and 21 deletions
+1 -11
View File
@@ -47,21 +47,11 @@ function buildLegacyQueryStringFrom(filters) {
function buildLegacyParam(fields = {}) {
return Object.keys(fields).reduce((result, key) => {
result[key] = pythonBooleanFormat(fields[key]);
result[key] = fields[key];
return result;
}, {});
}
function pythonBooleanFormat(val) {
if (val === true) {
return "True";
}
if (val === false) {
return "False";
}
return val;
}
/**
* Some of the current endpoints don't accept JSON, so we need to
* do some massaging to send in old form post data.
@@ -20,9 +20,9 @@ function toLower(value) {
return String(value).toLowerCase();
}
/** Converts user input to python boolean. */
function toBoolPython(value) {
return toLower(value) == "true" ? "True" : "False";
/** Converts user input to boolean. */
function toBool(value) {
return toLower(value) == "true";
}
/** Converts user input to lower case and strips quotation marks. */
@@ -97,14 +97,14 @@ const validFilters = {
create_time_gt: compare("create_time", "gt", toDate),
create_time_le: compare("create_time", "le", toDate),
create_time_lt: compare("create_time", "lt", toDate),
deleted: equals("deleted", "deleted", toBoolPython),
deleted: equals("deleted", "deleted", toBool),
extension: equals("extension"),
hid: equals("hid"),
hid_ge: compare("hid", "ge"),
hid_gt: compare("hid", "gt"),
hid_le: compare("hid", "le"),
hid_lt: compare("hid", "lt"),
visible: equals("visible", "visible", toBoolPython),
visible: equals("visible", "visible", toBool),
name: contains("name"),
state: equals("state"),
tag: contains("tags", "tag"),
@@ -55,8 +55,8 @@ describe("historyItemsFiltering", () => {
expect(queryDict["state-eq"]).toBe("success");
expect(queryDict["extension-eq"]).toBe("ext");
expect(queryDict["tag"]).toBe("first");
expect(queryDict["deleted"]).toBe("False");
expect(queryDict["visible"]).toBe("True");
expect(queryDict["deleted"]).toBe(false);
expect(queryDict["visible"]).toBe(true);
});
});
test("validate filtering of a history item", () => {
+2 -2
View File
@@ -1252,9 +1252,9 @@ def parse_bool(bool_string: Union[str, bool]) -> bool:
Parse a boolean from a string.
"""
# Be strict here to remove complexity of options (but allow already parsed).
if bool_string in ("True", True):
if bool_string in ("True", "true", True):
return True
if bool_string in ("False", False):
if bool_string in ("False", "false", False):
return False
raise ValueError(f"invalid boolean: {bool_string}")
@@ -748,7 +748,7 @@ class HistoryFiltersTestCase(BaseTestCase):
exceptions.RequestParameterInvalidException,
self.filter_parser.parse_filters,
[
("deleted", "eq", "true"),
("deleted", "eq", "wot"),
],
)