Skip to content

Commit 2980a6c

Browse files
author
Christoph Büscher
committed
Clarify some ToXContent implementations behaviour (#41000)
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 #16347
1 parent 5ef247d commit 2980a6c

File tree

31 files changed

+120
-104
lines changed

31 files changed

+120
-104
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java

-2
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
133133
builder.startArray(FAILURES.getPreferredName());
134134
if (shardFailures != null) {
135135
for (ShardOperationFailedException shardFailure : shardFailures) {
136-
builder.startObject();
137136
shardFailure.toXContent(builder, params);
138-
builder.endObject();
139137
}
140138
}
141139
builder.endArray();

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
@@ -25,7 +25,7 @@
2525
import org.elasticsearch.common.bytes.BytesReference;
2626
import org.elasticsearch.common.io.stream.StreamInput;
2727
import org.elasticsearch.common.io.stream.StreamOutput;
28-
import org.elasticsearch.common.xcontent.ToXContent;
28+
import org.elasticsearch.common.xcontent.ToXContentFragment;
2929
import org.elasticsearch.common.xcontent.XContentBuilder;
3030
import org.elasticsearch.common.xcontent.XContentHelper;
3131
import org.elasticsearch.common.xcontent.XContentType;
@@ -36,7 +36,7 @@
3636

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

39-
public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptRequest> implements ToXContent {
39+
public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptRequest> implements ToXContentFragment {
4040

4141
private String id;
4242
private String context;

server/src/main/java/org/elasticsearch/action/admin/indices/shards/IndicesShardStoresResponse.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.carrotsearch.hppc.cursors.IntObjectCursor;
2323
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
24+
2425
import org.elasticsearch.ElasticsearchException;
2526
import org.elasticsearch.Version;
2627
import org.elasticsearch.action.ActionResponse;
@@ -267,8 +268,10 @@ public void writeTo(StreamOutput out) throws IOException {
267268

268269
@Override
269270
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
271+
builder.startObject();
270272
builder.field("node", nodeId());
271-
super.toXContent(builder, params);
273+
super.innerToXContent(builder, params);
274+
builder.endObject();
272275
return builder;
273276
}
274277
}
@@ -361,9 +364,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
361364
if (failures.size() > 0) {
362365
builder.startArray(Fields.FAILURES);
363366
for (Failure failure : failures) {
364-
builder.startObject();
365367
failure.toXContent(builder, params);
366-
builder.endObject();
367368
}
368369
builder.endArray();
369370
}

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

+26-23
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.elasticsearch.common.xcontent.DeprecationHandler;
4040
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
4141
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
42-
import org.elasticsearch.common.xcontent.ToXContent;
42+
import org.elasticsearch.common.xcontent.ToXContentObject;
4343
import org.elasticsearch.common.xcontent.XContentBuilder;
4444
import org.elasticsearch.common.xcontent.XContentFactory;
4545
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -60,14 +60,14 @@
6060
import java.util.stream.Collectors;
6161

6262
import static org.elasticsearch.action.ValidateActions.addValidationError;
63-
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6463
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
6564
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
65+
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
6666

6767
/**
6868
* A request to create an index template.
6969
*/
70-
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContent {
70+
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContentObject {
7171

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

@@ -519,32 +519,35 @@ public void writeTo(StreamOutput out) throws IOException {
519519

520520
@Override
521521
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
522-
builder.field("index_patterns", indexPatterns);
523-
builder.field("order", order);
524-
if (version != null) {
525-
builder.field("version", version);
526-
}
522+
builder.startObject();
523+
{
524+
builder.field("index_patterns", indexPatterns);
525+
builder.field("order", order);
526+
if (version != null) {
527+
builder.field("version", version);
528+
}
527529

528-
builder.startObject("settings");
529-
settings.toXContent(builder, params);
530-
builder.endObject();
530+
builder.startObject("settings");
531+
settings.toXContent(builder, params);
532+
builder.endObject();
531533

532-
builder.startObject("mappings");
533-
for (Map.Entry<String, String> entry : mappings.entrySet()) {
534-
builder.field(entry.getKey());
535-
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
536-
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue())) {
537-
builder.copyCurrentStructure(parser);
534+
builder.startObject("mappings");
535+
for (Map.Entry<String, String> entry : mappings.entrySet()) {
536+
builder.field(entry.getKey());
537+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
538+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue())) {
539+
builder.copyCurrentStructure(parser);
540+
}
538541
}
539-
}
540-
builder.endObject();
542+
builder.endObject();
541543

542-
builder.startObject("aliases");
543-
for (Alias alias : aliases) {
544-
alias.toXContent(builder, params);
544+
builder.startObject("aliases");
545+
for (Alias alias : aliases) {
546+
alias.toXContent(builder, params);
547+
}
548+
builder.endObject();
545549
}
546550
builder.endObject();
547-
548551
return builder;
549552
}
550553
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ protected void metadataToXContent(XContentBuilder builder, Params params) throws
140140
builder.startArray();
141141
ShardOperationFailedException[] failures = ExceptionsHelper.groupBy(shardFailures);
142142
for (ShardOperationFailedException failure : failures) {
143-
builder.startObject();
144143
failure.toXContent(builder, params);
145-
builder.endObject();
146144
}
147145
builder.endArray();
148146
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.elasticsearch.common.io.stream.Writeable;
3131
import org.elasticsearch.common.unit.TimeValue;
3232
import org.elasticsearch.common.xcontent.StatusToXContentObject;
33-
import org.elasticsearch.common.xcontent.ToXContent;
33+
import org.elasticsearch.common.xcontent.ToXContentFragment;
3434
import org.elasticsearch.common.xcontent.XContentBuilder;
3535
import org.elasticsearch.common.xcontent.XContentParser;
3636
import org.elasticsearch.common.xcontent.XContentParser.Token;
@@ -408,7 +408,7 @@ public String toString() {
408408
* Holds info about the clusters that the search was executed on: how many in total, how many of them were successful
409409
* and how many of them were skipped.
410410
*/
411-
public static class Clusters implements ToXContent, Writeable {
411+
public static class Clusters implements ToXContentFragment, Writeable {
412412

413413
public static final Clusters EMPTY = new Clusters(0, 0, 0);
414414

0 commit comments

Comments
 (0)