Skip to content

Commit d36a577

Browse files
committed
Fix querying for a ParseObject with JSONArray key
`OfflineQueryLogic` was missing the proper code path to compare a value against a `JSONArray`. Added tests for comparing a value against a `List`.
1 parent 3873c3d commit d36a577

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Parse/src/main/java/com/parse/OfflineQueryLogic.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.parse.ParseQuery.RelationConstraint;
1414

1515
import org.json.JSONArray;
16+
import org.json.JSONException;
1617
import org.json.JSONObject;
1718

1819
import java.util.ArrayList;
@@ -178,13 +179,33 @@ private static boolean compareList(Object constraint, List<?> values, Decider de
178179
}
179180

180181
/**
182+
* Returns true if decider returns true for any value in the given list.
183+
*/
184+
private static boolean compareArray(Object constraint, JSONArray values, Decider decider) {
185+
for (int i = 0; i < values.length(); ++i) {
186+
try {
187+
if (decider.decide(constraint, values.get(i))) {
188+
return true;
189+
}
190+
} catch (JSONException e) {
191+
// This can literally never happen.
192+
throw new RuntimeException(e);
193+
}
194+
}
195+
return false;
196+
}
197+
198+
/**
199+
*
181200
* Returns true if the decider returns true for the given value and the given constraint. This
182201
* method handles Mongo's logic where an item can match either an item itself, or any item within
183202
* the item, if the item is an array.
184203
*/
185204
private static boolean compare(Object constraint, Object value, Decider decider) {
186205
if (value instanceof List) {
187206
return compareList(constraint, (List<?>) value, decider);
207+
} else if (value instanceof JSONArray) {
208+
return compareArray(constraint, (JSONArray) value, decider);
188209
} else {
189210
return decider.decide(constraint, value);
190211
}

Parse/src/test/java/com/parse/OfflineQueryLogicTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,44 @@ public void testMatchesWithin() throws ParseException {
510510

511511
//endregion
512512

513+
//region compare
514+
515+
@Test
516+
public void testCompareList() throws Exception {
517+
ParseObject object = new ParseObject("SomeObject");
518+
List<Integer> list = new ArrayList<>();
519+
list.add(1);
520+
list.add(2);
521+
list.add(3);
522+
object.put("list", list);
523+
524+
OfflineQueryLogic logic = new OfflineQueryLogic(null);
525+
526+
ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("SomeObject")
527+
.whereEqualTo("list", 2)
528+
.build();
529+
assertTrue(matches(logic, query, object));
530+
}
531+
532+
@Test
533+
public void testCompareJSONArray() throws Exception {
534+
ParseObject object = new ParseObject("SomeObject");
535+
JSONArray array = new JSONArray();
536+
array.put(1);
537+
array.put(2);
538+
array.put(3);
539+
object.put("array", array);
540+
541+
OfflineQueryLogic logic = new OfflineQueryLogic(null);
542+
543+
ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("SomeObject")
544+
.whereEqualTo("array", 2)
545+
.build();
546+
assertTrue(matches(logic, query, object));
547+
}
548+
549+
//endregion
550+
513551
//region compareTo
514552

515553
@Test

0 commit comments

Comments
 (0)