Skip to content
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 @@ -344,6 +344,8 @@ class SimpleDataBinder implements DataBinder {
} else if(genericType.isEnum() && val instanceof CharSequence) {
def enumValue = convertStringToEnum(genericType, val.toString())
addElementToCollectionAt obj, propName, collectionInstance, index, enumValue
} else {
addElementToCollectionAt obj, propName, collectionInstance, index, convert(genericType, val)
}
} else {
addElementToCollectionAt obj, propName, collectionInstance, index, val
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package grails.databinding


import grails.databinding.converters.ValueConverter
import grails.databinding.errors.BindingError
import grails.databinding.events.DataBindingListenerAdapter
import org.grails.databinding.converters.DateConversionHelper
Expand Down Expand Up @@ -574,6 +574,42 @@ class SimpleDataBinderSpec extends Specification {
widget.listOfLongs == [4L]
widget.listOfLongs.first().getClass() == Long
}

@Issue('https://github.com/grails/grails-core/issues/11235')
void 'Test binding to a list using custom value converters'() {
given:
def binder = new SimpleDataBinder()
def comment = new Comment()

and:
binder.registerConverter(new ValueConverter() {
@Override
boolean canConvert(Object value) {
value instanceof String
}

@Override
Object convert(Object value) {
new Attachment(filename: "$value")
}

@Override
Class<?> getTargetType() {
return Attachment
}
})

when:
binder.bind comment, [
'attachments[0]': 'foo.txt',
'attachments[1]': 'bar.txt'
] as SimpleMapDataBindingSource

then:
comment.attachments.size() == 2
comment.attachments.find { it.filename == 'foo.txt' }
comment.attachments.find { it.filename == 'bar.txt' }
}
}

class Factory {
Expand Down Expand Up @@ -642,3 +678,11 @@ class DateCollection {
List<Date> dates
}

class Comment {
Set<Attachment> attachments
}

class Attachment {
String filename
}