grails-ormgrails-3.0grails-3.3

bindData() is changing the domain id when using gorm inheritance


I have a grails domain class that extends a base class in the src folder, like so:

// src/main/groovy/demo/MyBaseClass.groovy
package demo

import grails.gorm.dirty.checking.DirtyCheck

@DirtyCheck
class MyBaseClass {
    String name
}
// grails-app/domain/demo/MyDomain.groovy
package demo

class MyDomain extends MyBaseClass {
    Date birthDate
}

However, when using bindData in the controller, the id of MyDomain can be changed by the request params. This can be checked with this failing test:

// src/test/groovy/demo/IdDataBindingSpec.groovy
package demo

import grails.web.databinding.DataBinder
import org.grails.testing.GrailsUnitTest
import spock.lang.Specification

class IdDataBindingSpec extends Specification 
        implements GrailsUnitTest, DataBinder {

    void "Bind id to MyDomain is not possible"() {
        given: "A new instance"
            MyDomain myDomain = new MyDomain()
        and: "Request params"
            Map params = [id:5, name:'test']

        when: "Binding data"
            bindData(myDomain, params)

        then: "id is not updated"
            !myDomain.id
        and: "Other params are updated"
            myDomain.name == 'test'
    }
}

Any normal domain will pass the above test, but the domain extending a base class from src will not. How can i fix this behavior?

Edit: I'm using Grails 3.3.8 with gorm 6.1


Solution

  • When using gorm inheritance, the base classes in src must be declared as abstract, like so:

    // src/main/groovy/demo/MyBaseClass.groovy
    package demo
    
    import grails.gorm.dirty.checking.DirtyCheck
    
    @DirtyCheck
    abstract class MyBaseClass {
        String name
    }