fix: copy missing fields in AbstractDetachedCriteria.clone()#15424
Merged
jamesfredley merged 1 commit into7.0.xfrom Feb 21, 2026
Merged
fix: copy missing fields in AbstractDetachedCriteria.clone()#15424jamesfredley merged 1 commit into7.0.xfrom
jamesfredley merged 1 commit into7.0.xfrom
Conversation
clone() was missing connectionName, lazyQuery, and associationCriteriaMap. This caused withConnection() settings to be lost when clone() was called by subsequent chained methods like max(), offset(), or sort() - each of which internally clones the criteria. Fixes #15422 Assisted-by: Claude Code <Claude@Claude.ai>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug in AbstractDetachedCriteria.clone() where three instance fields (connectionName, lazyQuery, and associationCriteriaMap) were not being copied during cloning. This caused withConnection() datasource routing settings to be silently lost when chained with other criteria methods like max(), offset(), or sort(), resulting in queries being executed against the wrong datasource.
Changes:
- Added copying of
connectionName,lazyQuery, andassociationCriteriaMapfields inAbstractDetachedCriteria.clone() - Created comprehensive unit test suite with 7 tests verifying clone behavior for all newly copied fields
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/query/criteria/AbstractDetachedCriteria.groovy |
Added 3 lines to clone() method to copy connectionName, lazyQuery, and associationCriteriaMap fields |
grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/query/criteria/DetachedCriteriaCloneSpec.groovy |
New test file with 7 unit tests verifying clone preserves all required fields and creates independent copies where needed |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
jdaugherty
approved these changes
Feb 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AbstractDetachedCriteria.clone()was missingconnectionName,lazyQuery, andassociationCriteriaMapwithConnection()settings to be silently lost when chained methods likemax(),offset(), orsort()were called afterward, because each internally clones the criteriaExample of the bug
Changes
AbstractDetachedCriteria.clone(): Added copying ofconnectionName,lazyQuery, andassociationCriteriaMap(as a shallow copy)DetachedCriteriaCloneSpec: 7 new unit tests verifying clone preserves all fields, creates independent map copies, and thatwithConnection().max()chaining works correctlyTest Results
DetachedCriteriaCloneSpec: 7/7 passcodeStyle: passFixes #15422
Reproducer: https://github.com/jamesfredley/grails-detachedcriteria-clone-connection