From 3e4c45ac6a3cd67d2e8e00343ac5ac097a3b1774 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Thu, 22 Sep 2022 20:22:11 +0100 Subject: [PATCH] Fix change node overwriting msg with itself and add test to close #3891 --- .../@node-red/nodes/core/function/15-change.js | 2 +- test/nodes/core/function/15-change_spec.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/15-change.js b/packages/node_modules/@node-red/nodes/core/function/15-change.js index 94f47fcbf..ee0e8d9c7 100644 --- a/packages/node_modules/@node-red/nodes/core/function/15-change.js +++ b/packages/node_modules/@node-red/nodes/core/function/15-change.js @@ -318,7 +318,7 @@ module.exports = function(RED) { } var r = node.rules[currentRule]; if (r.t === "move") { - if ((r.tot !== r.pt) || (r.p.indexOf(r.to) !== -1)) { + if ((r.tot !== r.pt) || (r.p.indexOf(r.to) !== -1) && (r.p !== r.to)) { applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:r.p, tot:r.pt},(err,msg) => { applyRule(msg,{t:"delete", p:r.p, pt:r.pt}, (err,msg) => { completeApplyingRules(msg,currentRule,done); diff --git a/test/nodes/core/function/15-change_spec.js b/test/nodes/core/function/15-change_spec.js index b8d7be03b..66f02d6d1 100644 --- a/test/nodes/core/function/15-change_spec.js +++ b/test/nodes/core/function/15-change_spec.js @@ -1717,6 +1717,24 @@ describe('change Node', function() { changeNode1.receive({topic:{foo:{bar:1}}, payload:"String"}); }); }); + it('moves the value of a message property object to itself', function(done) { + var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload","tot":"msg"}],"name":"changeNode","wires":[["helperNode1"]]}, + {id:"helperNode1", type:"helper", wires:[]}]; + helper.load(changeNode, flow, function() { + var changeNode1 = helper.getNode("changeNode1"); + var helperNode1 = helper.getNode("helperNode1"); + helperNode1.on("input", function(msg) { + try { + msg.should.have.property('payload'); + msg.payload.should.equal("bar"); + done(); + } catch(err) { + done(err); + } + }); + changeNode1.receive({payload:"bar"}); + }); + }); it('moves the value of a message property object to a sub-property', function(done) { var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.foo","tot":"msg"}],"name":"changeNode","wires":[["helperNode1"]]}, {id:"helperNode1", type:"helper", wires:[]}];