apache / apache/grails-core

Grails 4.0.3: Inherited domain with constraints throws error when validating on child property constraint

Open
#11,527 7 comments 0 reactions 1 assignee Claimed by @niravassar View on GitHub
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

When a domain hierarchy exists, having a constraint on the child class that checks a field of the child class will cause a grails error `Property [xxx] is not a valid property of class example.` when saving the child object.

This only occurs when the constraint is present, in the referenced example, if you remove the "unique" constraint on the child objects, they can be saved just fine. This worked in grails 3.

The error claims to come from the abstract parent object's validation, that the field does not exist on the object, even though it does exist on the concrete child object which was instantiated and saved.

### Task List

- [X] Steps to reproduce provided
- [X] Stacktrace (if present) provided
- [X] Example that reproduces the problem uploaded to Github
- [X] Full description of the issue provided (see above)

### Steps to Reproduce

1. Run the referenced application
2. Navigate to the "owner" controller default action
3. See error in log

Remove the constraints from Cat and Dog classes, restart application and repeat steps, the error does not occur

In the attached application, `PetObject` is abstract, `Dog` extends `PetObject` and has an additional property that it belongs to called `dogHouse`

Dog class:
```
class Dog extends PetObject
{
DogHouse dogHouse
static belongsTo = DogHouse
static constraints = {
name(blank:false,unique:'dogHouse', maxSize:255)
}
}
```

When attempting to create the object as follows
```
Dog dog = new Dog(name: "Rover", description: "Brown", petType: PetType.DOG, dogHouse: dogOwner)
dogOwner.addToDogs(dog)
dog.save()
```

The exception is thrown

Stack Trace:
```
IllegalArgumentException occurred when processing request: [GET] /owner/index
Property [dogHouse] is not a valid property of class example.PetObject. Stacktrace follows:

java.lang.reflect.InvocationTargetException: null
at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Property [dogHouse] is not a valid property of class example.PetObject
at org.grails.datastore.mapping.reflect.FieldEntityAccess$FieldEntityReflector.getPropertyReader(FieldEntityAccess.java:268)
at org.grails.datastore.mapping.reflect.FieldEntityAccess$FieldEntityReflector.getProperty(FieldEntityAccess.java:286)
at org.grails.datastore.gorm.validation.constraints.builtin.UniqueConstraint$_processValidate_closure1.doCall(UniqueConstraint.groovy:111)
at org.grails.datastore.gorm.query.criteria.AbstractDetachedCriteria.build(AbstractDetachedCriteria.groovy:821)
at grails.gorm.DetachedCriteria.build(DetachedCriteria.groovy:583)
at org.grails.datastore.gorm.validation.constraints.builtin.UniqueConstraint.processValidate(UniqueConstraint.groovy:106)
at org.grails.datastore.gorm.validation.constraints.AbstractConstraint.validate(AbstractConstraint.java:88)
at grails.gorm.validation.DefaultConstrainedProperty.validate(DefaultConstrainedProperty.groovy:601)
at grails.gorm.validation.PersistentEntityValidator.validatePropertyWithConstraint(PersistentEntityValidator.groovy:305)
at grails.gorm.validation.PersistentEntityValidator.validate(PersistentEntityValidator.groovy:76)
at org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.save(AbstractHibernateGormInstanceApi.groovy:124)
at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:119)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:100)
at example.OwnerController$_index_closure1.doCall(OwnerController.groovy:16)
at grails.gorm.transactions.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:94)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
at grails.gorm.transactions.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:91)
at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:1014)
at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:966)
at org.grails.datastore.gorm.GormStaticApi.withNewTransaction(GormStaticApi.groovy:927)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.withNewTransaction(GormEntity.groovy:954)
at example.OwnerController.index(OwnerController.groovy:6)
... 13 common frames omitted
```

### Expected Behaviour

Domain class should validate and save

### Actual Behaviour

Error above is thrown

### Environment Information

- **Operating System**: MacOS Mojave Version 10.14.6
- **Grails Version:** 4.0.3
- **JDK Version:**
OpenJDK Runtime Environment (Zulu 8.33.0.1-macosx) (build 1.8.0_192-b01)
OpenJDK 64-Bit Server VM (Zulu 8.33.0.1-macosx) (build 25.192-b01, mixed mode)

- **Container Version (If Applicable):** Tested on both Tomcat and Undertow, same error on both

### Example Application

https://github.com/Trebla7th/abstract-domain-validation-error

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.