[FIX] web: domain: can follow field relations

Before this commit, a domain like
```
   [["partner_id.name", "ilike", "Agrolait"]]
```
would crash when being evaluated.

Part-of: odoo/odoo#92475
This commit is contained in:
Mathieu Duckerts-Antoine
2022-06-30 09:29:39 +02:00
committed by Aaron Bohy
parent 3e6bbb4ebd
commit 7e1ed51c5e
2 changed files with 57 additions and 7 deletions
+8
View File
@@ -187,6 +187,14 @@ function matchCondition(record, condition) {
return condition;
}
const [field, operator, value] = condition;
if (typeof field === "string") {
const names = field.split(".");
if (names.length >= 2) {
return matchCondition(record[names[0]], [names.slice(1).join("."), operator, value]);
}
}
const fieldValue = typeof field === "number" ? field : record[field];
switch (operator) {
case "=":
+49 -7
View File
@@ -161,16 +161,37 @@ QUnit.module("domain", {}, () => {
'[("name", "=", "true")]'
);
assert.strictEqual(new Domain().toString(), "[]");
assert.strictEqual(new Domain([['name', 'in', [true, false]]]).toString(), '[("name", "in", [True, False])]');
assert.strictEqual(new Domain([['name', 'in', [null]]]).toString(), '[("name", "in", [None])]');
assert.strictEqual(new Domain([['name', 'in', ["foo", "bar"]]]).toString(), '[("name", "in", ["foo", "bar"])]');
assert.strictEqual(new Domain([['name', 'in', [1, 2]]]).toString(), '[("name", "in", [1, 2])]');
assert.strictEqual(new Domain(['&', ['name', '=', 'foo'], ['type', '=', 'bar']]).toString(), '["&", ("name", "=", "foo"), ("type", "=", "bar")]');
assert.strictEqual(new Domain(['|', ['name', '=', 'foo'], ['type', '=', 'bar']]).toString(), '["|", ("name", "=", "foo"), ("type", "=", "bar")]');
assert.strictEqual(
new Domain([["name", "in", [true, false]]]).toString(),
'[("name", "in", [True, False])]'
);
assert.strictEqual(
new Domain([["name", "in", [null]]]).toString(),
'[("name", "in", [None])]'
);
assert.strictEqual(
new Domain([["name", "in", ["foo", "bar"]]]).toString(),
'[("name", "in", ["foo", "bar"])]'
);
assert.strictEqual(
new Domain([["name", "in", [1, 2]]]).toString(),
'[("name", "in", [1, 2])]'
);
assert.strictEqual(
new Domain(["&", ["name", "=", "foo"], ["type", "=", "bar"]]).toString(),
'["&", ("name", "=", "foo"), ("type", "=", "bar")]'
);
assert.strictEqual(
new Domain(["|", ["name", "=", "foo"], ["type", "=", "bar"]]).toString(),
'["|", ("name", "=", "foo"), ("type", "=", "bar")]'
);
assert.strictEqual(new Domain().toString(), "[]");
// string domains are only reformatted
assert.strictEqual(new Domain('[("name","ilike","foo")]').toString(), '[("name", "ilike", "foo")]');
assert.strictEqual(
new Domain('[("name","ilike","foo")]').toString(),
'[("name", "ilike", "foo")]'
);
});
QUnit.test("implicit &", function (assert) {
@@ -310,6 +331,27 @@ QUnit.module("domain", {}, () => {
assert.throws(() => new Domain(["!"]), /invalid domain .* \(missing 1 segment/);
});
QUnit.test("follow relations", function (assert) {
assert.ok(
new Domain([["partner.city", "ilike", "Bru"]]).contains({
name: "Lucas",
partner: {
city: "Bruxelles",
},
})
);
assert.ok(
new Domain([["partner.city.name", "ilike", "Bru"]]).contains({
name: "Lucas",
partner: {
city: {
name: "Bruxelles",
},
},
})
);
});
// ---------------------------------------------------------------------------
// Normalization
// ---------------------------------------------------------------------------