apache / apache/grails-core

CompileStatic type validation for dynamic finders

Open
#14,417 4 comments 0 reactions 0 assignees View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I had a dynamic finder like this...

Set 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.

Contributor guide

Open the contributing guide

Research direction

Start in DynamicFinder.java, especially resetMethodExpressionPattern around lines 643 and 712, and inspect how the method-expression pattern validates dynamic finder names. Confirm that an unrecognized suffix such as "GreaterThanEqual" is rejected rather than partially matched as "GreaterThan". Done means the malformed finder reports an error instead of generating incorrect SQL.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.