Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure meta and document field maps are never null in GetResult #50112

Merged
merged 2 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -65,8 +65,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 @@ -95,6 +95,9 @@ public GetResult(StreamInput in) throws IOException {
metaFields = new HashMap<>();
splitFieldsByMetadata(fields, documentFields, metaFields);
}
} else {
metaFields = Collections.emptyMap();
documentFields = Collections.emptyMap();
}
}

Expand All @@ -111,14 +114,8 @@ public GetResult(String index, String id, long seqNo, long primaryTerm, long ver
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", "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