From 8da00c0872908670ff3c6e9618638b01a3c0d63a Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 30 Mar 2021 21:37:39 +0100 Subject: [PATCH] Fix Switch node handling of hasKey rule when property is undefined --- .../@node-red/nodes/core/function/10-switch.js | 2 +- test/nodes/core/function/10-switch_spec.js | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/10-switch.js b/packages/node_modules/@node-red/nodes/core/function/10-switch.js index 9d1793817..95071731d 100644 --- a/packages/node_modules/@node-red/nodes/core/function/10-switch.js +++ b/packages/node_modules/@node-red/nodes/core/function/10-switch.js @@ -72,7 +72,7 @@ module.exports = function(RED) { return ((min <= index) && (index <= max)); }, 'hask': function(a, b) { - return (typeof b !== "object" ) && a.hasOwnProperty(b+""); + return a !== undefined && a !== null && (typeof b !== "object" ) && a.hasOwnProperty(b+""); }, 'jsonata_exp': function(a, b) { return (b === true); }, 'else': function(a) { return a === true; } diff --git a/test/nodes/core/function/10-switch_spec.js b/test/nodes/core/function/10-switch_spec.js index dcb7dfd45..dfcc55572 100644 --- a/test/nodes/core/function/10-switch_spec.js +++ b/test/nodes/core/function/10-switch_spec.js @@ -267,7 +267,12 @@ describe('switch Node', function() { it('should not match if the key is not a string', function(done) { genericSwitchTest("hask", 1, true, false, {a:1}, done); }); - + it('should not match if the parent object does not exist - null', function(done) { + genericSwitchTest("hask", "a", true, false, null, done); + }); + it('should not match if the parent object does not exist - undefined', function(done) { + genericSwitchTest("hask", "a", true, false, undefined, done); + }); it('should check if payload is between given values', function(done) { twoFieldSwitchTest("btwn", "3", "5", true, true, 4, done); });