From de08017bcc56878441c4c40cf8608bac736dcc51 Mon Sep 17 00:00:00 2001 From: David Kron Date: Wed, 17 Aug 2022 18:10:57 +0200 Subject: [PATCH] respect defaultNullable() of Validateable classes (issue #218) --- .../BeanPropertyAccessorFactory.groovy | 5 ++- .../CommandPropertyAccessorSpec.groovy | 36 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/groovy/grails/plugin/formfields/BeanPropertyAccessorFactory.groovy b/src/main/groovy/grails/plugin/formfields/BeanPropertyAccessorFactory.groovy index cad061e3..128233d7 100644 --- a/src/main/groovy/grails/plugin/formfields/BeanPropertyAccessorFactory.groovy +++ b/src/main/groovy/grails/plugin/formfields/BeanPropertyAccessorFactory.groovy @@ -18,6 +18,7 @@ package grails.plugin.formfields import grails.core.GrailsApplication import grails.gorm.validation.DefaultConstrainedProperty +import grails.validation.Validateable import groovy.transform.PackageScope import grails.core.support.GrailsApplicationAware import grails.core.support.proxy.ProxyHandler @@ -110,7 +111,9 @@ class BeanPropertyAccessorFactory implements GrailsApplicationAware { } private Constrained resolveConstraints(BeanWrapper beanWrapper, String propertyName) { - grails.gorm.validation.Constrained constraint = constraintsEvaluator.evaluate(beanWrapper.wrappedClass)[propertyName] + Class type = beanWrapper.wrappedClass + boolean defaultNullable = Validateable.class.isAssignableFrom(type) ? type.metaClass.invokeStaticMethod(type, 'defaultNullable') : false + grails.gorm.validation.Constrained constraint = constraintsEvaluator.evaluate(type, defaultNullable)[propertyName] if (!constraint) { constraint = createDefaultConstraint(beanWrapper, propertyName) } diff --git a/src/test/groovy/grails/plugin/formfields/CommandPropertyAccessorSpec.groovy b/src/test/groovy/grails/plugin/formfields/CommandPropertyAccessorSpec.groovy index 9437396e..6d4a5026 100644 --- a/src/test/groovy/grails/plugin/formfields/CommandPropertyAccessorSpec.groovy +++ b/src/test/groovy/grails/plugin/formfields/CommandPropertyAccessorSpec.groovy @@ -1,7 +1,11 @@ package grails.plugin.formfields -import grails.plugin.formfields.mock.* -import spock.lang.* + +import grails.plugin.formfields.mock.Gender +import grails.plugin.formfields.mock.Person +import grails.validation.Validateable +import spock.lang.Issue +import spock.lang.Unroll @Unroll class CommandPropertyAccessorSpec extends BuildsAccessorFactory { @@ -256,6 +260,20 @@ class CommandPropertyAccessorSpec extends BuildsAccessorFactory { propertyAccessor.constraints.blank } + @Issue('https://github.com/grails-fields-plugin/grails-fields/issues/218') + void 'respect defaultNullable() when evaluating constraints of a Validateable'() { + given: + ValidateableCommand command = new ValidateableCommand() + DefaultNullableValidateableCommand command2 = new DefaultNullableValidateableCommand() + + and: + def propertyAccessor = factory.accessorFor(command, 'myNullableProperty') + def propertyAccessor2 = factory.accessorFor(command2, 'myNullableProperty') + + expect: + !propertyAccessor.constraints.nullable + propertyAccessor2.constraints.nullable + } } class TestCommand { @@ -278,4 +296,16 @@ class TestCommand { class UnconstrainedCommand { String stringProperty -} \ No newline at end of file +} + +class ValidateableCommand implements Validateable { + String myNullableProperty +} + +class DefaultNullableValidateableCommand implements Validateable { + String myNullableProperty + + static boolean defaultNullable() { + return true + } +}