Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2050: apoc.number.parseFloat throws NullPointerException with null argument #2070

Merged
merged 1 commit into from Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/main/java/apoc/number/Numbers.java
@@ -1,5 +1,6 @@
package apoc.number;

import org.apache.commons.lang.StringUtils;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;
Expand Down Expand Up @@ -35,6 +36,9 @@ public Long parseInt(final @Name("text") String text, @Name(value = "pattern",de
}

private Number parseNumber(@Name("text") String text, @Name(value = "pattern", defaultValue = "") String pattern, @Name(value = "lang", defaultValue = "") String lang) {
if (StringUtils.isBlank(text)) {
return null;
}
try {
return buildFormatter(pattern, lang).parse(text);
} catch (ParseException e) {
Expand Down
3 changes: 3 additions & 0 deletions core/src/test/java/apoc/number/NumbersTest.java
Expand Up @@ -11,6 +11,7 @@

import static apoc.util.TestUtil.testCall;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
* @author inserpio
Expand Down Expand Up @@ -50,6 +51,7 @@ public void testParseInt() throws Exception {
testCall(db, "RETURN apoc.number.parseInt('12,345', '#,##0.00;(#,##0.00)') AS value", row -> assertEquals(new Long(12345), row.get("value")));
testCall(db, "RETURN apoc.number.parseInt('12.345', '#,##0.00;(#,##0.00)', 'it') AS value", row -> assertEquals(new Long(12345), row.get("value")));
testCall(db, "RETURN apoc.number.parseInt('aaa') AS value", row -> assertEquals(null, row.get("value")));
testCall(db, "RETURN apoc.number.parseInt(null) AS value", row -> assertNull(row.get("value")));
}

// Parse Double
Expand All @@ -61,5 +63,6 @@ public void testParseFloat() throws Exception {
testCall(db, "RETURN apoc.number.parseFloat('12,345.67', '#,##0.00;(#,##0.00)') AS value", row -> assertEquals(new Double(12345.67), row.get("value")));
testCall(db, "RETURN apoc.number.parseFloat('12.345,67', '#,##0.00;(#,##0.00)', 'it') AS value", row -> assertEquals(new Double(12345.67), row.get("value")));
testCall(db, "RETURN apoc.number.parseFloat('aaa') AS value", row -> assertEquals(null, row.get("value")));
testCall(db, "RETURN apoc.number.parseFloat(null) AS value", row -> assertNull(row.get("value")));
}
}