Skip to content

Commit 7ada73d

Browse files
fix: Removed Jackson dependency (#508)
1 parent e44c092 commit 7ada73d

File tree

3 files changed

+75
-43
lines changed

3 files changed

+75
-43
lines changed

google-cloud-firestore/pom.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@
109109
<groupId>com.google.code.gson</groupId>
110110
<artifactId>gson</artifactId>
111111
</dependency>
112-
<dependency>
113-
<groupId>com.fasterxml.jackson.core</groupId>
114-
<artifactId>jackson-core</artifactId>
115-
</dependency>
116112
<dependency>
117113
<groupId>com.google.protobuf</groupId>
118114
<artifactId>protobuf-java-util</artifactId>
@@ -149,11 +145,6 @@
149145
<version>0.0.13</version>
150146
<scope>test</scope>
151147
</dependency>
152-
<dependency>
153-
<groupId>com.fasterxml.jackson.core</groupId>
154-
<artifactId>jackson-databind</artifactId>
155-
<scope>test</scope>
156-
</dependency>
157148
<dependency>
158149
<groupId>org.apache.commons</groupId>
159150
<artifactId>commons-lang3</artifactId>

google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.mockito.Mockito.doAnswer;
2121

22-
import com.fasterxml.jackson.core.type.TypeReference;
23-
import com.fasterxml.jackson.databind.ObjectMapper;
2422
import com.google.api.core.ApiFuture;
2523
import com.google.api.core.ApiFutures;
2624
import com.google.api.gax.retrying.RetrySettings;
@@ -55,13 +53,15 @@
5553
import com.google.firestore.v1.StructuredQuery.UnaryFilter;
5654
import com.google.firestore.v1.Value;
5755
import com.google.firestore.v1.Write;
56+
import com.google.gson.Gson;
57+
import com.google.gson.reflect.TypeToken;
5858
import com.google.protobuf.ByteString;
5959
import com.google.protobuf.Empty;
6060
import com.google.protobuf.GeneratedMessageV3;
6161
import com.google.protobuf.Message;
6262
import com.google.protobuf.NullValue;
6363
import com.google.type.LatLng;
64-
import java.io.IOException;
64+
import java.lang.reflect.Type;
6565
import java.math.BigInteger;
6666
import java.nio.ByteBuffer;
6767
import java.nio.charset.StandardCharsets;
@@ -970,12 +970,9 @@ public static <T> Map<String, T> mapAnyType(Object... entries) {
970970
}
971971

972972
private static Map<String, Object> fromJsonString(String json) {
973-
try {
974-
ObjectMapper mapper = new ObjectMapper();
975-
return mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
976-
} catch (IOException e) {
977-
throw new RuntimeException(e);
978-
}
973+
Type type = new TypeToken<Map<String, Object>>() {}.getType();
974+
Gson gson = new Gson();
975+
return gson.fromJson(json, type);
979976
}
980977

981978
public static Map<String, Object> fromSingleQuotedString(String json) {

google-cloud-firestore/src/test/java/com/google/cloud/firestore/MapperTest.java

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import static com.google.cloud.firestore.LocalFirestoreHelper.mapAnyType;
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.assertThrows;
2324
import static org.junit.Assert.assertTrue;
2425
import static org.junit.Assert.fail;
2526

27+
import com.google.cloud.Timestamp;
2628
import com.google.cloud.firestore.annotation.DocumentId;
2729
import com.google.cloud.firestore.annotation.Exclude;
2830
import com.google.cloud.firestore.annotation.PropertyName;
@@ -43,6 +45,7 @@
4345
import java.util.Objects;
4446
import java.util.Set;
4547
import org.junit.Test;
48+
import org.junit.function.ThrowingRunnable;
4649
import org.junit.runner.RunWith;
4750
import org.mockito.Mockito;
4851
import org.mockito.Spy;
@@ -698,11 +701,11 @@ public String getValue() {
698701
}
699702

700703
private static class PackageConstructorBean {
701-
private String value;
704+
private Timestamp value;
702705

703706
PackageConstructorBean() {}
704707

705-
public String getValue() {
708+
public Timestamp getValue() {
706709
return value;
707710
}
708711
}
@@ -950,11 +953,20 @@ private static <T> T deserialize(String jsonString, Class<T> clazz) {
950953
return deserialize(jsonString, clazz, /*docRef=*/ null);
951954
}
952955

956+
private static <T> T deserialize(Map<String, Object> json, Class<T> clazz) {
957+
return deserialize(json, clazz, /*docRef=*/ null);
958+
}
959+
953960
private static <T> T deserialize(String jsonString, Class<T> clazz, DocumentReference docRef) {
954961
Map<String, Object> json = fromSingleQuotedString(jsonString);
955962
return CustomClassMapper.convertToCustomClass(json, clazz, docRef);
956963
}
957964

965+
private static <T> T deserialize(
966+
Map<String, Object> json, Class<T> clazz, DocumentReference docRef) {
967+
return CustomClassMapper.convertToCustomClass(json, clazz, docRef);
968+
}
969+
958970
private static Object serialize(Object object) {
959971
return CustomClassMapper.convertToPlainJavaTypes(object);
960972
}
@@ -1079,23 +1091,35 @@ public void primitiveDeserializeDouble() {
10791091
@Test
10801092
public void primitiveDeserializeBigDecimal() {
10811093
BigDecimalBean beanBigdecimal = deserialize("{'value': 123}", BigDecimalBean.class);
1082-
assertEquals(BigDecimal.valueOf(123), beanBigdecimal.value);
1094+
assertEquals(BigDecimal.valueOf(123.0), beanBigdecimal.value);
10831095

10841096
beanBigdecimal = deserialize("{'value': '123'}", BigDecimalBean.class);
10851097
assertEquals(BigDecimal.valueOf(123), beanBigdecimal.value);
10861098

10871099
// Int
1088-
BigDecimalBean beanInt = deserialize("{'value': 1}", BigDecimalBean.class);
1100+
BigDecimalBean beanInt =
1101+
deserialize(Collections.<String, Object>singletonMap("value", 1), BigDecimalBean.class);
10891102
assertEquals(BigDecimal.valueOf(1), beanInt.value);
10901103

10911104
// Long
1092-
BigDecimalBean beanLong = deserialize("{'value': 1234567890123}", BigDecimalBean.class);
1105+
BigDecimalBean beanLong =
1106+
deserialize(
1107+
Collections.<String, Object>singletonMap("value", 1234567890123L),
1108+
BigDecimalBean.class);
10931109
assertEquals(BigDecimal.valueOf(1234567890123L), beanLong.value);
10941110

10951111
// Double
1096-
BigDecimalBean beanDouble = deserialize("{'value': 1.1}", BigDecimalBean.class);
1112+
BigDecimalBean beanDouble =
1113+
deserialize(Collections.<String, Object>singletonMap("value", 1.1), BigDecimalBean.class);
10971114
assertEquals(BigDecimal.valueOf(1.1), beanDouble.value);
10981115

1116+
// BigDecimal
1117+
BigDecimalBean beanBigDecimal =
1118+
deserialize(
1119+
Collections.<String, Object>singletonMap("value", BigDecimal.valueOf(1.2)),
1120+
BigDecimalBean.class);
1121+
assertEquals(BigDecimal.valueOf(1.2), beanBigDecimal.value);
1122+
10991123
// Boolean
11001124
try {
11011125
deserialize("{'value': true}", BigDecimalBean.class);
@@ -1117,10 +1141,13 @@ public void primitiveDeserializeFloat() {
11171141
assertEquals(1.1, beanFloat.value, EPSILON);
11181142

11191143
// Int
1120-
FloatBean beanInt = deserialize("{'value': 1}", FloatBean.class);
1144+
FloatBean beanInt =
1145+
deserialize(Collections.<String, Object>singletonMap("value", 1), FloatBean.class);
11211146
assertEquals(1, beanInt.value, EPSILON);
11221147
// Long
1123-
FloatBean beanLong = deserialize("{'value': 1234567890123}", FloatBean.class);
1148+
FloatBean beanLong =
1149+
deserialize(
1150+
Collections.<String, Object>singletonMap("value", 1234567890123L), FloatBean.class);
11241151
assertEquals((float) 1234567890123L, beanLong.value, EPSILON);
11251152

11261153
// Boolean
@@ -1213,15 +1240,18 @@ public void primitiveDeserializeLong() {
12131240

12141241
@Test
12151242
public void primitiveDeserializeWrongTypeMap() {
1216-
assertExceptionContains(
1217-
"Failed to convert value of type java.util.LinkedHashMap to String "
1218-
+ "(found in field 'value')",
1219-
new Runnable() {
1220-
@Override
1221-
public void run() {
1222-
deserialize("{'value': {'foo': 'bar'}}", StringBean.class);
1223-
}
1224-
});
1243+
String expectedExceptionMessage =
1244+
".* Failed to convert value of type .*Map to String \\(found in field 'value'\\).*";
1245+
Throwable exception =
1246+
assertThrows(
1247+
RuntimeException.class,
1248+
new ThrowingRunnable() {
1249+
@Override
1250+
public void run() throws Throwable {
1251+
deserialize("{'value': {'foo': 'bar'}}", StringBean.class);
1252+
}
1253+
});
1254+
assertTrue(exception.getMessage().matches(expectedExceptionMessage));
12251255
}
12261256

12271257
@Test
@@ -1565,14 +1595,16 @@ public void serializeDoubleBean() {
15651595
public void serializeIntBean() {
15661596
IntBean bean = new IntBean();
15671597
bean.value = 1;
1568-
assertJson("{'value': 1}", serialize(bean));
1598+
assertJson("{'value': 1}", serialize(Collections.singletonMap("value", 1.0)));
15691599
}
15701600

15711601
@Test
15721602
public void serializeLongBean() {
15731603
LongBean bean = new LongBean();
15741604
bean.value = 1234567890123L;
1575-
assertJson("{'value': 1234567890123}", serialize(bean));
1605+
assertJson(
1606+
"{'value': 1.234567890123E12}",
1607+
serialize(Collections.singletonMap("value", 1.234567890123E12)));
15761608
}
15771609

15781610
@Test
@@ -2061,8 +2093,12 @@ public void run() {
20612093

20622094
@Test
20632095
public void packageConstructorCanBeDeserialized() {
2064-
PackageConstructorBean bean = deserialize("{'value': 'foo'}", PackageConstructorBean.class);
2065-
assertEquals("foo", bean.value);
2096+
Timestamp timestamp = Timestamp.now();
2097+
PackageConstructorBean bean =
2098+
deserialize(
2099+
Collections.<String, Object>singletonMap("value", timestamp),
2100+
PackageConstructorBean.class);
2101+
assertEquals(timestamp, bean.value);
20662102
}
20672103

20682104
@Test
@@ -2245,10 +2281,10 @@ public void serializingGenericBeansSupported() {
22452281
recursiveBean.value.value = "foo";
22462282
assertJson("{'value': {'value': 'foo'}}", serialize(recursiveBean));
22472283

2248-
DoubleGenericBean<String, Integer> doubleBean = new DoubleGenericBean<>();
2284+
DoubleGenericBean<String, Double> doubleBean = new DoubleGenericBean<>();
22492285
doubleBean.valueA = "foo";
2250-
doubleBean.valueB = 1;
2251-
assertJson("{'valueA': 'foo', 'valueB': 1}", serialize(doubleBean));
2286+
doubleBean.valueB = 1.0;
2287+
assertJson("{'valueB': 1, 'valueA': 'foo'}", serialize(doubleBean));
22522288
}
22532289

22542290
@Test
@@ -2486,7 +2522,7 @@ public void run() {
24862522
public void settersCanOverridePrimitiveSettersSerializing() {
24872523
NonConflictingSetterSubBean bean = new NonConflictingSetterSubBean();
24882524
bean.value = 1;
2489-
assertJson("{'value': 1}", serialize(bean));
2525+
assertJson("{'value': 1}", serialize(Collections.singletonMap("value", 1.0)));
24902526
}
24912527

24922528
@Test
@@ -2741,6 +2777,14 @@ public void documentIdsDeserialize() {
27412777

27422778
assertEquals("doc123", deserialize("{}", DocumentIdOnStringField.class, ref).docId);
27432779

2780+
assertEquals(
2781+
"doc123",
2782+
deserialize(
2783+
Collections.<String, Object>singletonMap("property", 100),
2784+
DocumentIdOnStringField.class,
2785+
ref)
2786+
.docId);
2787+
27442788
DocumentIdOnStringFieldAsProperty target =
27452789
deserialize("{'anotherProperty': 100}", DocumentIdOnStringFieldAsProperty.class, ref);
27462790
assertEquals("doc123", target.docId);

0 commit comments

Comments
 (0)