-
-
Notifications
You must be signed in to change notification settings - Fork 970
Add clearConstraintsMapCache() public API to Validateable trait #15346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add a new static method to the Validateable trait that allows clearing the cached constraints map, forcing re-evaluation on next access. This is useful in testing scenarios where shared constraints may need to be re-evaluated after configuration changes, particularly during parallel test execution where constraints may be evaluated before doWithConfig() has registered shared constraints. The method enables proper test isolation by allowing tests to clear the constraint cache in setup/cleanup methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new public API method clearConstraintsMapCache() to the Validateable trait to enable clearing the cached constraints map for test isolation during parallel test execution. The trait caches constraints in a private static field, and during parallel tests, constraints may be evaluated before configuration has registered shared constraints, causing test failures.
Changes:
- Added
clearConstraintsMapCache()static method to theValidateabletrait with proper documentation - Added test isolation in setup/cleanup methods for tests using shared constraints
- Tests now clear both
ConstraintEvalUtils.defaultConstraintsMapand per-class constraints caches
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| grails-validation/src/main/groovy/grails/validation/Validateable.groovy | Added new clearConstraintsMapCache() public API method to clear the cached constraints map |
| grails-validation/src/test/groovy/grails/validation/ValidateableTraitSpec.groovy | Added setup/cleanup methods to clear SharedConstraintsValidateable constraints cache |
| grails-validation/src/test/groovy/grails/validation/ValidateableTraitAdHocSpec.groovy | Added setup/cleanup methods to clear PersonAdHocSharedConstraintsValidateable constraints cache |
| grails-test-suite-web/src/test/groovy/org/grails/web/commandobjects/CommandObjectsSpec.groovy | Added setup/cleanup methods to clear Artist and ArtistSubclass constraints caches plus ConstraintEvalUtils cache |
| grails-test-suite-web/src/test/groovy/org/grails/web/commandobjects/CommandObjectNoDataSpec.groovy | Added setup/cleanup methods to clear Artist constraints cache plus ConstraintEvalUtils cache |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
matrei
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we change the wording in the comments to move away from "parallel test execution" as that is another concept that is not in play here?
Update Javadoc and comments across constraint cache clearing code to use clearer 'prevent test environment pollution' wording instead of 'parallel test execution'.
|
All comments updated to a variant of "prevents test environment pollution" |
Summary
Add a new static method
clearConstraintsMapCache()to theValidateabletrait that allows clearing the cached constraints map, forcing re-evaluation on next access.Motivation
The
Validateabletrait caches evaluated constraints in a private static fieldconstraintsMapInternal. During parallel test execution, constraints may be evaluated beforedoWithConfig()has registered shared constraints, causing test failures where shared constraint names (like'isProg') are not found.This new public API method enables proper test isolation by allowing tests to clear the constraint cache in setup/cleanup methods.
API Addition
Usage Example
Why This is a Feature (7.1)
Per the Grails versioning policy, adding a new public method to a trait is a backward-compatible feature addition that belongs in a MINOR release (7.1.x), not a PATCH release (7.0.x).
A companion change in PR #15335 (targeting 7.0.x) uses reflection to achieve the same test isolation without modifying the public API.
Files Changed
grails-validation/src/main/groovy/grails/validation/Validateable.groovy- Add newclearConstraintsMapCache()methodgrails-validation/src/test/groovy/grails/validation/ValidateableTraitSpec.groovy- Add test isolationgrails-validation/src/test/groovy/grails/validation/ValidateableTraitAdHocSpec.groovy- Add test isolationgrails-test-suite-web/src/test/groovy/org/grails/web/commandobjects/CommandObjectsSpec.groovy- Add test isolationgrails-test-suite-web/src/test/groovy/org/grails/web/commandobjects/CommandObjectNoDataSpec.groovy- Add test isolation