fix IndexResponse#toString to print out shards info (#20562)

IndexResponse#toString method outputs an error caused by the shards object needing to be wrapped into another object. It is fixed by calling a different variant of Strings.toString(XContent) which accepts a second boolean argument that makes sure that a new object is created before outputting ShardInfo. I didn't change ShardInfo#toString directly as whether it needs a new object or not very much depends on where it is printed out. IndexResponse seemed a specific case as the rest of the info were not json, hence the shards object was the first one, but it is usually not the case.
This commit is contained in:
Luca Cavanna 2016-09-20 08:57:49 +02:00 committed by GitHub
parent 95bca7c390
commit b7314c8721
2 changed files with 35 additions and 1 deletions

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.index;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.rest.RestStatus;
@ -56,7 +57,7 @@ public class IndexResponse extends DocWriteResponse {
builder.append(",id=").append(getId());
builder.append(",version=").append(getVersion());
builder.append(",result=").append(getResult().getLowercase());
builder.append(",shards=").append(getShardInfo());
builder.append(",shards=").append(Strings.toString(getShardInfo(), true));
return builder.append("]").toString();
}

View File

@ -20,8 +20,11 @@ package org.elasticsearch.action.index;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESTestCase;
import java.util.Arrays;
@ -150,4 +153,34 @@ public class IndexRequestTests extends ESTestCase {
request.process(null, true, "index");
assertEquals(IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, request.getAutoGeneratedTimestamp());
}
public void testIndexResponse() {
ShardId shardId = new ShardId(randomAsciiOfLengthBetween(3, 10), randomAsciiOfLengthBetween(3, 10), randomIntBetween(0, 1000));
String type = randomAsciiOfLengthBetween(3, 10);
String id = randomAsciiOfLengthBetween(3, 10);
long version = randomLong();
boolean created = randomBoolean();
IndexResponse indexResponse = new IndexResponse(shardId, type, id, version, created);
int total = randomIntBetween(1, 10);
int successful = randomIntBetween(1, 10);
ReplicationResponse.ShardInfo shardInfo = new ReplicationResponse.ShardInfo(total, successful);
indexResponse.setShardInfo(shardInfo);
boolean forcedRefresh = false;
if (randomBoolean()) {
forcedRefresh = randomBoolean();
indexResponse.setForcedRefresh(forcedRefresh);
}
assertEquals(type, indexResponse.getType());
assertEquals(id, indexResponse.getId());
assertEquals(version, indexResponse.getVersion());
assertEquals(shardId, indexResponse.getShardId());
assertEquals(created ? RestStatus.CREATED : RestStatus.OK, indexResponse.status());
assertEquals(total, indexResponse.getShardInfo().getTotal());
assertEquals(successful, indexResponse.getShardInfo().getSuccessful());
assertEquals(forcedRefresh, indexResponse.forcedRefresh());
assertEquals("IndexResponse[index=" + shardId.getIndexName() + ",type=" + type + ",id="+ id +
",version=" + version + ",result=" + (created ? "created" : "updated") +
",shards={\"_shards\":{\"total\":" + total + ",\"successful\":" + successful + ",\"failed\":0}}]",
indexResponse.toString());
}
}