[FIX] web: do not read if field spec is empty

Have a view with many2many_checkboxes field (e.g. product.template
form view with inventory installed, and some routes). Tick a
checkbox. Before this commit, a read was done, but the field spec
was empty (there was no field to read). So basically the read only
returned the id. This is obviously useless. This commit prevents
from doing read rpcs when the spec is empty.

Part of task~3179751

Part-of: odoo/odoo#114024
This commit is contained in:
Jorge Pinna Puissant
2023-07-24 20:17:56 +02:00
committed by Aaron Bohy
parent 0df1f4d439
commit 2129d774a2
2 changed files with 68 additions and 11 deletions
@@ -519,17 +519,24 @@ export class RelationalModel extends Model {
if (!resIds.length) {
return [];
}
const kwargs = {
context: { bin_size: true, ...context },
specification: getFieldsSpec(activeFields, fields, evalContext),
};
const records = await this.orm.call(resModel, "web_read", [resIds], kwargs);
if (!records.length) {
throw new FetchRecordError(resIds);
}
const fieldSpec = getFieldsSpec(activeFields, fields, evalContext);
if (Object.keys(fieldSpec).length > 0) {
const kwargs = {
context: { bin_size: true, ...context },
specification: fieldSpec,
};
const records = await this.orm.call(resModel, "web_read", [resIds], kwargs);
if (!records.length) {
throw new FetchRecordError(resIds);
}
this._applyProperties(records, config);
return records;
this._applyProperties(records, config);
return records;
} else {
return resIds.map((resId) => {
return { id: resId };
});
}
}
/**
@@ -1,6 +1,12 @@
/** @odoo-module **/
import { click, clickSave, editInput, getFixture } from "@web/../tests/helpers/utils";
import {
click,
clickSave,
editInput,
getFixture,
getNodesTextContent,
} from "@web/../tests/helpers/utils";
import { makeView, setupViewRegistries } from "@web/../tests/views/helpers";
let serverData;
@@ -125,6 +131,50 @@ QUnit.module("Fields", (hooks) => {
);
});
QUnit.test("Many2ManyCheckBoxesField does not read added record", async function (assert) {
serverData.models.partner.records[0].timmy = [];
await makeView({
type: "form",
resModel: "partner",
resId: 1,
serverData,
arch: `
<form>
<group>
<field name="timmy" widget="many2many_checkboxes" />
</group>
</form>`,
mockRPC(route, args) {
assert.step(args.method);
},
});
assert.containsN(target, "div.o_field_widget div.form-check", 2);
assert.deepEqual(
getNodesTextContent(target.querySelectorAll(".o_field_widget .form-check-label")),
["gold", "silver"]
);
assert.containsNone(target, "div.o_field_widget div.form-check input:checked");
await click(target.querySelector("div.o_field_widget div.form-check input"));
assert.containsN(target, "div.o_field_widget div.form-check", 2);
assert.deepEqual(
getNodesTextContent(target.querySelectorAll(".o_field_widget .form-check-label")),
["gold", "silver"]
);
assert.containsOnce(target, "div.o_field_widget div.form-check input:checked");
await clickSave(target);
assert.containsN(target, "div.o_field_widget div.form-check", 2);
assert.deepEqual(
getNodesTextContent(target.querySelectorAll(".o_field_widget .form-check-label")),
["gold", "silver"]
);
assert.containsOnce(target, "div.o_field_widget div.form-check input:checked");
assert.verifySteps(["get_views", "web_read", "name_search", "write", "web_read"]);
});
QUnit.test(
"Many2ManyCheckBoxesField: start non empty, then remove twice",
async function (assert) {