diff --git a/core/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java b/core/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java index 067d4666722..a355a12d672 100644 --- a/core/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java +++ b/core/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; +import java.util.Objects; public class CompressorFactory { @@ -68,9 +69,10 @@ public class CompressorFactory { /** * Uncompress the provided data, data can be detected as compressed using {@link #isCompressed(BytesReference)}. + * @throws NullPointerException a NullPointerException will be thrown when bytes is null */ public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException { - Compressor compressor = compressor(bytes); + Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null")); BytesReference uncompressed; if (compressor != null) { uncompressed = uncompress(bytes, compressor); diff --git a/core/src/main/java/org/elasticsearch/index/get/GetResult.java b/core/src/main/java/org/elasticsearch/index/get/GetResult.java index 2618efddae6..78c3b78fe61 100644 --- a/core/src/main/java/org/elasticsearch/index/get/GetResult.java +++ b/core/src/main/java/org/elasticsearch/index/get/GetResult.java @@ -54,7 +54,7 @@ public class GetResult implements Streamable, Iterable, ToXContentObje private static final String _VERSION = "_version"; private static final String FOUND = "found"; private static final String FIELDS = "fields"; - + private String index; private String type; private String id; @@ -135,6 +135,10 @@ public class GetResult implements Streamable, Iterable, ToXContentObje * Returns bytes reference, also un compress the source if needed. */ public BytesReference sourceRef() { + if (source == null) { + return null; + } + try { this.source = CompressorFactory.uncompressIfNeeded(this.source); return this.source; diff --git a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java index 958f6ac095a..0d0008cd1ce 100644 --- a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java +++ b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java @@ -79,6 +79,17 @@ public class GetResultTests extends ESTestCase { } } + public void testGetSourceAsBytes() { + XContentType xContentType = randomFrom(XContentType.values()); + Tuple tuple = randomGetResult(xContentType); + GetResult getResult = tuple.v1(); + if (getResult.isExists()) { + assertNotNull(getResult.sourceRef()); + } else { + assertNull(getResult.sourceRef()); + } + } + public void testEqualsAndHashcode() { checkEqualsAndHashCode(randomGetResult(XContentType.JSON).v1(), GetResultTests::copyGetResult, GetResultTests::mutateGetResult); }