Skip to content

Commit

Permalink
bug fixed for WildcardType extends Generic Type. issue #998
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 16, 2017
1 parent e8b094a commit f83fee8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
Expand All @@ -27,7 +28,15 @@ public ArrayListTypeFieldDeserializer(ParserConfig mapping, Class<?> clazz, Fiel

Type fieldType = fieldInfo.fieldType;
if (fieldType instanceof ParameterizedType) {
this.itemType = ((ParameterizedType) fieldInfo.fieldType).getActualTypeArguments()[0];
Type argType = ((ParameterizedType) fieldInfo.fieldType).getActualTypeArguments()[0];
if (argType instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) argType;
Type[] upperBounds = wildcardType.getUpperBounds();
if (upperBounds.length == 1) {
argType = upperBounds[0];
}
}
this.itemType = argType;
} else {
this.itemType = Object.class;
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/alibaba/json/bvt/bug/Issue998.java
@@ -0,0 +1,28 @@
package com.alibaba.json.bvt.bug;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import junit.framework.TestCase;

import java.util.List;

/**
* Created by wenshao on 16/01/2017.
*/
public class Issue998 extends TestCase {
public void test_for_issue() throws Exception {
Model model = JSON.parseObject("{\"items\":[{\"id\":123}]}", Model.class);
assertNotNull(model);
assertNotNull(model.items);
assertEquals(1, model.items.size());
assertEquals(123, model.items.get(0).id);
}

public static class Model {
public List<? extends Item> items;
}

public static class Item {
public int id;
}
}

0 comments on commit f83fee8

Please sign in to comment.