Skip to content

Commit 29a8bed

Browse files
FrancoisGeaab-odoo
authored andcommitted
[IMP] web: throw error when a domain is invalid
closes #787 Signed-off-by: Géry Debongnie (ged) <ged@openerp.com>
1 parent ee02bd0 commit 29a8bed

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

addons/web/static/src/core/domain.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { toPyValue } from "./py_js/py_utils";
1010
* @typedef {DomainListRepr | string | Domain} DomainRepr
1111
*/
1212

13+
class InvalidDomainError extends Error {}
14+
1315
/**
1416
* Javascript representation of an Odoo domain
1517
*/
@@ -151,19 +153,27 @@ function normalizeDomainAST(domain, op = "&") {
151153
if (domain.type !== 4 /* List */) {
152154
throw new Error("Invalid domain AST");
153155
}
154-
let expected = -1;
156+
if (domain.value.length === 0) {
157+
return domain;
158+
}
159+
let expected = 1;
155160
for (let child of domain.value) {
156161
if (child.type === 1 /* String */ && (child.value === "&" || child.value === "|")) {
157-
expected--;
158-
} else if (child.type !== 1 /* String */ || child.value !== "!") {
159162
expected++;
163+
} else if (child.type !== 1 /* String */ || child.value !== "!") {
164+
expected--;
160165
}
161166
}
162167
let values = domain.value.slice();
163-
while (expected > 0) {
164-
expected--;
168+
while (expected < 0) {
169+
expected++;
165170
values.unshift({ type: 1 /* String */, value: op });
166171
}
172+
if (expected > 0) {
173+
throw new InvalidDomainError(
174+
`invalid domain ${formatAST(domain)} (missing ${expected} segment(s))`
175+
);
176+
}
167177
return { type: 4 /* List */, value: values };
168178
}
169179

addons/web/static/tests/core/domain_tests.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,32 @@ QUnit.module("domain", {}, () => {
257257
assert.notOk(Domain.and([Domain.FALSE, new Domain([["a", "=", 3]])]).contains({ a: 3 }));
258258
});
259259

260+
QUnit.test("invalid domains should not succeed", function (assert) {
261+
assert.throws(
262+
() => new Domain(["|", ["hr_presence_state", "=", "absent"]]),
263+
/invalid domain .* \(missing 1 segment/
264+
);
265+
assert.throws(
266+
() =>
267+
new Domain([
268+
"|",
269+
"|",
270+
["hr_presence_state", "=", "absent"],
271+
["attendance_state", "=", "checked_in"],
272+
]),
273+
/invalid domain .* \(missing 1 segment/
274+
);
275+
assert.throws(
276+
() => new Domain(["|", "|", ["hr_presence_state", "=", "absent"]]),
277+
/invalid domain .* \(missing 2 segment\(s\)/
278+
);
279+
assert.throws(
280+
() => new Domain(["&", ["composition_mode", "!=", "mass_post"]]),
281+
/invalid domain .* \(missing 1 segment/
282+
);
283+
assert.throws(() => new Domain(["!"]), /invalid domain .* \(missing 1 segment/);
284+
});
285+
260286
// ---------------------------------------------------------------------------
261287
// Normalization
262288
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)