Skip to content

Commit 1b9e214

Browse files
committed
fix for GRAILS-9011 "Validation exception thrown on adding object and inherited object to a collection-Domain Mocks - Unit test"
1 parent 21d4df5 commit 1b9e214

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

grails-core/src/main/groovy/org/codehaus/groovy/grails/validation/GrailsDomainClassValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public boolean supports(Class clazz) {
6666
*/
6767
@SuppressWarnings({ "unchecked", "rawtypes" })
6868
public void validate(Object obj, Errors errors, boolean cascade) {
69-
if (obj == null || !domainClass.getFullName().equals(obj.getClass().getName())) {
69+
if (obj == null) {
7070
throw new IllegalArgumentException("Argument [" + obj + "] is not an instance of [" +
7171
domainClass.getClazz() + "] which this validator is configured for");
7272
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package grails.test.mixin
2+
3+
import grails.persistence.Entity
4+
import org.junit.Test
5+
6+
/**
7+
* Test for GRAILS-9010
8+
*/
9+
@Mock([AbstractCustomPropertyValue])
10+
class InheritanceWithValidationTests {
11+
12+
@Test
13+
void testNewStringValue () {
14+
CustomProperty property = new CustomProperty ()
15+
16+
AbstractCustomPropertyValue propertyValue = property.newValue ("testValue")
17+
assertValid (propertyValue)
18+
}
19+
20+
private void assertValid (def propertyValue) {
21+
assert propertyValue.valid
22+
23+
def result = propertyValue.validate () // fails here with: java.lang.IllegalArgumentException:
24+
// Argument [org.example.StringPropertyValue : null] is not an instance of
25+
// [class org.example.AbstractCustomPropertyValue] which this validator is configured for
26+
assert result == true
27+
}
28+
}
29+
30+
@Entity
31+
class AbstractCustomPropertyValue {
32+
33+
boolean valid = false
34+
35+
static constraints = {
36+
valid (validator: validator)
37+
}
38+
39+
static transients = ['valid']
40+
41+
protected static validator = { value, instance ->
42+
if (!instance.valid) {
43+
return 'invalid.value.for.type'
44+
}
45+
46+
return null // returning null means valid
47+
}
48+
}
49+
50+
@Entity
51+
class CustomProperty {
52+
53+
public AbstractCustomPropertyValue newValue (String value) {
54+
return new StringPropertyValue (value)
55+
}
56+
}
57+
58+
59+
@Entity
60+
class StringPropertyValue extends AbstractCustomPropertyValue {
61+
62+
String stringValue
63+
64+
static constraints = {
65+
stringValue (nullable: true)
66+
}
67+
68+
public StringPropertyValue (String value) {
69+
this.stringValue = value
70+
this.valid = true
71+
}
72+
}

0 commit comments

Comments
 (0)