Skip to content

Commit

Permalink
fix: add support for deserialize a class extending ArrayList
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-qlogic committed Aug 20, 2020
1 parent 556b7b9 commit 57e18a9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
Expand Up @@ -266,7 +266,18 @@ private static <T> T deserializeToParameterizedType(
Type genericType = type.getActualTypeArguments()[0];
if (o instanceof List) {
List<Object> list = (List<Object>) o;
List<Object> result = new ArrayList<>(list.size());
List<Object> result = null;
try {
result =
(rawType == List.class)
? new ArrayList<>(list.size())
: (List<Object>) rawType.getDeclaredConstructor().newInstance();
} catch (InstantiationException
| IllegalAccessException
| NoSuchMethodException
| InvocationTargetException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < list.size(); i++) {
result.add(
deserializeToType(
Expand Down
Expand Up @@ -24,6 +24,7 @@
import static com.google.cloud.firestore.LocalFirestoreHelper.DOCUMENT_NAME;
import static com.google.cloud.firestore.LocalFirestoreHelper.DOCUMENT_PATH;
import static com.google.cloud.firestore.LocalFirestoreHelper.FIELD_TRANSFORM_COMMIT_RESPONSE;
import static com.google.cloud.firestore.LocalFirestoreHelper.FOO_LIST;
import static com.google.cloud.firestore.LocalFirestoreHelper.GEO_POINT;
import static com.google.cloud.firestore.LocalFirestoreHelper.NESTED_CLASS_OBJECT;
import static com.google.cloud.firestore.LocalFirestoreHelper.SERVER_TIMESTAMP_PROTO;
Expand Down Expand Up @@ -69,10 +70,13 @@
import com.google.cloud.firestore.LocalFirestoreHelper.InvalidPOJO;
import com.google.cloud.firestore.spi.v1.FirestoreRpc;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.firestore.v1.ArrayValue;
import com.google.firestore.v1.BatchGetDocumentsRequest;
import com.google.firestore.v1.BatchGetDocumentsResponse;
import com.google.firestore.v1.CommitRequest;
import com.google.firestore.v1.CommitResponse;
import com.google.firestore.v1.MapValue;
import com.google.firestore.v1.Value;
import java.math.BigInteger;
import java.util.ArrayList;
Expand Down Expand Up @@ -1073,4 +1077,33 @@ public void deleteNestedFieldUsingFieldPath() throws Exception {
Collections.<String, Value>emptyMap(), Collections.singletonList("`a.b`.`c.d`")));
assertEquals(expectedCommit, commitCapture.getValue());
}

@Test
public void deserializeCustomList() throws ExecutionException, InterruptedException {
ImmutableMap CUSTOM_LIST_PROTO =
ImmutableMap.<String, Value>builder()
.put(
"fooList",
Value.newBuilder()
.setArrayValue(
ArrayValue.newBuilder()
.addValues(
Value.newBuilder()
.setMapValue(
MapValue.newBuilder().putAllFields(SINGLE_FIELD_PROTO))
.build()))
.build())
.build();
doAnswer(getAllResponse(CUSTOM_LIST_PROTO))
.when(firestoreMock)
.streamRequest(
getAllCapture.capture(),
streamObserverCapture.capture(),
Matchers.<ServerStreamingCallable>any());
DocumentSnapshot snapshot = documentReference.get().get();
LocalFirestoreHelper.CustomList customList =
snapshot.toObject(LocalFirestoreHelper.CustomList.class);

assertEquals(FOO_LIST, customList.fooList);
}
}
Expand Up @@ -143,6 +143,7 @@ public final class LocalFirestoreHelper {
public static final Timestamp TIMESTAMP;
public static final GeoPoint GEO_POINT;
public static final Blob BLOB;
public static final FooList<SingleField> FOO_LIST = new FooList<>();

public static final Precondition UPDATE_PRECONDITION;

Expand All @@ -165,6 +166,18 @@ public boolean equals(Object o) {
}
}

public static class FooList<E> extends ArrayList<SingleField> {
public FooList() {
super();
}
}

public static class CustomList {
public CustomList() {}

public FooList<SingleField> fooList;
}

public static class NestedClass {
public SingleField first = new SingleField();
public AllSupportedTypes second = new AllSupportedTypes();
Expand Down Expand Up @@ -773,6 +786,7 @@ public boolean equals(Object o) {
SINGLE_FIELD_MAP = map("foo", (Object) "bar");
SINGLE_FILED_MAP_WITH_DOT = map("c.d", (Object) "bar");
SINGLE_FIELD_OBJECT = new SingleField();
FOO_LIST.add(SINGLE_FIELD_OBJECT);
SINGLE_FIELD_PROTO = map("foo", Value.newBuilder().setStringValue("bar").build());
UPDATED_POJO_PROTO =
map(
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.firestore.it;

import static com.google.cloud.firestore.LocalFirestoreHelper.FOO_LIST;
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_SINGLE_FIELD_OBJECT;
import static com.google.cloud.firestore.LocalFirestoreHelper.map;
import static com.google.common.truth.Truth.assertThat;
Expand Down Expand Up @@ -1402,6 +1403,18 @@ public Void updateCallback(Transaction transaction) throws Exception {
}
}

@Test
public void deserializeCustomList() throws Exception {
LocalFirestoreHelper.CustomList customList = new LocalFirestoreHelper.CustomList();
customList.fooList = FOO_LIST;
DocumentReference documentReference = randomColl.document("doc1");
documentReference.set(customList).get();
DocumentSnapshot documentSnapshots = documentReference.get().get();
LocalFirestoreHelper.CustomList targetCustomList =
documentSnapshots.toObject(LocalFirestoreHelper.CustomList.class);
assertEquals(FOO_LIST, targetCustomList.fooList);
}

/** Wrapper around ApiStreamObserver that returns the results in a list. */
private static class StreamConsumer<T> implements ApiStreamObserver<T> {
SettableApiFuture<List<T>> done = SettableApiFuture.create();
Expand Down

0 comments on commit 57e18a9

Please sign in to comment.