Skip to content

skipValidate logic in GormValidateable causes missed validations and is inconsistent #1155

@longwa

Description

@longwa

GormValidateable has a property skipValidate that is trying to skip calling validate multiple times when an entity is saved and then later flushed.

However, it has the potential to cause validations to be skipped under some scenarios. It's also completely ignored for explicit calls to validate() without a list of properties, which seems inconsistent.

The attached project's AuthorSpec test does the following:

  1. Create a new valid Author and call save but don't flush
  2. Update the name attribute on the Author instance to an invalid state
  3. Call validate(['name']) to re-validate the author instance
  4. Validation should fail, however step 1 left the Author with skipValidate = true so validation is skipped instead and the validate returns true

If you call validate without any properties, the validation will fail as expected, which seems like a weird side effect as well.

The problem is that this finally block:

https://github.com/grails/grails-data-mapping/blob/ef1730a42bc515cfb96cf9175d81e344d8a08a61/grails-datastore-gorm-hibernate-core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormInstanceApi.groovy#L162

Incorrectly restores the skipValidate based on the shouldFlush flag, which leaves the non-flushed object in this state.

It almost seems like it should just restore the skipValidate to false after everything is finished instead of tying it to !shouldFlush.

  • **GORM Version: 6.1.10.RELEASE
  • **Grails Version (if using Grails): 3.3.8
  • **JDK Version: 1.8.0_111

Example Application

https://github.com/longwa/gorm-skipValidate-bug

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions