From 2b848a010d756aa03832d794588f917591ff23e5 Mon Sep 17 00:00:00 2001 From: Tao Date: Fri, 6 Sep 2024 09:50:49 +0800 Subject: [PATCH 1/4] [Fix-16534][Master] switch task doesn't support includes method --- .../server/master/utils/SwitchTaskUtils.java | 6 ++++++ .../server/master/utils/SwitchTaskUtilsTest.java | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java index 1676df7e01c1..d875621578e5 100644 --- a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java +++ b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java @@ -40,9 +40,15 @@ public class SwitchTaskUtils { private static final NashornSandbox sandbox; private static final String rgex = "['\"]*\\$\\{(.*?)\\}['\"]*"; + public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES = "if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(valueToFind, fromIndex) { if (this == null) { throw new TypeError('\"this\" is null or not defined'); } var o = Object(this); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); } while (k < len) { if (sameValueZero(o[k], valueToFind)) { return true; } k++; } return false; } }); }"; static { sandbox = NashornSandboxes.create(); + try { + sandbox.eval(NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES); + } catch (ScriptException e) { + log.error("failed to load Nashorn polyfill", e); + } } public static boolean evaluate(String expression) throws ScriptException { diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java index 34785ada4757..a8e50866fc1e 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java @@ -71,4 +71,12 @@ public void testIllegalCondition() { }); } + + @Test + public void testIncludes() throws ScriptException { + String content = "['abc','def'].includes('abc')"; + boolean result = SwitchTaskUtils.evaluate(content); + Assertions.assertTrue(result); + } + } From acfcaac5e3eb8fc2df5aba4b001e5053bf624848 Mon Sep 17 00:00:00 2001 From: Tao Date: Fri, 6 Sep 2024 15:28:34 +0800 Subject: [PATCH 2/4] style: format code --- .../dolphinscheduler/server/master/utils/SwitchTaskUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java index d875621578e5..2a080d631ff4 100644 --- a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java +++ b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java @@ -40,7 +40,8 @@ public class SwitchTaskUtils { private static final NashornSandbox sandbox; private static final String rgex = "['\"]*\\$\\{(.*?)\\}['\"]*"; - public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES = "if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(valueToFind, fromIndex) { if (this == null) { throw new TypeError('\"this\" is null or not defined'); } var o = Object(this); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); } while (k < len) { if (sameValueZero(o[k], valueToFind)) { return true; } k++; } return false; } }); }"; + public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES = + "if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(valueToFind, fromIndex) { if (this == null) { throw new TypeError('\"this\" is null or not defined'); } var o = Object(this); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); } while (k < len) { if (sameValueZero(o[k], valueToFind)) { return true; } k++; } return false; } }); }"; static { sandbox = NashornSandboxes.create(); From c6eece12873b0aa0e1ad76dabaa32bf92bc7e880 Mon Sep 17 00:00:00 2001 From: Tao Date: Tue, 24 Sep 2024 13:12:58 +0800 Subject: [PATCH 3/4] format code --- .../server/master/utils/SwitchTaskUtils.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java index 2a080d631ff4..6ee509a1dba8 100644 --- a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java +++ b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtils.java @@ -41,7 +41,29 @@ public class SwitchTaskUtils { private static final NashornSandbox sandbox; private static final String rgex = "['\"]*\\$\\{(.*?)\\}['\"]*"; public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES = - "if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(valueToFind, fromIndex) { if (this == null) { throw new TypeError('\"this\" is null or not defined'); } var o = Object(this); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); } while (k < len) { if (sameValueZero(o[k], valueToFind)) { return true; } k++; } return false; } }); }"; + "if (!Array.prototype.includes) {" + + " Object.defineProperty(Array.prototype, 'includes', {" + + " value: function(valueToFind, fromIndex) {" + + " if (this == null) {" + + " throw new TypeError('\"this\" is null or not defined');" + + " }" + + " var o = Object(this);" + + " var len = o.length >>> 0;" + + " if (len === 0) { return false; }" + + " var n = fromIndex | 0;" + + " var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);" + + " function sameValueZero(x, y) {" + + " return x === y || (typeof x === 'number' && " + + " typeof y === 'number' && isNaN(x) && isNaN(y));" + + " }" + + " while (k < len) {" + + " if (sameValueZero(o[k], valueToFind)) { return true; }" + + " k++;" + + " }" + + " return false;" + + " }" + + " });" + + "}"; static { sandbox = NashornSandboxes.create(); From 7caf35043c0e0b7508c9ccce710d4c3c22f5ef73 Mon Sep 17 00:00:00 2001 From: Tao Date: Wed, 25 Sep 2024 10:09:40 +0800 Subject: [PATCH 4/4] [16534][test] Improve unit test --- .../server/master/utils/SwitchTaskUtilsTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java index a8e50866fc1e..81b8b1bc9c99 100644 --- a/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java +++ b/dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/utils/SwitchTaskUtilsTest.java @@ -77,6 +77,10 @@ public void testIncludes() throws ScriptException { String content = "['abc','def'].includes('abc')"; boolean result = SwitchTaskUtils.evaluate(content); Assertions.assertTrue(result); + + SwitchTaskUtils.evaluate(SwitchTaskUtils.NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES); + result = SwitchTaskUtils.evaluate(content); + Assertions.assertTrue(result); } }