From 50f58212410b321c1c4ad3d42a91cd1d23567308 Mon Sep 17 00:00:00 2001
From: Mathieu Duckerts-Antoine <dam@odoo.com>
Date: Fri, 25 Nov 2022 14:25:59 +0000
Subject: [PATCH] [FIX] web: search model: one2many search default labels

When a one2many is used as a search default, its label has to be fetch
via a name_get in order to get a correct display of the facet
corresponding to that field in the search bar. It turns out that the
search model did not wait properly the return of the name_gets before to
start to compute the facets.

closes odoo/odoo#106575

X-original-commit: 95ef425
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Signed-off-by: Mathieu Duckerts-Antoine <dam@odoo.com>
---
 .../static/src/search/search_arch_parser.js   |  2 +-
 .../static/tests/search/search_bar_tests.js   | 31 +++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/addons/web/static/src/search/search_arch_parser.js b/addons/web/static/src/search/search_arch_parser.js
index dd1a80521efd..f913697a64bb 100644
--- a/addons/web/static/src/search/search_arch_parser.js
+++ b/addons/web/static/src/search/search_arch_parser.js
@@ -160,7 +160,7 @@ export class SearchArchParser extends XMLParser {
                     preField.defaultAutocompleteValue.label = option[1];
                 } else if (fieldType === "many2one") {
                     this.labels.push((orm) => {
-                        orm.call(relation, "name_get", [value], { context }).then((results) => {
+                        return orm.call(relation, "name_get", [value], { context }).then((results) => {
                             preField.defaultAutocompleteValue.label = results[0][1];
                         });
                     });
diff --git a/addons/web/static/tests/search/search_bar_tests.js b/addons/web/static/tests/search/search_bar_tests.js
index 239f54679f25..ed235a3e94fa 100644
--- a/addons/web/static/tests/search/search_bar_tests.js
+++ b/addons/web/static/tests/search/search_bar_tests.js
@@ -998,4 +998,35 @@ QUnit.module("Search", (hooks) => {
         await triggerEvent(searchInput, null, "keydown", { key: "Enter" });
         assert.deepEqual(getDomain(controlPanel), [["company", "=", 5]]);
     });
+
+    QUnit.test("should wait label promises for one2many search defaults", async function (assert) {
+        assert.expect(3);
+
+        const target = getFixture();
+
+        const def = makeDeferred();
+        const mockRPC = async (_, args) => {
+            if (args.method === "name_get") {
+                await def;
+            }
+        };
+
+        makeWithSearch({
+            serverData,
+            mockRPC,
+            resModel: "partner",
+            Component: ControlPanel,
+            searchMenuTypes: [],
+            searchViewId: false,
+            context: { search_default_company: 1 },
+        });
+
+        await nextTick();
+        assert.containsNone(target, ".o_control_panel");
+
+        def.resolve();
+        await nextTick();
+        assert.containsOnce(target, ".o_control_panel");
+        assert.strictEqual(getFacetTexts(target)[0].replace("\n", ""), "CompanyFirst record");
+    });
 });
-- 
GitLab