Permalink
Browse files

[#1339] Fix NPE when binding request parameters that do not have a pr…

…efix
  • Loading branch information...
1 parent 2f0d794 commit 9e5c3d64f15ece6f7e349edc3e8a31f257034db2 @swallez swallez committed Dec 27, 2011
@@ -272,7 +272,7 @@ public static void bindBean(RootParamNode rootParamNode, String name, Object bea
return;
}
- ParamNode paramNode = rootParamNode.getChild(name);
+ ParamNode paramNode = StringUtils.isEmpty(name) ? rootParamNode : rootParamNode.getChild(name);
try {
internalBindBean(paramNode, bean, new BindingAnnotations());
@@ -551,12 +551,12 @@ public static Object directBind(String name, Annotation[] annotations, String va
if (clazz.equals(String.class)) {
return value;
}
-
+
// Handles the case where the model property is a sole character
if (clazz.equals(Character.class)) {
return value.charAt(0);
}
-
+
// Enums
if (Enum.class.isAssignableFrom(clazz)) {
return nullOrEmpty ? null : Enum.valueOf((Class<Enum>) clazz, value);
@@ -3,6 +3,8 @@
import org.junit.Before;
import org.junit.Test;
import play.PlayBuilder;
+import play.data.validation.Validation;
+import play.data.validation.ValidationPlugin;
import java.lang.annotation.Annotation;
import java.util.*;
@@ -151,6 +153,51 @@ public void verify_binding_of_simple_bean_collections() throws Exception {
assertThat(lst.get(12).a).isEqualTo("a12");
}
+ @Test
+ @SuppressWarnings("deprecation")
+ public void verify_binding_of_root_parameters() throws Exception {
+ Map<String, String[]> params = new HashMap<String, String[]>();
+ params.put("a", new String[] {"foo"});
+ params.put("b", new String[] {"2"});
+
+ RootParamNode rootParamNode = ParamNode.convert(params);
+ Data1 data1 = new Data1();
+ Binder.bindBean(rootParamNode, "", data1);
+
+ assertThat(data1.a).isEqualTo("foo");
+ assertThat(data1.b).isEqualTo(2);
+
+ // Also test with the old deprecated but shorter form
+ data1 = new Data1();
+ Binder.bind(data1, null, params);
+ assertThat(data1.a).isEqualTo("foo");
+ assertThat(data1.b).isEqualTo(2);
+
+ }
+
+ @Test
+ @SuppressWarnings("deprecation")
+ public void verify_validation_error_of_root_parameters() throws Exception {
+ // Initialize Validation.current()
+ new ValidationPlugin().beforeInvocation();
+
+ Map<String, String[]> params = new HashMap<String, String[]>();
+ params.put("a", new String[] {"foo"});
+ params.put("b", new String[] {"bar"});
+
+ RootParamNode rootParamNode = ParamNode.convert(params);
+ Data1 data1 = new Data1();
+ Binder.bindBean(rootParamNode, null, data1);
+
+ assertThat(Validation.error("a")).isNull();
+ assertThat(Validation.error("b")).isNotNull();
+
+ // Also test with the old deprecated but shorter form
+ data1 = new Data1();
+ Binder.bind(data1, null, params);
+ assertThat(Validation.error("a")).isNull();
+ assertThat(Validation.error("b")).isNotNull();
+ }
/**
* Transforms map from Unbinder to Binder

0 comments on commit 9e5c3d6

Please sign in to comment.