From 8b104622550c1dab04db2f830bdbad15b3ddee1e Mon Sep 17 00:00:00 2001 From: Anindya Chatterjee Date: Sat, 28 Sep 2024 22:01:39 +0530 Subject: [PATCH] Update NitriteDocument.java --- .../no2/collection/NitriteDocument.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/nitrite/src/main/java/org/dizitart/no2/collection/NitriteDocument.java b/nitrite/src/main/java/org/dizitart/no2/collection/NitriteDocument.java index 05a36f53..afccfdd9 100644 --- a/nitrite/src/main/java/org/dizitart/no2/collection/NitriteDocument.java +++ b/nitrite/src/main/java/org/dizitart/no2/collection/NitriteDocument.java @@ -342,6 +342,10 @@ private void deepRemove(String[] splits) { throw new ValidationException("Invalid key provided"); } String key = splits[0]; + if (isNullOrEmpty(key)) { + throw new ValidationException("Invalid key provided"); + } + if (splits.length == 1) { // if last key, simply remove the current document remove(key); @@ -417,8 +421,13 @@ private Object getByEmbeddedKey(String embeddedKey) { return null; } + String key = path[0]; + if (isNullOrEmpty(key)) { + throw new ValidationException("Invalid key provided"); + } + // get current level value and scan to next level using remaining keys - return recursiveGet(get(path[0]), Arrays.copyOfRange(path, 1, path.length)); + return recursiveGet(get(key), Arrays.copyOfRange(path, 1, path.length)); } @SuppressWarnings("unchecked") @@ -433,7 +442,12 @@ private Object recursiveGet(Object object, String[] remainingPath) { if (object instanceof Document) { // if the current level value is document, scan to the next level with remaining keys - return recursiveGet(((Document) object).get(remainingPath[0]), + String key = remainingPath[0]; + if (isNullOrEmpty(key)) { + throw new ValidationException("Invalid key provided"); + } + + return recursiveGet(((Document) object).get(key), Arrays.copyOfRange(remainingPath, 1, remainingPath.length)); } @@ -442,6 +456,9 @@ private Object recursiveGet(Object object, String[] remainingPath) { // get the first key String accessor = remainingPath[0]; + if (isNullOrEmpty(accessor)) { + throw new ValidationException("Invalid key provided"); + } // convert current value to object array Object[] array = convertToObjectArray(object);