Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

respect defaultNullable() of Validateable classes (issue #218) #320

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -278,4 +296,16 @@ class TestCommand {

class UnconstrainedCommand {
String stringProperty
}
}

class ValidateableCommand implements Validateable {
String myNullableProperty
}

class DefaultNullableValidateableCommand implements Validateable {
String myNullableProperty

static boolean defaultNullable() {
return true
}
}