diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonDeserialization.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonDeserialization.java index 4c51078c4..fb00d23d3 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonDeserialization.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonDeserialization.java @@ -23,6 +23,7 @@ import java.io.InputStreamReader; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; import java.nio.charset.Charset; import javax.enterprise.inject.Instance; @@ -120,7 +121,7 @@ public Object[] deserialize(InputStream inputStream, ControllerMethod method) { JsonElement node = root.get(parameter.getName()); if (deserializee.isWithoutRoot()) { - values[i] = gson.fromJson(root, parameter.getParameterizedType()); + values[i] = gson.fromJson(root, fallbackTo(parameter.getParameterizedType(), types[i])); logger.info("json without root deserialized"); break; @@ -156,6 +157,11 @@ public Object[] deserialize(InputStream inputStream, ControllerMethod method) { return values; } + private Type fallbackTo(Type parameterizedType, Class type) { + if (parameterizedType instanceof TypeVariable) return type; + return parameterizedType; + } + private String getContentOfStream(InputStream input) throws IOException { Charset charset = Charset.forName(getRequestCharset()); logger.debug("Using charset {}", charset); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java index 90b36c528..12cc29835 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java @@ -87,9 +87,9 @@ public class GsonDeserializerTest { private ControllerMethod dateParameter; private ControllerMethod dogParameterWithoutRoot; private ControllerMethod dogParameterNameEqualsJsonPropertyWithoutRoot; - + private @Mock Container container; - + private @Mock Instance deserializeeInstance; @Before @@ -119,7 +119,7 @@ public void setUp() throws Exception { listDog = new DefaultControllerMethod(controllerClass, DogController.class.getDeclaredMethod("list", List.class)); dogParameterWithoutRoot = new DefaultControllerMethod(controllerClass, DogController.class.getDeclaredMethod("dogParameterWithoutRoot", Dog.class)); dogParameterNameEqualsJsonPropertyWithoutRoot = new DefaultControllerMethod(controllerClass, DogController.class.getDeclaredMethod("dogParameterNameEqualsJsonPropertyWithoutRoot", Dog.class)); - + when(deserializeeInstance.get()).thenReturn(new Deserializee()); when(container.instanceFor(WithRoot.class)).thenReturn(new WithRoot()); when(container.instanceFor(WithoutRoot.class)).thenReturn(new WithoutRoot()); @@ -383,6 +383,21 @@ public void shouldDeserializeFromGenericTypeOneParam() { assertThat(dog.name, equalTo("Brutus")); } + @Test + public void shouldDeserializeFromGenericTypeWithoutRoot() { + InputStream stream = asStream("{'name':'Brutus','age':7,'birthday':'2013-07-23T17:14:14-03:00'}"); + BeanClass resourceClass = new DefaultBeanClass(DogGenericController.class); + Method method = new Mirror().on(DogGenericController.class).reflect().method("method").withAnyArgs(); + ControllerMethod resource = new DefaultControllerMethod(resourceClass, method); + + Object[] deserialized = deserializer.deserialize(stream, resource); + + Dog dog = (Dog) deserialized[0]; + + assertThat(dog.name, equalTo("Brutus")); + assertThat(dog.age, equalTo(7)); + } + @Test public void shouldDeserializeFromGenericTypeTwoParams() { InputStream stream = asStream("{'entity':{'name':'Brutus','age':7,'birthday':'2013-07-23T17:14:14-03:00'}, 'param': 'test', 'over': 'value'}");