Repository: searchbox-io/Jest
Pull request #643 (2019-04-11 10:09:15)

Avoid unnecessary String round-trip on Hit binding
Some code in Jest serializes GSON JsonElement objects to String and back in order to use `GSON.fromJson`.
This code was written on or before 2013, see 
https://github.com/searchbox-io/Jest/blame/8ae435f2670af98cb0abc4867c4f9c5b524f374e/jest-common/src/main/java/io/searchbox/client/JestResult.java#L270

Since GSON 1.3 (around 2017), there is now an overload that allows you to use the GSON reflection binding from a JsonElement source input, avoiding this extra work of going to String and back.

This PR changes this use the new, more efficient, method. I checked the rest of the codebase and could not find any other instances of this issue.

I am reasonably sure that this will be identical in behaviour but should be quite a bit faster.

All unit tests pass.



coveralls: 
[![Coverage Status](https://coveralls.io/builds/22743045/badge)](https://coveralls.io/builds/22743045)

Coverage decreased (-0.01%) to 58.03% when pulling **dfec289e9a68922fb3509d12e45e905c8796cec8 on RichardBradley:string-round-trip-fix** into **42f0a97acc45582baf5e8ecd7ccbbc5ac0c111fc on searchbox-io:master**.


Patch:
diff --git a/README.md b/README.md
index 1ca59b03..332e63ee 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ Jest is a Java HTTP Rest client for [ElasticSearch][es].
 
 ElasticSearch is an Open Source (Apache 2), Distributed, RESTful, Search Engine built on top of Apache Lucene.
 
-ElasticSearch already has a Java API which is also used by ElasticSearch internally, [but Jest fills a gap, it is the missing client for ElasticSearch Http Rest interface](#comparison-to-native-api).
+ElasticSearch already has a Java API which is also used by ElasticSearch internally, [but Jest fills a gap, it is the missing client for ElasticSearch Http Rest interface](jest/README.md#comparison-to-native-api).
 
 >Read great [introduction][ibm] to ElasticSearch and Jest from IBM Developer works.
 
diff --git a/jest-common/src/main/java/io/searchbox/client/JestResult.java b/jest-common/src/main/java/io/searchbox/client/JestResult.java
index 0f7307f6..00e28db8 100644
--- a/jest-common/src/main/java/io/searchbox/client/JestResult.java
+++ b/jest-common/src/main/java/io/searchbox/client/JestResult.java
@@ -267,8 +267,7 @@ public class JestResult {
         T obj = null;
         try {
 
-            String json = source.toString();
-            obj = gson.fromJson(json, type);
+            obj = gson.fromJson(source, type);
 
             // Check if JestId is visible
             Class clazz = type;
diff --git a/jest-common/src/main/java/io/searchbox/core/Bulk.java b/jest-common/src/main/java/io/searchbox/core/Bulk.java
index ee6b2498..cb28f704 100644
--- a/jest-common/src/main/java/io/searchbox/core/Bulk.java
+++ b/jest-common/src/main/java/io/searchbox/core/Bulk.java
@@ -155,6 +155,16 @@ public class Bulk extends AbstractAction<BulkResult> {
         return result;
     }
 
+    @Override
+    public boolean equals(Object obj) {
+        return super.equals(obj) && Objects.equals(bulkableActions, ((Bulk) obj).bulkableActions);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(super.hashCode(), this.bulkableActions);
+    }
+
     public static class Builder extends GenericResultAbstractAction.Builder<Bulk, Builder> {
         private List<BulkableAction> actions = new LinkedList<BulkableAction>();
         private String defaultIndex;


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/core/BulkTest.java b/jest-common/src/test/java/io/searchbox/core/BulkTest.java
index c4a8f98a..e64ce365 100644
--- a/jest-common/src/test/java/io/searchbox/core/BulkTest.java
+++ b/jest-common/src/test/java/io/searchbox/core/BulkTest.java
@@ -1,7 +1,9 @@
 package io.searchbox.core;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -109,6 +111,42 @@ public class BulkTest {
         assertEquals("twitter/tweet/_bulk", bulkWitIndexAndType.getURI(ElasticsearchVersion.UNKNOWN));
     }
 
+    @Test
+    public void testEqualsWithUnequalActions() {
+        final String index = "twitter";
+        final String type = "tweet";
+
+        final Object leftPayload = Collections.singletonMap("field", "value");
+        final Update leftAction = new Update.Builder(leftPayload).build();
+        final Bulk left = new Bulk.Builder()
+                .addAction(leftAction).defaultIndex(index).defaultType(type)
+                .build();
+
+        final Bulk right = new Bulk.Builder()
+                .defaultIndex(index).defaultType(type)
+                .build();
+
+        assertNotEquals(left, right);
+    }
+
+    @Test
+    public void testHashCodeWithUnequalActions() {
+        final String index = "twitter";
+        final String type = "tweet";
+
+        final Object leftPayload = Collections.singletonMap("field", "value");
+        final Update leftAction = new Update.Builder(leftPayload).build();
+        final Bulk left = new Bulk.Builder()
+                .addAction(leftAction).defaultIndex(index).defaultType(type)
+                .build();
+
+        final Bulk right = new Bulk.Builder()
+                .defaultIndex(index).defaultType(type)
+                .build();
+
+        assertNotEquals(left.hashCode(), right.hashCode());
+    }
+
     private void executeAsserts(Bulk bulk) {
         assertEquals("POST", bulk.getRestMethodName());
         assertEquals("/_bulk", bulk.getURI(ElasticsearchVersion.UNKNOWN));