From b2dffa40ca89d6ff55d3345771a2f55aaafc9bf9 Mon Sep 17 00:00:00 2001
From: Lucas Perais <lpe@odoo.com>
Date: Wed, 17 May 2023 13:34:47 +0000
Subject: [PATCH] [FIX] web: aceField with a value of false

Have a record with a field displayed with the aceField.
That field should have a value of false (ie: unset).
Display that record on a form view.

With the pager, go on to the next record.

Before this commit, a write was triggered because the aceField considered itself as changed
even though we did not do anything.
This was because the value of false was compared to the ace value (empty string) when leaving the record.
Being different, this triggered the current record to be updated and saved.

After this commit, a false value is locally transformed to the empty string when checking if the field has
some changes. Hence, there is no write triggered.

opw-3326914

closes odoo/odoo#121634

Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
---
 .../static/src/views/fields/ace/ace_field.js  |  2 +-
 .../views/fields/ace_editor_field_tests.js    | 29 +++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/addons/web/static/src/views/fields/ace/ace_field.js b/addons/web/static/src/views/fields/ace/ace_field.js
index 9b3afdd5544e..e9dfade8eac1 100644
--- a/addons/web/static/src/views/fields/ace/ace_field.js
+++ b/addons/web/static/src/views/fields/ace/ace_field.js
@@ -103,7 +103,7 @@ export class AceField extends Component {
     commitChanges() {
         if (!this.props.readonly) {
             const value = this.aceSession.getValue();
-            if (this.props.value !== value) {
+            if ((this.props.value || "") !== value) {
                 return this.props.update(value);
             }
         }
diff --git a/addons/web/static/tests/views/fields/ace_editor_field_tests.js b/addons/web/static/tests/views/fields/ace_editor_field_tests.js
index f4c57c6a5331..3cba00aaa4a2 100644
--- a/addons/web/static/tests/views/fields/ace_editor_field_tests.js
+++ b/addons/web/static/tests/views/fields/ace_editor_field_tests.js
@@ -96,4 +96,33 @@ QUnit.module("Fields", (hooks) => {
 
         assert.ok(target.querySelector(".o_field_ace").textContent.includes("blip"));
     });
+
+    QUnit.test(
+        "leaving an untouched record with an unset ace field should not write",
+        async (assert) => {
+            serverData.models.partner.records.forEach((rec) => {
+                rec.foo = false;
+            });
+            await makeView({
+                type: "form",
+                resModel: "partner",
+                resId: 1,
+                resIds: [1, 2],
+                serverData,
+                arch: /* xml */ `
+                <form>
+                    <field name="foo" widget="ace" />
+                </form>`,
+                mockRPC(route, args) {
+                    if (args.method) {
+                        assert.step(`${args.method}: ${JSON.stringify(args.args)}`);
+                    }
+                },
+            });
+
+            assert.verifySteps(["get_views: []", 'read: [[1],["foo","display_name"]]']);
+            await pagerNext(target);
+            assert.verifySteps(['read: [[2],["foo","display_name"]]']);
+        }
+    );
 });
-- 
GitLab