grails-core icon indicating copy to clipboard operation
grails-core copied to clipboard

Its not possible to register a custom constraint in Application.groovy unless its not @CompileStatic which it is by deafult

Open boardbloke opened this issue 2 years ago • 0 comments

Expected Behavior

It should be possible to register a custom constraint (i.e. a class that implements Constraint) in a default web-app using e.g.:

     // register constraints with domain class validator
      JavaxValidatorRegistry registry = (JavaxValidatorRegistry)applicationContext.getBean(ValidatorRegistry)
      registry.addConstraint(ExampleConstraint, [CharSequence])

      // register constraint with @Validateable validator
      DefaultConstraintEvaluator evaluator = (DefaultConstraintEvaluator)applicationContext.getBean(ConstraintsEvaluator)
      evaluator.constraintRegistry.addConstraint ExampleConstraint

However evaluator.constraintRegistry fails because its a protected property of DefaultConstraintEvaluator

You have to remove the @CompileStatic from the Application class to get it to work

Either a means to register a customer constraint ought to be added to the ConstraintsEvaluator interface, or the constraintRegistry should be made pubic so it can be accessed from statically compiled code

Actual Behaviour

Its not possible to register a constraint unless you remove the @CompileStatic from the Application class

Steps To Reproduce

  1. grails create-app to create a baseline App
  2. Create a custom constraint, e.g. ExampleConstraint
  3. Attempt to register it in the Application class as above

Environment Information

No response

Example Application

No response

Version

5.1.2

boardbloke avatar Feb 01 '22 17:02 boardbloke