From 2129d774a20b78416972435f8a3340d7e7ef2ed4 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Mon, 17 Jul 2023 12:39:37 +0000 Subject: [PATCH] [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 --- .../relational_model/relational_model.js | 27 ++++++---- .../many2many_checkboxes_field_tests.js | 52 ++++++++++++++++++- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/addons/web/static/src/model/relational_model/relational_model.js b/addons/web/static/src/model/relational_model/relational_model.js index e0483b54766..27dfa6a4646 100644 --- a/addons/web/static/src/model/relational_model/relational_model.js +++ b/addons/web/static/src/model/relational_model/relational_model.js @@ -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 }; + }); + } } /** diff --git a/addons/web/static/tests/views/fields/many2many_checkboxes_field_tests.js b/addons/web/static/tests/views/fields/many2many_checkboxes_field_tests.js index 67483eba35e..34850538727 100644 --- a/addons/web/static/tests/views/fields/many2many_checkboxes_field_tests.js +++ b/addons/web/static/tests/views/fields/many2many_checkboxes_field_tests.js @@ -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: ` +
+ + + +
`, + 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) {