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

Fix NodeMapper handling of lists with generic types #1635

Merged
merged 1 commit into from
Feb 28, 2023
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 @@ -186,6 +186,13 @@ public NodeMapper.ObjectCreator getCreator(NodeType nodeType, Type target, NodeM

// Extract out the expected generic type of the collection.
Type memberType = Object.class;

// If given a Class, then attempt to find the generic superclass (e.g., extending ArrayList with a
// concrete generic type).
if (targetType instanceof Class) {
targetType = ((Class<?>) target).getGenericSuperclass();
}

if (targetType instanceof ParameterizedType) {
Type[] genericTypes = ((ParameterizedType) targetType).getActualTypeArguments();
if (genericTypes.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.hasValue;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
Expand Down Expand Up @@ -940,6 +942,59 @@ public static final class NonZeroArgConstructorCollection extends ArrayList<Stri
public NonZeroArgConstructorCollection(String notGonnaWork) {}
}

// We previously reported this kind of deserialization as working, but it actually was storing objects of the
// wrong type in collections, causing ClassCastExceptions at runtime.
@Test
public void deserializesIntoTypedList() {
Node value = Node.parse("[{\"a\": true}, {\"a\": false}]");
NodeMapper mapper = new NodeMapper();

FooList result = mapper.deserialize(value, FooList.class);

assertThat(result, hasSize(2));
assertThat(result.get(0), instanceOf(FooList.Foo.class));
assertThat(result.get(0).a(), is(true));
assertThat(result.get(1), instanceOf(FooList.Foo.class));
assertThat(result.get(1).a(), is(false));
}

public static final class FooList extends ArrayList<FooList.Foo> {
public static final class Foo {
boolean a;

public boolean a() {
return a;
}

public void a(boolean a) {
this.a = a;
}
}
}

// Throw some nested generics at the NodeMapper to ensure it works with them.
@Test
public void deserializesIntoTypedListWithNestedGenerics() {
Node value = Node.parse("[ [[{\"a\": true}]], [[{\"a\": false}]] ]");
NodeMapper mapper = new NodeMapper();
NestedFooList result = mapper.deserialize(value, NestedFooList.class);

assertThat(result, hasSize(2));
assertThat(result.get(0), instanceOf(ArrayList.class));
assertThat(result.get(0), hasSize(1));
assertThat(result.get(0).get(0), hasSize(1));
assertThat(result.get(0).get(0), instanceOf(FooList.class));
assertThat(result.get(0).get(0).get(0), instanceOf(FooList.Foo.class));

assertThat(result.get(1), instanceOf(ArrayList.class));
assertThat(result.get(1), hasSize(1));
assertThat(result.get(1).get(0), hasSize(1));
assertThat(result.get(1).get(0), instanceOf(FooList.class));
assertThat(result.get(1).get(0).get(0), instanceOf(FooList.Foo.class));
}

public static final class NestedFooList extends ArrayList<ArrayList<FooList>> {}

@Test
public void deserializedIntoMap() {
Node heterogeneous = Node.parse("{\"foo\":\"foo\",\"baz\":10}");
Expand Down