Fix ShardInfo#toString

ShardInfo#toString resorts to calling ShardInfo#toXContent via
Strings#toString. However, the resulting XContent object will not start
with an object, and this is a violation of the generator state
machine. This commit fixes this issue by replacing the override of
toString to provide simple non-toXContent output of the ShardInfo
instance. Without this fix, ShardInfo#toString will instead produce
"Error building toString out of XContent:
com.fasterxml.jackson.core.JsonGenerationException: Can not write a
field name, expecting a value"

Relates #21319
This commit is contained in:
Jason Tedor 2016-11-03 20:40:39 -04:00 committed by GitHub
parent 68bed9cd3b
commit c3e176908c
2 changed files with 45 additions and 2 deletions

View File

@ -24,7 +24,6 @@ import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
@ -34,6 +33,7 @@ import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.rest.RestStatus;
import java.io.IOException;
import java.util.Arrays;
/**
* Base class for write action responses.
@ -162,7 +162,11 @@ public class ReplicationResponse extends ActionResponse {
@Override
public String toString() {
return Strings.toString(this);
return "ShardInfo{" +
"total=" + total +
", successful=" + successful +
", failures=" + Arrays.toString(failures) +
'}';
}
public static ShardInfo readShardInfo(StreamInput in) throws IOException {

View File

@ -0,0 +1,39 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.action.support.replication;
import org.elasticsearch.test.ESTestCase;
import java.util.Locale;
import static org.hamcrest.CoreMatchers.equalTo;
public class ReplicationResponseTests extends ESTestCase {
public void testShardInfoToString() {
final int total = 5;
final int successful = randomIntBetween(1, total);
final ReplicationResponse.ShardInfo shardInfo = new ReplicationResponse.ShardInfo(total, successful);
assertThat(
shardInfo.toString(),
equalTo(String.format(Locale.ROOT, "ShardInfo{total=5, successful=%d, failures=[]}", successful)));
}
}