Skip to content

Conversation

@jamesfredley
Copy link
Contributor

Summary

Add a new static method clearConstraintsMapCache() to the Validateable trait that allows clearing the cached constraints map, forcing re-evaluation on next access.

Motivation

The Validateable trait caches evaluated constraints in a private static field constraintsMapInternal. During parallel test execution, constraints may be evaluated before doWithConfig() 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

/**
 * Clears 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.
 *
 * @since 7.1
 */
@Generated
static void clearConstraintsMapCache()

Usage Example

class CommandObjectsSpec extends Specification implements ControllerUnitTest<TestController> {
    
    Closure doWithConfig() {{ config ->
        config['grails.gorm.default.constraints'] = {
            isProg inList: ['Emerson', 'Lake', 'Palmer']
        }
    }}

    def setup() {
        ConstraintEvalUtils.clearDefaultConstraints()
        Artist.clearConstraintsMapCache()
    }

    def cleanup() {
        ConstraintEvalUtils.clearDefaultConstraints()
        Artist.clearConstraintsMapCache()
    }
}

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 new clearConstraintsMapCache() method
  • grails-validation/src/test/groovy/grails/validation/ValidateableTraitSpec.groovy - Add test isolation
  • grails-validation/src/test/groovy/grails/validation/ValidateableTraitAdHocSpec.groovy - Add test isolation
  • grails-test-suite-web/src/test/groovy/org/grails/web/commandobjects/CommandObjectsSpec.groovy - Add test isolation
  • grails-test-suite-web/src/test/groovy/org/grails/web/commandobjects/CommandObjectNoDataSpec.groovy - Add test isolation

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.
Copy link
Contributor

Copilot AI left a 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 the Validateable trait with proper documentation
  • Added test isolation in setup/cleanup methods for tests using shared constraints
  • Tests now clear both ConstraintEvalUtils.defaultConstraintsMap and 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.

@jamesfredley jamesfredley moved this to In Progress in Apache Grails Jan 25, 2026
@jamesfredley jamesfredley added this to the grails:7.1.0 milestone Jan 25, 2026
@jamesfredley jamesfredley self-assigned this Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant