Skip to content

feat(gorm): the criteria closures of the static GORM API type-check under CompileStatic - #17080

Open
borinquenkid wants to merge 1 commit into
8.1.xfrom
feat/gorm-entity-delegates-to-8.1.x
Open

borinquenkid wants to merge 1 commit into
8.1.xfrom
feat/gorm-entity-delegates-to-8.1.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

@DelegatesTo part of #16823 (item 5 of matrei's review), on 8.1.x: it lets code that did not compile before compile, so it is an improvement for the next minor, not a fix for a patch line.

Part of #16808

The change

The closures of where, whereLazy, whereAny and both findAll overloads on GormEntity are annotated with @DelegatesTo(DetachedCriteria), so eq, gt and the other criteria methods type-check inside plain @CompileStatic code instead of needing @CompileDynamic. GormEntity is in grails-datamapping-core, so every datastore gets it. No result changes: code that compiled before runs as it did.

What changes for applications

Nothing has to change. Code that used @CompileDynamic only for these calls can drop it. The upgrade guide has a note in the Grails 8.1.0 section.

Tests

GormEntityCriteriaTypingSpec (Hibernate 7): the flat criteria of the five calls under plain @CompileStatic (the spec does not compile without the annotations), and where closures under @GrailsCompileStatic with an association block, a path and a mix, which return the rows of the association rather than binding the call to the outer criteria. The full grails-datamapping-core and grails-data-hibernate7-core suites pass.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…nder CompileStatic

where, whereLazy, whereAny and findAll take a closure that is annotated with DelegatesTo(DetachedCriteria), so eq, gt and the other criteria methods type-check inside CompileStatic code. No result changes.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A nested query attaches its condition to the outer query.

*/
@Generated
static DetachedCriteria<D> where(Closure callable) {
static DetachedCriteria<D> where(@DelegatesTo(DetachedCriteria) Closure callable) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A where query on one class contains a where query on another, and the inner query calls eq. That eq should restrict the inner query. It restricts the outer query, so the inner query matches every row. Resolve the call on the inner query first.

@sanjana2505006

Copy link
Copy Markdown
Member

Agree with James on this one. I think the cause is the missing strategy on the annotation. @DelegatesTo defaults to OWNER_FIRST, but DetachedCriteria.build() runs the closure with DELEGATE_FIRST, so under static compilation the inner eq ends up on the outer criteria.

Using @DelegatesTo(value = DetachedCriteria, strategy = Closure.DELEGATE_FIRST), the same way DetachedCriteria.where does, should fix it. A test with a nested where query would be nice to have as well.

Small side note: RxEntity seems to have the same pattern, maybe for a separate PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants