Skip to content

Commit

Permalink
Merge pull request #995 from clairton/master
Browse files Browse the repository at this point in the history
Fix Get Adapters in Proxy Class
  • Loading branch information
Turini committed Jul 23, 2015
2 parents e88f19b + 5409dba commit ba2c627
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package br.com.caelum.vraptor.serialization.gson;

import static br.com.caelum.vraptor.proxy.CDIProxies.extractRawTypeIfPossible;
import static java.util.Collections.singletonList;

import java.lang.reflect.ParameterizedType;
Expand All @@ -26,15 +27,15 @@
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import br.com.caelum.vraptor.core.ReflectionProvider;
import br.com.caelum.vraptor.serialization.Serializee;

import com.google.gson.ExclusionStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.core.ReflectionProvider;
import br.com.caelum.vraptor.serialization.Serializee;

/**
* Builder Wrapper for JSON using GSON.
*
Expand Down Expand Up @@ -90,9 +91,10 @@ private void registerAdapter(Class<?> adapterType, Object adapter) {
}

private Class<?> getAdapterType(Object adapter) {
Type[] genericInterfaces = adapter.getClass().getGenericInterfaces();
ParameterizedType type = (ParameterizedType) genericInterfaces[0];
Type actualType = type.getActualTypeArguments()[0];
final Class<?> klazz = extractRawTypeIfPossible(adapter.getClass());
final Type[] genericInterfaces = klazz.getGenericInterfaces();
final ParameterizedType type = (ParameterizedType) genericInterfaces[0];
final Type actualType = type.getActualTypeArguments()[0];

if (actualType instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) actualType).getRawType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package br.com.caelum.vraptor.serialization.gson;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Type;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.WeldJunitRunner;

@RunWith(WeldJunitRunner.class)
public class GsonBuilderWrapperTest {
private @Inject GsonBuilderWrapper builder;
private Gson gson;

@Before
public void init(){
gson = builder.create();
}

@Test
public void test() {
String json = gson.toJson(new Bean());
assertEquals("{\"test123\":{}}", json);
}
}

class Bean{

}


@RegisterStrategy(RegisterType.SINGLE)
@RequestScoped
class BeanSerializer implements JsonSerializer<Bean> {
private static final JsonObject element = new JsonObject();
static{
element.add("test123", new JsonObject());
}

@Override
public JsonElement serialize(Bean src, Type typeOfSrc, JsonSerializationContext context) {
return element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@
import javax.enterprise.inject.Instance;
import javax.servlet.http.HttpServletRequest;

import net.vidageek.mirror.dsl.Mirror;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.Consumes;
import br.com.caelum.vraptor.controller.BeanClass;
import br.com.caelum.vraptor.controller.ControllerMethod;
Expand All @@ -62,12 +66,7 @@
import br.com.caelum.vraptor.serialization.Serializee;
import br.com.caelum.vraptor.util.test.MockInstanceImpl;
import br.com.caelum.vraptor.view.GenericController;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;
import net.vidageek.mirror.dsl.Mirror;

public class GsonDeserializerTest {

Expand Down

0 comments on commit ba2c627

Please sign in to comment.