Skip to content

Commit 55b84a6

Browse files
authored
Enforce data binding to instance should not affect static properties (#12664)
1 parent 5b42019 commit 55b84a6

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

grails-databinding/src/main/groovy/grails/databinding/SimpleDataBinder.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.grails.databinding.xml.GPathResultMap
3232
import java.lang.annotation.Annotation
3333
import java.lang.reflect.Array
3434
import java.lang.reflect.Field
35+
import java.lang.reflect.Modifier
3536
import java.lang.reflect.ParameterizedType
3637
import java.security.ProtectionDomain
3738

@@ -263,7 +264,10 @@ class SimpleDataBinder implements DataBinder {
263264
}
264265

265266
protected boolean isOkToBind(MetaProperty property, List whitelist, List blacklist) {
266-
isOkToBind(property.name, whitelist, blacklist) && (property.type != null && !(ClassLoader.class.isAssignableFrom(property.type) || ProtectionDomain.class.isAssignableFrom(property.type)))
267+
isOkToBind(property.name, whitelist, blacklist) &&
268+
(property.type != null) &&
269+
!Modifier.isStatic(property.modifiers) &&
270+
!(ClassLoader.class.isAssignableFrom(property.type) || ProtectionDomain.class.isAssignableFrom(property.type))
267271
}
268272

269273
protected IndexedPropertyReferenceDescriptor getIndexedPropertyReferenceDescriptor(propName) {

grails-databinding/src/test/groovy/grails/databinding/SimpleDataBinderSpec.groovy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,31 @@ class SimpleDataBinderSpec extends Specification {
610610
comment.attachments.find { it.filename == 'foo.txt' }
611611
comment.attachments.find { it.filename == 'bar.txt' }
612612
}
613+
614+
void 'Test binding when property is static Object, it should not bind'() {
615+
given:
616+
def binder = new SimpleDataBinder()
617+
def widget = new Widget()
618+
619+
when:
620+
binder.bind widget, [objectStaticProp: 6174] as SimpleMapDataBindingSource
621+
622+
then:
623+
widget.objectStaticProp instanceof Closure
624+
}
625+
626+
void 'Test binding when property is static, it should not bind'() {
627+
given:
628+
def binder = new SimpleDataBinder()
629+
def widget = new Widget()
630+
631+
when:
632+
binder.bind widget, [stringStaticProp: 'abc'] as SimpleMapDataBindingSource
633+
634+
then:
635+
widget.stringStaticProp == 'unchanged'
636+
}
637+
613638
}
614639

615640
class Factory {
@@ -639,6 +664,8 @@ class Widget {
639664
List<Integer> listOfIntegers = []
640665
List<Long> listOfLongs = []
641666
Set<Factory> factories
667+
static objectStaticProp = { -> 495 }
668+
static String stringStaticProp = 'unchanged'
642669
}
643670

644671
class Gadget extends Widget {

0 commit comments

Comments
 (0)