apache / apache/grails-core

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

Open
#12,368 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### 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

Contributor guide

Open the contributing guide

Research direction

Start with the Application.groovy registration examples and inspect DefaultConstraintEvaluator, ConstraintsEvaluator, and JavaxValidatorRegistry to understand the protected constraintRegistry access under @CompileStatic. Determine whether the public interface or evaluator property should expose registration, then verify that a statically compiled application can register ExampleConstraint as described.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.