apache / apache/grails-core

Entity.find with closure breaks in Spock then: block

Open
#14,420 1 comment 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

When doing a query with a `.find { }` method on a domain object (in this case in a Grails 4.0.11 application) the generated detached criteria does not contain the criteria defined in code.

Example:
```
def "test where query is in then block with def"() {
given:
Bundle bundle = new Bundle(name: 'bundle')
bundle.addToVersions(name: '1.0')
bundle.save(flush: true)

when:
bundle.addToVersions(name: '1.1')
bundle.save(flush: true)

then:
def result = BundleVersion.find({ name == '1.1' && bundle.name == 'bundle' })
result.name == '1.1'
}
```
where the `result` is a variable, then it works. But if the variable is omitted like this:
```
def "test where query is in then block without def"() {
given:
Bundle bundle = new Bundle(name: 'bundle')
bundle.addToVersions(name: '1.0')
bundle.save(flush: true)

when:
bundle.addToVersions(name: '1.1')
bundle.save(flush: true)

then: "this will fail - which is not expected"
BundleVersion.find({ name == '1.1' && bundle.name == 'bundle' }).name == '1.1'
}
```
Then `1.0` is found which is unexpected.

An example of this behaviour can be found here: https://github.com/sbglasius/gorm-query-bug

Discovered by @thomas58

Contributor guide

Open the contributing guide

Research direction

Start with the linked gorm-query-bug reproduction and the two Spock specifications shown in the issue. Trace how BundleVersion.find handles a closure when called inline in a then block, compared with assigning the result first. Done means the inline expression applies both criteria and the specification passes while still finding version 1.1.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.