grails-data-mapping icon indicating copy to clipboard operation
grails-data-mapping copied to clipboard

No error for misspelled dynamic finder

Open xpusostomos opened this issue 3 years ago • 0 comments

I had a dynamic finder like this...

Set<AccountEntry> aes = AccountEntry.findAllByDateGreaterThanEqualAndDateLessThanEqual(c.fromDate, c.toDate)

And I couldn't figure out why it was generating a > and < SQL rather than >= and <= SQL.

So it turned out to be my bad that it should be "Equals" rather than "Equal". However, it's not good that this doesn't generate any error.

So it turns out that in DynamicFinder.java line 643, it tries to use regular expressions to figure out which expression to use. This expression is generated from DynamicFinder.java line 712...

\p{Upper}[\p{Lower}\d]+(Equal|NotEqual|NotInList|InList|InRange|Between|Like|Ilike|Rlike|GreaterThanEquals|LessThanEquals|GreaterThan|LessThan|IsNull|IsNotNull|IsEmpty|IsNotEmpty)

from...

private static void resetMethodExpressionPattern() {
    String expressionPattern = DefaultGroovyMethods.join((Iterable)methodExpressions.keySet(), "|");
    methodExpressinPattern = Pattern.compile("\\p{Upper}[\\p{Lower}\\d]+(" + expressionPattern + ")");
}

The trouble is, this expression isn't anchored at the end... so it matches GreaterThan and generates no error about the "Equal" which it doesn't understand.

It seems to me that if the expression generated from resetMethodExpressionPattern was changed to end with "$"... the regex end-anchor, it would generate an error about unrecognized "GreaterThanEqual" and lead to more robust code from not having subtle errors that only manifest in certain edge cases.

xpusostomos avatar Nov 21 '21 02:11 xpusostomos