Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting deserialization for GenericController without root #932

Merged
merged 1 commit into from
Jan 29, 2015
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 @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public class GsonDeserializerTest {
private ControllerMethod dateParameter;
private ControllerMethod dogParameterWithoutRoot;
private ControllerMethod dogParameterNameEqualsJsonPropertyWithoutRoot;

private @Mock Container container;

private @Mock Instance<Deserializee> deserializeeInstance;

@Before
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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'}");
Expand Down