BillGoneMad
BillGoneMad

Reputation: 77

Grails 4 hasone not validating correctly

I'm working on upgrading a fairly large Grails 2.5 app to Grails 4.0.3. I have something similar in my domain structure to my example here.

package com.test

class Face {

    static hasOne = [mouth: Mouth]

    static constraints = {
    }
}

package com.test

abstract class Mouth {
    Face face

    static constraints = {
    }
}

package com.test

class BigMouth extends Mouth {
    Integer numberOfTeeth
    Boolean tonsilsRemoved

}

The behavior I expect to see when validating a face object is that it should also validate the mouth object. So I have a test Controller set up to test that like such

package com.test

class TestController {

    def index() { 
        Face face = new Face()
        face.mouth = new BigMouth(
                face: face
        )

        // numberOfTeeth & tonsilsRemoved cannot be null, but are
        assert !face.validate()
        println 'no failure'
    }
}

And a test case

package com.test

import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification

class FaceSpec extends Specification implements DomainUnitTest<Face> {

    def setup() {
    }

    def cleanup() {
    }

    void "test hasOneValidation"() {
        when:
        domain.mouth = new BigMouth(numberOfTeeth: null)

        then:
        !domain.validate()
    }
}

However, the behavior I'm seeing is that face is valid and it is not passing the assert statement. This worked for us in 2.5. Another note on this test case is that If I move the BigMouth Properties to Mouth and make that a concrete class, The same test will have a face object that fails vaildation and passes the assert statement.

Is this expected behavior for Grails 4.0.3 or not? If not, am I doing something blatantly wrong here?

Running Grails 4.0.3, Java 11, Windows 10

Upvotes: 1

Views: 139

Answers (1)

Trebla
Trebla

Reputation: 1172

This appears to be caused by this bug in Grails 4. Domain hierarchy is attempting to apply child constraints on the parent object, and failing to do so because properties of the child don't exist on the parent.

Upvotes: 0

Related Questions