Permalink
Browse files

fix for GRAILS-4746

  • Loading branch information...
1 parent d9556fc commit d9f637facef166d2a91df1c64e16322c03416dda @graemerocher graemerocher committed Jul 22, 2009
View
62 grails/src/java/org/codehaus/groovy/grails/web/binding/GrailsDataBinder.java
@@ -641,29 +641,57 @@ private void bindCollectionAssociation(MutablePropertyValues mpvs, PropertyValue
Collection collection = (Collection) bean.getPropertyValue(pv.getName());
collection.clear();
- if(v != null && v.getClass().isArray()) {
- Object[] identifiers = (Object[])v;
- for (Object id : identifiers) {
- if (id != null) {
- associateObjectForId(pv, id);
+ final Class associatedType = getReferencedTypeForCollection(pv.getName(), getTarget());
+ final boolean isArray = v != null && v.getClass().isArray();
+ if(isDomainAssociation(associatedType)) {
+ if(isArray) {
+
+ Object[] identifiers = (Object[])v;
+ for (Object id : identifiers) {
+ if (id != null) {
+ associateObjectForId(pv, id,associatedType);
+ }
}
- }
- mpvs.removePropertyValue(pv);
+ mpvs.removePropertyValue(pv);
+ }
+ else if(v!=null && (v instanceof String)) {
+ associateObjectForId(pv,v, associatedType);
+ mpvs.removePropertyValue(pv);
+ }
}
- else if(v!=null && (v instanceof String)) {
- associateObjectForId(pv,v);
- mpvs.removePropertyValue(pv);
+ else if(GrailsDomainConfigurationUtil.isBasicType(associatedType)) {
+ if(isArray) {
+ Object[] values = (Object[])v;
+ List list = collection instanceof List ? (List)collection : null;
+ for (int i = 0; i < values.length; i++) {
+ Object value = values[i];
+ try {
+ Object newValue = getTypeConverter().convertIfNecessary(value, associatedType);
+ if(list!=null) {
+ if(i>list.size()-1) {
+ list.add(i,newValue);
+ }
+ else {
+ list.set(i, newValue);
+ }
+ }
+ else {
+ collection.add(newValue);
+ }
+ }
+ catch (TypeMismatchException e) {
+ // ignore
+ }
+ }
+ }
}
}
- private void associateObjectForId(PropertyValue pv, Object id) {
+ private void associateObjectForId(PropertyValue pv, Object id, Class associatedType) {
final Object target = getTarget();
- final Class associatedType = getReferencedTypeForCollection(pv.getName(), target);
- if(isDomainAssociation(associatedType)) {
- final Object obj = getPersistentInstance(associatedType, id);
- addAssociationToTarget(pv.getName(), target, obj);
- }
+ final Object obj = getPersistentInstance(associatedType, id);
+ addAssociationToTarget(pv.getName(), target, obj);
}
private boolean isDomainAssociation(Class associatedType) {
@@ -685,7 +713,7 @@ private Class getReferencedTypeForCollection(String name, Object target) {
GrailsDomainClass domainClass = (GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, target.getClass().getName());
if(domainClass!=null) {
GrailsDomainClassProperty domainProperty = domainClass.getPropertyByName(name);
- if(domainProperty!=null && (domainProperty.isOneToMany()|| domainProperty.isManyToMany())) {
+ if(domainProperty!=null) {
return domainProperty.getReferencedPropertyType();
}
}
View
49 .../src/test/org/codehaus/groovy/grails/web/binding/BindStringArrayToGenericListTests.groovy
@@ -0,0 +1,49 @@
+package org.codehaus.groovy.grails.web.binding
+
+import org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerTests
+
+/**
+ * @author Graeme Rocher
+ * @since 1.1
+ */
+
+public class BindStringArrayToGenericListTests extends AbstractGrailsControllerTests{
+
+ protected void onSetUp() {
+ gcl.parseClass('''
+import grails.persistence.*
+
+@Entity
+class Menu {
+
+ String name
+ static hasMany = [items: String]
+
+ List<String> items
+
+}
+
+class MenuController {
+
+ def save = {
+ def m = new Menu(params)
+ [menu:m]
+ }
+}
+''')
+ }
+
+ void testBindStringArrayToGenericList() {
+ def controller = ga.getControllerClass("MenuController").newInstance()
+
+ controller.params.name = "day"
+ controller.params.items = ['rice', 'soup'] as String[]
+
+ def model = controller.save()
+
+ assertEquals( ['rice', 'soup'], model.menu.items )
+
+ }
+
+
+}

0 comments on commit d9f637f

Please sign in to comment.