Skip to content

Commit

Permalink
fix issue google#1107: resolve element type in wildcard collection ty…
Browse files Browse the repository at this point in the history
…pes (google#1146)

* fix issue google#1107: resolve element type in wildcard collection types

* fix Codacy warnings

* fix Codacy warnings
  • Loading branch information
amogilev authored and inder123 committed Sep 22, 2017
1 parent 08bbb22 commit b1fb9ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gson/src/main/java/com/google/gson/internal/$Gson$Types.java
Expand Up @@ -275,6 +275,10 @@ static Type getGenericSupertype(Type context, Class<?> rawType, Class<?> toResol
* @param supertype a superclass of, or interface implemented by, this.
*/
static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) {
if (context instanceof WildcardType) {
// wildcards are useless for resolving supertypes. As the upper bound has the same raw type, use it instead
context = ((WildcardType)context).getUpperBounds()[0];
}
checkArgument(supertype.isAssignableFrom(contextRawType));
return resolve(context, contextRawType,
$Gson$Types.getGenericSupertype(context, contextRawType, supertype));
Expand Down
19 changes: 19 additions & 0 deletions gson/src/test/java/com/google/gson/functional/CollectionTest.java
Expand Up @@ -393,4 +393,23 @@ public void testSetDeserialization() {
assertTrue(entry.value == 1 || entry.value == 2);
}
}

private class BigClass { private Map<String, ? extends List<SmallClass>> inBig; }

private class SmallClass { private String inSmall; }

public void testIssue1107() {
String json = "{\n" +
" \"inBig\": {\n" +
" \"key\": [\n" +
" { \"inSmall\": \"hello\" }\n" +
" ]\n" +
" }\n" +
"}";
BigClass bigClass = new Gson().fromJson(json, BigClass.class);
SmallClass small = bigClass.inBig.get("key").get(0);
assertNotNull(small);
assertEquals("hello", small.inSmall);
}

}

0 comments on commit b1fb9ca

Please sign in to comment.