-
Notifications
You must be signed in to change notification settings - Fork 205
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
Implementation in gson deserialization to able deserialize generics parameters #553
Conversation
It's a little too specific... We don't need to create a new annotation for this... It is possible to discover the generic type if you have a subclass: public class DogController extends GenericController<Dog> {...}
Class<?> dogClass = DogController.class.getGenericSuperclass().getGenericTypeArguments[0]; |
Ok, I change to keep simple after your recommendation. Now it is able to deserialize generics, if method type is equal to generics types. |
@@ -91,6 +91,19 @@ else if(node != null){ | |||
return params; | |||
} | |||
|
|||
protected Class<?>[] getTypes(ResourceMethod method) { | |||
Type superclass = method.getResource().getType().getGenericSuperclass(); | |||
if(!superclass.equals(Object.class)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the right test...
you should do something like if (superclass instanceof ParameterizedType)
Done |
for (int i = 0; i < genericsTypes.length; i++) { | ||
classes[i] = (Class<?>) genericsTypes[i]; | ||
} | ||
return classes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to point it out: It won't cover the case when the generic controller method receives more than one argument:
public class GenericController<T> {
public void save(T obj, String something) {...}
}
the deserializer will deserialize only the first ParameterizedType, so, will be possible add arguments after the generic type if the GenericController has more than one. |
Not really...
|
in the last commit is require that it everytime make overlay of the first parameter, forming a convention. I think that we can to create a boolean property in @consumes annotation, with default false, to enable this feature, just when to need. the name property could be same enableGenericsOverlay. |
As far as I know, if you don't have a root, it tries do deserialize to the first argument. But if you have a root, with the names of the parameters, you can deserialize as many properties as you want. I don't like changing the The point is, even supporting multiple parameters and the Generic parameter, it's possible to know which parameter is the generic one. If I'm not wrong: Type[] parameterTypes = method.getGenericParameterTypes();
for (Type type : parameterTypes) {
if (type instanceof TypeVariable)
// use the controller generic type
else
// use type
} |
We need compare parameter with TypeArgumet
to me is working fine |
Write a test for it =) If the tests are green It works =) |
} | ||
} | ||
} | ||
return parameterTypes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd extract the generic extraction to a method:
Class<?>[] parameterTypes = method.getMethod().getParameterTypes();
Type genericType = getGenericType(method.getResource().getType);
if (genericType != null) {
for ...
}
return parameterTypes;
the tests are |
@@ -53,8 +53,8 @@ public GsonDeserialization(ParameterNameProvider paramNameProvider, Collection<J | |||
} | |||
|
|||
public Object[] deserialize(InputStream inputStream, ResourceMethod method) { | |||
Method jMethod = method.getMethod(); | |||
Class<?>[] types = jMethod.getParameterTypes(); | |||
Method jMethod = getJMethod(method); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method.getMethod()
is already clear. You don't need to extract this method. Please get back to method.getMethod()
I hope you are not getting upset, coding with reflection is always messy :P |
relax man, I just did not understand |
verify please |
Merged, thanks! =) |
thank you too |
In fact, the line |
Take a look: 9c5a0a8 |
The GsonDeserialization not is able to deserialize generics parameters like this:
class GenericCtrl {
void method(T type)
}
class ExtendedCtrl extends GenericCtrl{
}
edit: removed the old description