Skip to content

Commit

Permalink
GRAILS-11175 - improve binding XML to non domain objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Scott Brown committed Mar 4, 2014
1 parent 0d343b5 commit 22f8915
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import grails.persistence.Entity
import grails.test.mixin.Mock
import grails.test.mixin.TestMixin
import grails.test.mixin.domain.DomainClassUnitTestMixin
import grails.validation.Validateable

import org.codehaus.groovy.grails.web.binding.GrailsWebDataBinder

Expand Down Expand Up @@ -71,6 +72,40 @@ class GrailsWebDataBinderBindingXmlSpec extends Specification {
writer.books[1].publisher == 'Publisher Two'
writer.books[1].title == 'Book Two'
}

@Issue('GRAILS-11175')
void 'Test binding a single XML child element to a List in a non domain class'() {
given:
def binder = new GrailsWebDataBinder(grailsApplication)
def obj = new CommandObject()

when:
def xml = new XmlSlurper().parseText("""
<commandObject>
<somethings>
<something><name>One</name></something>
</somethings>
</commandObject>
""")
binder.bind obj, xml

then:
println obj.errors
!obj.hasErrors()
obj.somethings?.size() == 1
obj.somethings[0].name == 'One'
}


}

@Validateable
class CommandObject {
List<Something> somethings
}

class Something {
String name
}

@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class GrailsWebDataBinder extends SimpleDataBinder {
}
} else if(Collection.isAssignableFrom(metaProperty.type)) {
def referencedType = getReferencedTypeForCollection(propName, obj)
if(referencedType && isDomainClass(referencedType)) {
if(referencedType) {
def listValue
if(val instanceof List) {
listValue = (List)val
Expand All @@ -301,13 +301,15 @@ class GrailsWebDataBinder extends SimpleDataBinder {
def itemsWhichNeedBinding = []
listValue.each { item ->
def persistentInstance
if(item instanceof Map || item instanceof DataBindingSource) {
def idValue = getIdentifierValueFrom(item)
if(idValue != null) {
persistentInstance = getPersistentInstance(referencedType, idValue)
if(persistentInstance != null) {
bind persistentInstance, new SimpleMapDataBindingSource(item), listener
itemsWhichNeedBinding << persistentInstance
if(isDomainClass(referencedType)) {
if(item instanceof Map || item instanceof DataBindingSource) {
def idValue = getIdentifierValueFrom(item)
if(idValue != null) {
persistentInstance = getPersistentInstance(referencedType, idValue)
if(persistentInstance != null) {
bind persistentInstance, new SimpleMapDataBindingSource(item), listener
itemsWhichNeedBinding << persistentInstance
}
}
}
}
Expand Down

0 comments on commit 22f8915

Please sign in to comment.