From 6eb5002c128de6d2bb25d943b2bda2ee9f2c7951 Mon Sep 17 00:00:00 2001 From: Jeff Boulter Date: Tue, 9 Jun 2020 16:57:57 -0400 Subject: [PATCH 1/2] Check for nulls in range function --- .../java/com/hubspot/jinjava/lib/fn/Functions.java | 10 ++++++---- .../com/hubspot/jinjava/lib/fn/RangeFunctionTest.java | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java b/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java index c8669da89..2aa716f56 100644 --- a/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java +++ b/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java @@ -196,7 +196,7 @@ public static long unixtimestamp(Object... var) { value = "var", type = "datetimeFormat", desc = "format of the datetime string" - ) + ), } ) public static PyishDate stringToTime(String datetimeString, String datetimeFormat) { @@ -331,16 +331,18 @@ public static List range(Object arg1, Object... args) { break; case 1: start = NumberUtils.toInt(arg1.toString()); - if (NumberUtils.isNumber(args[0].toString())) { + if (args[0] != null && NumberUtils.isNumber(args[0].toString())) { end = NumberUtils.toInt(args[0].toString(), start + RANGE_LIMIT); } break; default: start = NumberUtils.toInt(arg1.toString()); - if (NumberUtils.isNumber(args[0].toString())) { + if (args[0] != null && NumberUtils.isNumber(args[0].toString())) { end = NumberUtils.toInt(args[0].toString(), start + RANGE_LIMIT); } - step = NumberUtils.toInt(args[1].toString(), 1); + if (args[1] != null) { + step = NumberUtils.toInt(args[1].toString(), 1); + } } if (step == 0) { diff --git a/src/test/java/com/hubspot/jinjava/lib/fn/RangeFunctionTest.java b/src/test/java/com/hubspot/jinjava/lib/fn/RangeFunctionTest.java index 4503315da..534c73192 100644 --- a/src/test/java/com/hubspot/jinjava/lib/fn/RangeFunctionTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/fn/RangeFunctionTest.java @@ -38,6 +38,8 @@ public void itHandlesBadRanges() { public void itHandlesBadValues() { assertThat(Functions.range("f")).isEmpty(); assertThat(Functions.range(2, "f")).isEmpty(); + assertThat(Functions.range(2, new Object[] { null })).isEmpty(); + assertThat(Functions.range(2, 4, null)).isEqualTo(Arrays.asList(2, 3)); } @Test From bf049f699b82bca3410188da6adb52c792a4a30d Mon Sep 17 00:00:00 2001 From: Jeff Boulter Date: Tue, 9 Jun 2020 17:04:24 -0400 Subject: [PATCH 2/2] prettier --- src/main/java/com/hubspot/jinjava/lib/fn/Functions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java b/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java index 2aa716f56..bb323c5a5 100644 --- a/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java +++ b/src/main/java/com/hubspot/jinjava/lib/fn/Functions.java @@ -196,7 +196,7 @@ public static long unixtimestamp(Object... var) { value = "var", type = "datetimeFormat", desc = "format of the datetime string" - ), + ) } ) public static PyishDate stringToTime(String datetimeString, String datetimeFormat) {