Add null check in InternalSearchHit#sourceRef to prevent NPE (#21431)

Add null check in InternalSearchHit#sourceRef to prevent NPE

Closes #19279
This commit is contained in:
Nguyễn Thanh Tiến 2016-11-10 16:54:43 +07:00 committed by Luca Cavanna
parent 4b94eb5cb8
commit 27a7b30349
2 changed files with 15 additions and 0 deletions

View File

@ -200,6 +200,10 @@ public class InternalSearchHit implements SearchHit {
*/
@Override
public BytesReference sourceRef() {
if (this.source == null) {
return null;
}
try {
this.source = CompressorFactory.uncompressIfNeeded(this.source);
return this.source;

View File

@ -76,4 +76,15 @@ public class InternalSearchHitTests extends ESTestCase {
assertThat(results.getAt(1).shard(), equalTo(target));
}
public void testNullSource() throws Exception {
InternalSearchHit searchHit = new InternalSearchHit(0, "_id", new Text("_type"), null);
assertThat(searchHit.source(), nullValue());
assertThat(searchHit.sourceRef(), nullValue());
assertThat(searchHit.sourceAsMap(), nullValue());
assertThat(searchHit.sourceAsString(), nullValue());
assertThat(searchHit.getSource(), nullValue());
assertThat(searchHit.getSourceRef(), nullValue());
assertThat(searchHit.getSourceAsString(), nullValue());
}
}