Skip to content

Commit

Permalink
Ensure meta and document field maps are never null in GetResult (elas…
Browse files Browse the repository at this point in the history
…tic#50112)

This commit ensures deseriable a GetResult from StreamInput does not
leave metaFields and documentFields null. This could cause an NPE in
situations where upsert response for a document that did not exist is
passed back to a node that forwarded the upsert request.

closes elastic#48215
  • Loading branch information
rjernst committed Dec 12, 2019
1 parent 47e5e34 commit cbff636
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
17 changes: 7 additions & 10 deletions server/src/main/java/org/elasticsearch/index/get/GetResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public class GetResult implements Writeable, Iterable<DocumentField>, ToXContent
private long seqNo;
private long primaryTerm;
private boolean exists;
private Map<String, DocumentField> documentFields;
private Map<String, DocumentField> metaFields;
private final Map<String, DocumentField> documentFields;
private final Map<String, DocumentField> metaFields;
private Map<String, Object> sourceAsMap;
private BytesReference source;
private byte[] sourceAsBytes;
Expand Down Expand Up @@ -99,6 +99,9 @@ public GetResult(StreamInput in) throws IOException {
metaFields = new HashMap<>();
splitFieldsByMetadata(fields, documentFields, metaFields);
}
} else {
metaFields = Collections.emptyMap();
documentFields = Collections.emptyMap();
}
}

Expand All @@ -116,14 +119,8 @@ public GetResult(String index, String type, String id, long seqNo, long primaryT
this.version = version;
this.exists = exists;
this.source = source;
this.documentFields = documentFields;
if (this.documentFields == null) {
this.documentFields = emptyMap();
}
this.metaFields = metaFields;
if (this.metaFields == null) {
this.metaFields = emptyMap();
}
this.documentFields = documentFields == null ? emptyMap() : documentFields;
this.metaFields = metaFields == null ? emptyMap() : metaFields;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -138,6 +139,18 @@ public void testToXContentEmbeddedNotFound() throws IOException {
assertEquals("{\"found\":false}", originalBytes.utf8ToString());
}

public void testSerializationNotFound() throws IOException {
// serializes and deserializes with streamable, then prints back to xcontent
GetResult getResult = new GetResult("index", "type", "id", UNASSIGNED_SEQ_NO, 0, 1, false, null, null, null);

BytesStreamOutput out = new BytesStreamOutput();
getResult.writeTo(out);
getResult = new GetResult(out.bytes().streamInput());

BytesReference originalBytes = toXContentEmbedded(getResult, XContentType.JSON, false);
assertEquals("{\"found\":false}", originalBytes.utf8ToString());
}

public void testGetSourceAsBytes() {
XContentType xContentType = randomFrom(XContentType.values());
Tuple<GetResult, GetResult> tuple = randomGetResult(xContentType);
Expand Down

0 comments on commit cbff636

Please sign in to comment.