From 7ada73d3c0a282be8ce2682c2e9c737690d9f891 Mon Sep 17 00:00:00 2001 From: abhijeetshuklaoist Date: Fri, 12 Mar 2021 17:33:37 +0530 Subject: [PATCH] fix: Removed Jackson dependency (#508) --- google-cloud-firestore/pom.xml | 9 -- .../cloud/firestore/LocalFirestoreHelper.java | 15 ++- .../google/cloud/firestore/MapperTest.java | 94 ++++++++++++++----- 3 files changed, 75 insertions(+), 43 deletions(-) diff --git a/google-cloud-firestore/pom.xml b/google-cloud-firestore/pom.xml index b62690ab2..4c26bd05a 100644 --- a/google-cloud-firestore/pom.xml +++ b/google-cloud-firestore/pom.xml @@ -109,10 +109,6 @@ com.google.code.gson gson - - com.fasterxml.jackson.core - jackson-core - com.google.protobuf protobuf-java-util @@ -149,11 +145,6 @@ 0.0.13 test - - com.fasterxml.jackson.core - jackson-databind - test - org.apache.commons commons-lang3 diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java index 5ebd32712..2ac9c033b 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java @@ -19,8 +19,6 @@ import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.doAnswer; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; import com.google.api.gax.retrying.RetrySettings; @@ -55,13 +53,15 @@ import com.google.firestore.v1.StructuredQuery.UnaryFilter; import com.google.firestore.v1.Value; import com.google.firestore.v1.Write; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; import com.google.protobuf.GeneratedMessageV3; import com.google.protobuf.Message; import com.google.protobuf.NullValue; import com.google.type.LatLng; -import java.io.IOException; +import java.lang.reflect.Type; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -970,12 +970,9 @@ public static Map mapAnyType(Object... entries) { } private static Map fromJsonString(String json) { - try { - ObjectMapper mapper = new ObjectMapper(); - return mapper.readValue(json, new TypeReference>() {}); - } catch (IOException e) { - throw new RuntimeException(e); - } + Type type = new TypeToken>() {}.getType(); + Gson gson = new Gson(); + return gson.fromJson(json, type); } public static Map fromSingleQuotedString(String json) { diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/MapperTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/MapperTest.java index a64eb466c..76be4126d 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/MapperTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/MapperTest.java @@ -20,9 +20,11 @@ import static com.google.cloud.firestore.LocalFirestoreHelper.mapAnyType; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.cloud.Timestamp; import com.google.cloud.firestore.annotation.DocumentId; import com.google.cloud.firestore.annotation.Exclude; import com.google.cloud.firestore.annotation.PropertyName; @@ -43,6 +45,7 @@ import java.util.Objects; import java.util.Set; import org.junit.Test; +import org.junit.function.ThrowingRunnable; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.Spy; @@ -698,11 +701,11 @@ public String getValue() { } private static class PackageConstructorBean { - private String value; + private Timestamp value; PackageConstructorBean() {} - public String getValue() { + public Timestamp getValue() { return value; } } @@ -950,11 +953,20 @@ private static T deserialize(String jsonString, Class clazz) { return deserialize(jsonString, clazz, /*docRef=*/ null); } + private static T deserialize(Map json, Class clazz) { + return deserialize(json, clazz, /*docRef=*/ null); + } + private static T deserialize(String jsonString, Class clazz, DocumentReference docRef) { Map json = fromSingleQuotedString(jsonString); return CustomClassMapper.convertToCustomClass(json, clazz, docRef); } + private static T deserialize( + Map json, Class clazz, DocumentReference docRef) { + return CustomClassMapper.convertToCustomClass(json, clazz, docRef); + } + private static Object serialize(Object object) { return CustomClassMapper.convertToPlainJavaTypes(object); } @@ -1079,23 +1091,35 @@ public void primitiveDeserializeDouble() { @Test public void primitiveDeserializeBigDecimal() { BigDecimalBean beanBigdecimal = deserialize("{'value': 123}", BigDecimalBean.class); - assertEquals(BigDecimal.valueOf(123), beanBigdecimal.value); + assertEquals(BigDecimal.valueOf(123.0), beanBigdecimal.value); beanBigdecimal = deserialize("{'value': '123'}", BigDecimalBean.class); assertEquals(BigDecimal.valueOf(123), beanBigdecimal.value); // Int - BigDecimalBean beanInt = deserialize("{'value': 1}", BigDecimalBean.class); + BigDecimalBean beanInt = + deserialize(Collections.singletonMap("value", 1), BigDecimalBean.class); assertEquals(BigDecimal.valueOf(1), beanInt.value); // Long - BigDecimalBean beanLong = deserialize("{'value': 1234567890123}", BigDecimalBean.class); + BigDecimalBean beanLong = + deserialize( + Collections.singletonMap("value", 1234567890123L), + BigDecimalBean.class); assertEquals(BigDecimal.valueOf(1234567890123L), beanLong.value); // Double - BigDecimalBean beanDouble = deserialize("{'value': 1.1}", BigDecimalBean.class); + BigDecimalBean beanDouble = + deserialize(Collections.singletonMap("value", 1.1), BigDecimalBean.class); assertEquals(BigDecimal.valueOf(1.1), beanDouble.value); + // BigDecimal + BigDecimalBean beanBigDecimal = + deserialize( + Collections.singletonMap("value", BigDecimal.valueOf(1.2)), + BigDecimalBean.class); + assertEquals(BigDecimal.valueOf(1.2), beanBigDecimal.value); + // Boolean try { deserialize("{'value': true}", BigDecimalBean.class); @@ -1117,10 +1141,13 @@ public void primitiveDeserializeFloat() { assertEquals(1.1, beanFloat.value, EPSILON); // Int - FloatBean beanInt = deserialize("{'value': 1}", FloatBean.class); + FloatBean beanInt = + deserialize(Collections.singletonMap("value", 1), FloatBean.class); assertEquals(1, beanInt.value, EPSILON); // Long - FloatBean beanLong = deserialize("{'value': 1234567890123}", FloatBean.class); + FloatBean beanLong = + deserialize( + Collections.singletonMap("value", 1234567890123L), FloatBean.class); assertEquals((float) 1234567890123L, beanLong.value, EPSILON); // Boolean @@ -1213,15 +1240,18 @@ public void primitiveDeserializeLong() { @Test public void primitiveDeserializeWrongTypeMap() { - assertExceptionContains( - "Failed to convert value of type java.util.LinkedHashMap to String " - + "(found in field 'value')", - new Runnable() { - @Override - public void run() { - deserialize("{'value': {'foo': 'bar'}}", StringBean.class); - } - }); + String expectedExceptionMessage = + ".* Failed to convert value of type .*Map to String \\(found in field 'value'\\).*"; + Throwable exception = + assertThrows( + RuntimeException.class, + new ThrowingRunnable() { + @Override + public void run() throws Throwable { + deserialize("{'value': {'foo': 'bar'}}", StringBean.class); + } + }); + assertTrue(exception.getMessage().matches(expectedExceptionMessage)); } @Test @@ -1565,14 +1595,16 @@ public void serializeDoubleBean() { public void serializeIntBean() { IntBean bean = new IntBean(); bean.value = 1; - assertJson("{'value': 1}", serialize(bean)); + assertJson("{'value': 1}", serialize(Collections.singletonMap("value", 1.0))); } @Test public void serializeLongBean() { LongBean bean = new LongBean(); bean.value = 1234567890123L; - assertJson("{'value': 1234567890123}", serialize(bean)); + assertJson( + "{'value': 1.234567890123E12}", + serialize(Collections.singletonMap("value", 1.234567890123E12))); } @Test @@ -2061,8 +2093,12 @@ public void run() { @Test public void packageConstructorCanBeDeserialized() { - PackageConstructorBean bean = deserialize("{'value': 'foo'}", PackageConstructorBean.class); - assertEquals("foo", bean.value); + Timestamp timestamp = Timestamp.now(); + PackageConstructorBean bean = + deserialize( + Collections.singletonMap("value", timestamp), + PackageConstructorBean.class); + assertEquals(timestamp, bean.value); } @Test @@ -2245,10 +2281,10 @@ public void serializingGenericBeansSupported() { recursiveBean.value.value = "foo"; assertJson("{'value': {'value': 'foo'}}", serialize(recursiveBean)); - DoubleGenericBean doubleBean = new DoubleGenericBean<>(); + DoubleGenericBean doubleBean = new DoubleGenericBean<>(); doubleBean.valueA = "foo"; - doubleBean.valueB = 1; - assertJson("{'valueA': 'foo', 'valueB': 1}", serialize(doubleBean)); + doubleBean.valueB = 1.0; + assertJson("{'valueB': 1, 'valueA': 'foo'}", serialize(doubleBean)); } @Test @@ -2486,7 +2522,7 @@ public void run() { public void settersCanOverridePrimitiveSettersSerializing() { NonConflictingSetterSubBean bean = new NonConflictingSetterSubBean(); bean.value = 1; - assertJson("{'value': 1}", serialize(bean)); + assertJson("{'value': 1}", serialize(Collections.singletonMap("value", 1.0))); } @Test @@ -2741,6 +2777,14 @@ public void documentIdsDeserialize() { assertEquals("doc123", deserialize("{}", DocumentIdOnStringField.class, ref).docId); + assertEquals( + "doc123", + deserialize( + Collections.singletonMap("property", 100), + DocumentIdOnStringField.class, + ref) + .docId); + DocumentIdOnStringFieldAsProperty target = deserialize("{'anotherProperty': 100}", DocumentIdOnStringFieldAsProperty.class, ref); assertEquals("doc123", target.docId);