Skip to content

Commit 7c8a423

Browse files
Eduard Bosch BertranEduard Bosch Bertran
authored andcommitted
fix: Constraints for $all $regex are KeyConstraints and not strings
1 parent caaf453 commit 7c8a423

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private static boolean matchesEqualConstraint(Object constraint, Object value) {
236236
decider = new Decider() {
237237
@Override
238238
public boolean decide(Object constraint, Object value) {
239-
return ((String) value).matches(constraint.toString());
239+
return ((String) value).matches(((KeyConstraints)constraint).get("$regex").toString());
240240
}
241241
};
242242
} else {
@@ -390,9 +390,13 @@ private static boolean isAnyValueRegexStartsWith(Collection<?> constraints) {
390390
* All values in a $all constraint must be a starting with another string regex.
391391
*/
392392
private static Collection<?> cleanRegexStartsWith(Collection<?> constraints) {
393-
ArrayList<String> cleanedValues = new ArrayList<>();
393+
ArrayList<KeyConstraints> cleanedValues = new ArrayList<>();
394394
for (Object constraint : constraints) {
395-
String cleanedRegex = cleanRegexStartsWith((String) constraint);
395+
if (!(constraint instanceof KeyConstraints)) {
396+
return null;
397+
}
398+
399+
KeyConstraints cleanedRegex = cleanRegexStartsWith((KeyConstraints) constraint);
396400
if (cleanedRegex == null) {
397401
return null;
398402
}
@@ -409,28 +413,35 @@ private static Collection<?> cleanRegexStartsWith(Collection<?> constraints) {
409413
* If given string is not a regex to match a string at the beginning of another string, then null
410414
* is returned.
411415
*/
412-
private static String cleanRegexStartsWith(String regex) {
416+
private static KeyConstraints cleanRegexStartsWith(KeyConstraints regex) {
413417
if (!isStartsWithRegex(regex)) {
414418
return null;
415419
}
416420

417421
// remove all instances of \Q and \E from the remaining text & escape single quotes
418-
String literalizedString = regex.replaceAll("([^\\\\])(\\\\E)", "$1")
422+
String literalizedString = ((String)regex.get("$regex"))
423+
.replaceAll("([^\\\\])(\\\\E)", "$1")
419424
.replaceAll("([^\\\\])(\\\\Q)", "$1")
420425
.replaceAll("^\\\\E", "")
421426
.replaceAll("^\\\\Q", "")
422427
.replaceAll("([^'])'", "$1''")
423428
.replaceAll("^'([^'])", "''$1");
424429

425-
return '^' + literalizedString + ".*";
430+
regex.put("$regex", literalizedString + ".*");
431+
return regex;
426432
}
427433

428434
/**
429435
* Check if given constraint is a regex to match strings that starts with another string.
430436
*/
431437
private static boolean isStartsWithRegex(Object constraint) {
432-
return constraint != null && constraint instanceof String &&
433-
((String)constraint).startsWith("^");
438+
if (constraint == null || !(constraint instanceof KeyConstraints)) {
439+
return false;
440+
}
441+
442+
KeyConstraints keyConstraints = (KeyConstraints) constraint;
443+
return keyConstraints.size() == 1 && keyConstraints.containsKey("$regex") &&
444+
((String)keyConstraints.get("$regex")).startsWith("^");
434445
}
435446

436447
/**

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,25 +395,25 @@ public void testMatchesAllStartingWith() throws Exception {
395395
query = new ParseQuery.State.Builder<>("TestObject")
396396
.addCondition("foo", "$all",
397397
Arrays.asList(
398-
buildStartsWithRegex("foo"),
399-
buildStartsWithRegex("bar")))
398+
buildStartsWithRegexKeyConstraint("foo"),
399+
buildStartsWithRegexKeyConstraint("bar")))
400400
.build();
401401
assertTrue(matches(logic, query, object));
402402

403403
query = new ParseQuery.State.Builder<>("TestObject")
404404
.addCondition("foo", "$all",
405405
Arrays.asList(
406-
buildStartsWithRegex("fo"),
407-
buildStartsWithRegex("b")))
406+
buildStartsWithRegexKeyConstraint("fo"),
407+
buildStartsWithRegexKeyConstraint("b")))
408408
.build();
409409
assertTrue(matches(logic, query, object));
410410

411411
query = new ParseQuery.State.Builder<>("TestObject")
412412
.addCondition("foo", "$all",
413413
Arrays.asList(
414-
buildStartsWithRegex("foo"),
415-
buildStartsWithRegex("bar"),
416-
buildStartsWithRegex("qux")))
414+
buildStartsWithRegexKeyConstraint("foo"),
415+
buildStartsWithRegexKeyConstraint("bar"),
416+
buildStartsWithRegexKeyConstraint("qux")))
417417
.build();
418418
assertFalse(matches(logic, query, object));
419419

@@ -439,19 +439,43 @@ public void testMatchesAllStartingWithParameters() throws Exception {
439439
query = new ParseQuery.State.Builder<>("TestObject")
440440
.addCondition("foo", "$all",
441441
Arrays.asList(
442-
buildStartsWithRegex("foo"),
443-
buildStartsWithRegex("bar")))
442+
buildStartsWithRegexKeyConstraint("foo"),
443+
buildStartsWithRegexKeyConstraint("bar")))
444444
.build();
445445
assertTrue(matches(logic, query, object));
446446

447447
query = new ParseQuery.State.Builder<>("TestObject")
448448
.addCondition("foo", "$all",
449449
Arrays.asList(
450-
buildStartsWithRegex("fo"),
450+
buildStartsWithRegexKeyConstraint("fo"),
451+
buildStartsWithRegex("ba"),
451452
"b"))
452453
.build();
453454
thrown.expect(IllegalArgumentException.class);
454455
assertFalse(matches(logic, query, object));
456+
457+
query = new ParseQuery.State.Builder<>("TestObject")
458+
.addCondition("foo", "$all",
459+
Arrays.asList(
460+
buildStartsWithRegexKeyConstraint("fo"),
461+
"b"))
462+
.build();
463+
thrown.expect(IllegalArgumentException.class);
464+
assertFalse(matches(logic, query, object));
465+
}
466+
467+
/**
468+
* Helper method to convert a string to a key constraint to match strings that starts with given
469+
* string.
470+
*
471+
* @param prefix String to use as prefix in regex.
472+
* @return The key constraint for word matching at the beginning of a string.
473+
*/
474+
@NonNull
475+
private ParseQuery.KeyConstraints buildStartsWithRegexKeyConstraint(String prefix) {
476+
ParseQuery.KeyConstraints constraint = new ParseQuery.KeyConstraints();
477+
constraint.put("$regex", buildStartsWithRegex(prefix));
478+
return constraint;
455479
}
456480

457481
/**

0 commit comments

Comments
 (0)