You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ng...@apache.org on 2023/02/01 02:26:02 UTC

[jackrabbit-oak] branch trunk updated: OAK-10095 | Fix NPE in FieldFactory.dateToLong (#838)

This is an automated email from the ASF dual-hosted git repository.

ngupta pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 5671cdce1d OAK-10095 | Fix NPE in FieldFactory.dateToLong (#838)
5671cdce1d is described below

commit 5671cdce1d1f9dce48546ef27818153f89d2e9bd
Author: nit0906 <ni...@gmail.com>
AuthorDate: Wed Feb 1 07:55:56 2023 +0530

    OAK-10095 | Fix NPE in FieldFactory.dateToLong (#838)
    
    * OAK-10095 | Fix NPE in FieldFactory.dateToLong
---
 .../jackrabbit/oak/plugins/index/lucene/FieldFactory.java   | 13 ++++++++++++-
 .../oak/plugins/index/lucene/LuceneIndexEditorTest.java     | 10 ++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/FieldFactory.java b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/FieldFactory.java
index ea15d56329..a1d973f36d 100644
--- a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/FieldFactory.java
+++ b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/FieldFactory.java
@@ -19,6 +19,7 @@ package org.apache.jackrabbit.oak.plugins.index.lucene;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.Collection;
 
 import com.google.common.primitives.Ints;
@@ -186,7 +187,17 @@ public final class FieldFactory {
             return null;
         }
         //TODO OAK-2204 - Should we change the precision to lower resolution
-        return ISO8601.parse(date).getTimeInMillis();
+        Calendar c = ISO8601.parse(date);
+        if (c != null) {
+            return c.getTimeInMillis();
+        } else {
+            // ISO8601.parse returns null in case of multiple exceptions like IllegalFormatException, IndexOutOfBoundsException, NumberFormatException etc
+            // However returning null for us would basically store a null value in the document (which seems wrong).
+            // So throwing an unchecked exception here with a proper description.
+            // Earlier such a situation was leading to an NPE which was confusing to understand.
+            // Refer https://jackrabbit.apache.org/api/2.20/index.html?org/apache/jackrabbit/util/ISO8601.html
+            throw new RuntimeException("Unable to parse the provided date field : " + date + " to convert to millis. Supported format is ±YYYY-MM-DDThh:mm:ss.SSSTZD");
+        }
     }
 
 }
diff --git a/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorTest.java b/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorTest.java
index 69c0a6f8db..9a5d503167 100644
--- a/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorTest.java
+++ b/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexEditorTest.java
@@ -233,6 +233,16 @@ public class LuceneIndexEditorTest {
         //Date
         assertEquals("/test", getPath(NumericRangeQuery.newLongRange("creationTime",
                 dateToTime("05/05/2014"), dateToTime("05/07/2014"), true, true)));
+
+        // Call FieldFactory.dateToLong with an unsupported Date format - this should throw a RuntimeException
+        try {
+            getPath(NumericRangeQuery.newLongRange("creationTime",
+                    FieldFactory.dateToLong("05/05/2014"), FieldFactory.dateToLong("05/07/2014"), true, true));
+        } catch (RuntimeException e) {
+            assertEquals("Unable to parse the provided date field : 05/05/2014 to convert to millis." +
+                    " Supported format is ±YYYY-MM-DDThh:mm:ss.SSSTZD", e.getMessage());
+        }
+
     }
 
     @Test