Skip to content

Commit

Permalink
[8.1] Correctly return _type field for documents in V7 compatiblity m…
Browse files Browse the repository at this point in the history
…ode (#84873) (#84937)

Backports #84873 to 8.1

* Currently, it's not returned for bulk indexing, because BulkItemResponse calls DocWriteResponse.innerToXContent instead of DocWriteResponse.toXContent.
  • Loading branch information
arteam committed Mar 14, 2022
1 parent 55e1767 commit cce5e6d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/84873.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 84873
summary: Correctly return `_type` field for documents in V7 compatiblity mode
area: Infra/REST API
type: bug
issues:
- 84173
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,6 @@ private void writeWithoutShardId(StreamOutput out) throws IOException {
@Override
public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (builder.getRestApiVersion() == RestApiVersion.V_7) {
builder.field(MapperService.TYPE_FIELD_NAME, MapperService.SINGLE_MAPPING_NAME);
}
innerToXContent(builder, params);
builder.endObject();
return builder;
Expand All @@ -308,6 +305,9 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
builder.field(_SEQ_NO, getSeqNo());
builder.field(_PRIMARY_TERM, getPrimaryTerm());
}
if (builder.getRestApiVersion() == RestApiVersion.V_7) {
builder.field(MapperService.TYPE_FIELD_NAME, MapperService.SINGLE_MAPPING_NAME);
}
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,52 @@
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse.Failure;
import org.elasticsearch.action.delete.DeleteResponseTests;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.index.IndexResponseTests;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.action.update.UpdateResponseTests;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;

import java.io.IOException;
import java.util.UUID;

import static org.elasticsearch.ElasticsearchExceptionTests.assertDeepEquals;
import static org.elasticsearch.ElasticsearchExceptionTests.randomExceptions;
import static org.hamcrest.Matchers.containsString;

public class BulkItemResponseTests extends ESTestCase {

public void testBulkItemResponseShouldContainTypeInV7CompatibilityMode() throws IOException {
BulkItemResponse bulkItemResponse = BulkItemResponse.success(
randomInt(),
DocWriteRequest.OpType.INDEX,
new IndexResponse(
new ShardId(randomAlphaOfLength(8), UUID.randomUUID().toString(), randomInt()),
randomAlphaOfLength(4),
randomNonNegativeLong(),
randomNonNegativeLong(),
randomNonNegativeLong(),
true
)
);
XContentBuilder xContentBuilder = bulkItemResponse.toXContent(
XContentBuilder.builder(JsonXContent.jsonXContent, RestApiVersion.V_7),
ToXContent.EMPTY_PARAMS
);

String json = BytesReference.bytes(xContentBuilder).utf8ToString();
assertThat(json, containsString("\"_type\":\"_doc\""));
}

public void testFailureToString() {
Failure failure = new Failure("index", "id", new RuntimeException("test"));
String toString = failure.toString();
Expand Down

0 comments on commit cce5e6d

Please sign in to comment.