Skip to content

Commit 2815178

Browse files
author
Christoph Büscher
committed
Clarify some ToXContent implementations behaviour
This change adds either ToXContentObject or ToXContentFragment to classes directly implementing ToXContent currently. This helps in reasoning about whether those implementations output full xcontent object or just fragments. Relates to elastic#16347
1 parent d31207a commit 2815178

File tree

25 files changed

+87
-69
lines changed

25 files changed

+87
-69
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.elasticsearch.common.settings.Settings;
3232
import org.elasticsearch.common.xcontent.DeprecationHandler;
3333
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
34-
import org.elasticsearch.common.xcontent.ToXContent;
34+
import org.elasticsearch.common.xcontent.ToXContentFragment;
3535
import org.elasticsearch.common.xcontent.XContentBuilder;
3636
import org.elasticsearch.common.xcontent.XContentFactory;
3737
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -56,7 +56,7 @@
5656
/**
5757
* A request to create an index template.
5858
*/
59-
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContent {
59+
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContentFragment {
6060

6161
private String name;
6262

@@ -191,7 +191,7 @@ public PutIndexTemplateRequest settings(Map<String, Object> source) {
191191
public Settings settings() {
192192
return this.settings;
193193
}
194-
194+
195195
/**
196196
* Adds mapping that will be added when the index gets created.
197197
*
@@ -201,7 +201,7 @@ public Settings settings() {
201201
public PutIndexTemplateRequest mapping(String source, XContentType xContentType) {
202202
internalMapping(XContentHelper.convertToMap(new BytesArray(source), true, xContentType).v2());
203203
return this;
204-
}
204+
}
205205

206206
/**
207207
* The cause for this index template creation.
@@ -221,11 +221,11 @@ public String cause() {
221221
* @param source The mapping source
222222
*/
223223
public PutIndexTemplateRequest mapping(XContentBuilder source) {
224-
internalMapping(XContentHelper.convertToMap(BytesReference.bytes(source),
224+
internalMapping(XContentHelper.convertToMap(BytesReference.bytes(source),
225225
true, source.contentType()).v2());
226-
return this;
227-
}
228-
226+
return this;
227+
}
228+
229229
/**
230230
* Adds mapping that will be added when the index gets created.
231231
*
@@ -235,16 +235,16 @@ public PutIndexTemplateRequest mapping(XContentBuilder source) {
235235
public PutIndexTemplateRequest mapping(BytesReference source, XContentType xContentType) {
236236
internalMapping(XContentHelper.convertToMap(source, true, xContentType).v2());
237237
return this;
238-
}
239-
238+
}
239+
240240
/**
241241
* Adds mapping that will be added when the index gets created.
242242
*
243243
* @param source The mapping source
244244
*/
245245
public PutIndexTemplateRequest mapping(Map<String, Object> source) {
246246
return internalMapping(source);
247-
}
247+
}
248248

249249
private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
250250
try {
@@ -257,12 +257,12 @@ private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
257257
return this;
258258
} catch (IOException e) {
259259
throw new UncheckedIOException("failed to convert source to json", e);
260-
}
260+
}
261261
} catch (IOException e) {
262262
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
263263
}
264-
}
265-
264+
}
265+
266266
public BytesReference mappings() {
267267
return this.mappings;
268268
}
@@ -349,8 +349,8 @@ public PutIndexTemplateRequest source(byte[] source, int offset, int length, XCo
349349
*/
350350
public PutIndexTemplateRequest source(BytesReference source, XContentType xContentType) {
351351
return source(XContentHelper.convertToMap(source, true, xContentType).v2());
352-
}
353-
352+
}
353+
354354

355355
public Set<Alias> aliases() {
356356
return this.aliases;
@@ -441,7 +441,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
441441
builder.copyCurrentStructure(parser);
442442
}
443443
}
444-
444+
445445
builder.startObject("aliases");
446446
for (Alias alias : aliases) {
447447
alias.toXContent(builder, params);

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FindFileStructureRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.elasticsearch.common.bytes.BytesArray;
2626
import org.elasticsearch.common.bytes.BytesReference;
2727
import org.elasticsearch.common.unit.TimeValue;
28-
import org.elasticsearch.common.xcontent.ToXContent;
28+
import org.elasticsearch.common.xcontent.ToXContentFragment;
2929
import org.elasticsearch.common.xcontent.XContentBuilder;
3030

3131
import java.io.IOException;
@@ -34,7 +34,7 @@
3434
import java.util.Objects;
3535
import java.util.Optional;
3636

37-
public class FindFileStructureRequest implements Validatable, ToXContent {
37+
public class FindFileStructureRequest implements Validatable, ToXContentFragment {
3838

3939
public static final ParseField LINES_TO_SAMPLE = new ParseField("lines_to_sample");
4040
public static final ParseField TIMEOUT = new ParseField("timeout");

client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.client;
2121

2222
import com.fasterxml.jackson.core.JsonParseException;
23+
2324
import org.apache.http.HttpEntity;
2425
import org.apache.http.HttpHost;
2526
import org.apache.http.HttpResponse;
@@ -61,6 +62,7 @@
6162
import org.elasticsearch.common.util.set.Sets;
6263
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
6364
import org.elasticsearch.common.xcontent.ToXContent;
65+
import org.elasticsearch.common.xcontent.ToXContentFragment;
6466
import org.elasticsearch.common.xcontent.XContentBuilder;
6567
import org.elasticsearch.common.xcontent.XContentParser;
6668
import org.elasticsearch.common.xcontent.cbor.CborXContent;
@@ -176,7 +178,7 @@ public void testInfo() throws IOException {
176178
MainResponse testInfo = new MainResponse("nodeName", new MainResponse.Version("number", "buildFlavor", "buildType", "buildHash",
177179
"buildDate", true, "luceneVersion", "minimumWireCompatibilityVersion", "minimumIndexCompatibilityVersion"),
178180
"clusterName", "clusterUuid", "You Know, for Search");
179-
mockResponse((builder, params) -> {
181+
mockResponse((ToXContentFragment) (builder, params) -> {
180182
// taken from the server side MainResponse
181183
builder.field("name", testInfo.getNodeName());
182184
builder.field("cluster_name", testInfo.getClusterName());
@@ -762,12 +764,12 @@ public void testApiNamingConventions() throws Exception {
762764
Collectors.mapping(Tuple::v2, Collectors.toSet())));
763765

764766
// TODO remove in 8.0 - we will undeprecate indices.get_template because the current getIndexTemplate
765-
// impl will replace the existing getTemplate method.
767+
// impl will replace the existing getTemplate method.
766768
// The above general-purpose code ignores all deprecated methods which in this case leaves `getTemplate`
767-
// looking like it doesn't have a valid implementatation when it does.
769+
// looking like it doesn't have a valid implementatation when it does.
768770
apiUnsupported.remove("indices.get_template");
769-
770-
771+
772+
771773

772774
for (Map.Entry<String, Set<Method>> entry : methods.entrySet()) {
773775
String apiName = entry.getKey();
@@ -830,7 +832,7 @@ private static void assertSyncMethod(Method method, String apiName, List<String>
830832
assertThat("the return type for method [" + method + "] is incorrect",
831833
method.getReturnType().getSimpleName(), equalTo("boolean"));
832834
} else {
833-
// It's acceptable for 404s to be represented as empty Optionals
835+
// It's acceptable for 404s to be represented as empty Optionals
834836
if (!method.getReturnType().isAssignableFrom(Optional.class)) {
835837
assertThat("the return type for method [" + method + "] is incorrect",
836838
method.getReturnType().getSimpleName(), endsWith("Response"));

modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
5656
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
5757
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
58-
import org.elasticsearch.common.xcontent.ToXContent;
5958
import org.elasticsearch.common.xcontent.ToXContentObject;
6059
import org.elasticsearch.common.xcontent.XContentBuilder;
6160
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -107,7 +106,7 @@ public Response newResponse() {
107106
return new Response();
108107
}
109108

110-
public static class Request extends SingleShardRequest<Request> implements ToXContent {
109+
public static class Request extends SingleShardRequest<Request> implements ToXContentObject {
111110

112111
private static final ParseField SCRIPT_FIELD = new ParseField("script");
113112
private static final ParseField CONTEXT_FIELD = new ParseField("context");

server/src/main/java/org/elasticsearch/action/ShardOperationFailedException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import org.elasticsearch.common.Nullable;
2323
import org.elasticsearch.common.io.stream.Streamable;
24-
import org.elasticsearch.common.xcontent.ToXContent;
24+
import org.elasticsearch.common.xcontent.ToXContentObject;
2525
import org.elasticsearch.rest.RestStatus;
2626

2727
import java.util.Objects;
@@ -30,7 +30,7 @@
3030
* An exception indicating that a failure occurred performing an operation on the shard.
3131
*
3232
*/
33-
public abstract class ShardOperationFailedException implements Streamable, ToXContent {
33+
public abstract class ShardOperationFailedException implements Streamable, ToXContentObject {
3434

3535
protected String index;
3636
protected int shardId = -1;

server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.elasticsearch.common.bytes.BytesReference;
2525
import org.elasticsearch.common.io.stream.StreamInput;
2626
import org.elasticsearch.common.io.stream.StreamOutput;
27-
import org.elasticsearch.common.xcontent.ToXContent;
27+
import org.elasticsearch.common.xcontent.ToXContentFragment;
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
2929
import org.elasticsearch.common.xcontent.XContentHelper;
3030
import org.elasticsearch.common.xcontent.XContentType;
@@ -35,7 +35,7 @@
3535

3636
import static org.elasticsearch.action.ValidateActions.addValidationError;
3737

38-
public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptRequest> implements ToXContent {
38+
public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptRequest> implements ToXContentFragment {
3939

4040
private String id;
4141
private String context;

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.elasticsearch.common.xcontent.DeprecationHandler;
3939
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
4040
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
41-
import org.elasticsearch.common.xcontent.ToXContent;
41+
import org.elasticsearch.common.xcontent.ToXContentFragment;
4242
import org.elasticsearch.common.xcontent.XContentBuilder;
4343
import org.elasticsearch.common.xcontent.XContentFactory;
4444
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -59,14 +59,14 @@
5959
import java.util.stream.Collectors;
6060

6161
import static org.elasticsearch.action.ValidateActions.addValidationError;
62-
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6362
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
6463
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
64+
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6565

6666
/**
6767
* A request to create an index template.
6868
*/
69-
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContent {
69+
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContentFragment {
7070

7171
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(PutIndexTemplateRequest.class));
7272

server/src/main/java/org/elasticsearch/action/search/SearchResponse.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.elasticsearch.common.io.stream.Writeable;
3030
import org.elasticsearch.common.unit.TimeValue;
3131
import org.elasticsearch.common.xcontent.StatusToXContentObject;
32-
import org.elasticsearch.common.xcontent.ToXContent;
32+
import org.elasticsearch.common.xcontent.ToXContentFragment;
3333
import org.elasticsearch.common.xcontent.XContentBuilder;
3434
import org.elasticsearch.common.xcontent.XContentParser;
3535
import org.elasticsearch.common.xcontent.XContentParser.Token;
@@ -401,7 +401,7 @@ public String toString() {
401401
* Holds info about the clusters that the search was executed on: how many in total, how many of them were successful
402402
* and how many of them were skipped.
403403
*/
404-
public static class Clusters implements ToXContent, Writeable {
404+
public static class Clusters implements ToXContentFragment, Writeable {
405405

406406
public static final Clusters EMPTY = new Clusters(0, 0, 0);
407407

server/src/main/java/org/elasticsearch/action/search/ShardSearchFailure.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,18 @@ public void writeTo(StreamOutput out) throws IOException {
118118

119119
@Override
120120
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
121-
builder.field(SHARD_FIELD, shardId());
122-
builder.field(INDEX_FIELD, index());
123-
if (shardTarget != null) {
124-
builder.field(NODE_FIELD, shardTarget.getNodeId());
125-
}
126-
builder.field(REASON_FIELD);
127121
builder.startObject();
128-
ElasticsearchException.generateThrowableXContent(builder, params, cause);
122+
{
123+
builder.field(SHARD_FIELD, shardId());
124+
builder.field(INDEX_FIELD, index());
125+
if (shardTarget != null) {
126+
builder.field(NODE_FIELD, shardTarget.getNodeId());
127+
}
128+
builder.field(REASON_FIELD);
129+
builder.startObject();
130+
ElasticsearchException.generateThrowableXContent(builder, params, cause);
131+
builder.endObject();
132+
}
129133
builder.endObject();
130134
return builder;
131135
}

server/src/main/java/org/elasticsearch/action/support/DefaultShardOperationFailedException.java

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public String toString() {
9191

9292
@Override
9393
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
94+
builder.startObject();
9495
builder.field("shard", shardId());
9596
builder.field("index", index());
9697
builder.field("status", status.name());
@@ -99,6 +100,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
99100
ElasticsearchException.generateThrowableXContent(builder, params, cause);
100101
builder.endObject();
101102
}
103+
builder.endObject();
102104
return builder;
103105
}
104106

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/Decision.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.elasticsearch.common.io.stream.StreamOutput;
2525
import org.elasticsearch.common.io.stream.Writeable;
2626
import org.elasticsearch.common.xcontent.ToXContent;
27+
import org.elasticsearch.common.xcontent.ToXContentFragment;
28+
import org.elasticsearch.common.xcontent.ToXContentObject;
2729
import org.elasticsearch.common.xcontent.XContentBuilder;
2830

2931
import java.io.IOException;
@@ -150,7 +152,7 @@ public boolean higherThan(Type other) {
150152
/**
151153
* Simple class representing a single decision
152154
*/
153-
public static class Single extends Decision {
155+
public static class Single extends Decision implements ToXContentObject {
154156
private Type type;
155157
private String label;
156158
private String explanation;
@@ -269,7 +271,7 @@ public void writeTo(StreamOutput out) throws IOException {
269271
/**
270272
* Simple class representing a list of decisions
271273
*/
272-
public static class Multi extends Decision {
274+
public static class Multi extends Decision implements ToXContentFragment {
273275

274276
private final List<Decision> decisions = new ArrayList<>();
275277

server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.elasticsearch.common.io.stream.StreamOutput;
3232
import org.elasticsearch.common.io.stream.Writeable;
3333
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
34-
import org.elasticsearch.common.xcontent.ToXContent;
3534
import org.elasticsearch.common.xcontent.ToXContentFragment;
35+
import org.elasticsearch.common.xcontent.ToXContentObject;
3636
import org.elasticsearch.common.xcontent.XContentBuilder;
3737
import org.elasticsearch.common.xcontent.XContentParser;
3838
import org.elasticsearch.index.analysis.NamedAnalyzer;
@@ -457,7 +457,7 @@ protected boolean accept(IntervalIterator it) {
457457
}
458458
}
459459

460-
public static class IntervalFilter implements ToXContent, Writeable {
460+
public static class IntervalFilter implements ToXContentObject, Writeable {
461461

462462
public static final String NAME = "filter";
463463

server/src/main/java/org/elasticsearch/index/seqno/RetentionLease.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.elasticsearch.common.io.stream.StreamOutput;
2525
import org.elasticsearch.common.io.stream.Writeable;
2626
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
27-
import org.elasticsearch.common.xcontent.ToXContent;
27+
import org.elasticsearch.common.xcontent.ToXContentObject;
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
2929
import org.elasticsearch.common.xcontent.XContentParser;
3030

@@ -37,7 +37,7 @@
3737
* otherwise merge away operations that have been soft deleted). Each retention lease contains a unique identifier, the retaining sequence
3838
* number, the timestamp of when the lease was created or renewed, and the source of the retention lease (e.g., "ccr").
3939
*/
40-
public final class RetentionLease implements ToXContent, Writeable {
40+
public final class RetentionLease implements ToXContentObject, Writeable {
4141

4242
private final String id;
4343

server/src/main/java/org/elasticsearch/index/seqno/RetentionLeases.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.common.io.stream.Writeable;
2626
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2727
import org.elasticsearch.common.xcontent.ToXContent;
28+
import org.elasticsearch.common.xcontent.ToXContentFragment;
2829
import org.elasticsearch.common.xcontent.XContentBuilder;
2930
import org.elasticsearch.common.xcontent.XContentParser;
3031
import org.elasticsearch.gateway.MetaDataStateFormat;
@@ -42,7 +43,7 @@
4243
* Represents a versioned collection of retention leases. We version the collection of retention leases to ensure that sync requests that
4344
* arrive out of order on the replica, using the version to ensure that older sync requests are rejected.
4445
*/
45-
public class RetentionLeases implements ToXContent, Writeable {
46+
public class RetentionLeases implements ToXContentFragment, Writeable {
4647

4748
private final long primaryTerm;
4849

0 commit comments

Comments
 (0)