Skip to content

Commit 6de5818

Browse files
committed
Decouple XContentBuilder from BytesReference (#28972)
* Decouple XContentBuilder from BytesReference This commit removes all mentions of `BytesReference` from `XContentBuilder`. This is needed so that we can completely decouple the XContent code and move it into its own dependency. While this change appears large, it is due to two main changes, moving `.bytes()` and `.string()` out of XContentBuilder itself into static methods `BytesReference.bytes` and `Strings.toString` respectively. The rest of the change is code reacting to these changes (the majority of it in tests). Relates to #28504
1 parent f501139 commit 6de5818

File tree

306 files changed

+3577
-3375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

306 files changed

+3577
-3375
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
330330
}
331331
metadata.endObject();
332332

333-
BytesRef metadataSource = metadata.bytes().toBytesRef();
333+
BytesRef metadataSource = BytesReference.bytes(metadata).toBytesRef();
334334
content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length);
335335
content.write(separator);
336336
}
@@ -345,7 +345,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
345345
LoggingDeprecationHandler.INSTANCE, indexSource, indexXContentType)) {
346346
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
347347
builder.copyCurrentStructure(parser);
348-
source = builder.bytes().toBytesRef();
348+
source = BytesReference.bytes(builder).toBytesRef();
349349
}
350350
}
351351
} else if (opType == DocWriteRequest.OpType.UPDATE) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ public void testBulk() throws IOException {
617617
bulkRequest.add(deleteRequest);
618618

619619
} else {
620-
BytesReference source = XContentBuilder.builder(xContentType.xContent()).startObject().field("id", i).endObject().bytes();
620+
BytesReference source = BytesReference.bytes(XContentBuilder.builder(xContentType.xContent())
621+
.startObject().field("id", i).endObject());
621622
if (opType == DocWriteRequest.OpType.INDEX) {
622623
IndexRequest indexRequest = new IndexRequest("index", "test", id).source(source, xContentType);
623624
if (erroneous) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.elasticsearch.action.search.ShardSearchFailure;
5656
import org.elasticsearch.cluster.ClusterName;
5757
import org.elasticsearch.common.CheckedFunction;
58+
import org.elasticsearch.common.bytes.BytesReference;
5859
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
5960
import org.elasticsearch.common.xcontent.ToXContent;
6061
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -272,7 +273,7 @@ private static HttpEntity createBinaryEntity(XContentBuilder xContentBuilder, Co
272273
builder.startObject();
273274
builder.field("field", "value");
274275
builder.endObject();
275-
return new ByteArrayEntity(builder.bytes().toBytesRef().bytes, contentType);
276+
return new ByteArrayEntity(BytesReference.bytes(builder).toBytesRef().bytes, contentType);
276277
}
277278
}
278279

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.action.search.SearchRequest;
3535
import org.elasticsearch.action.search.SearchResponse;
3636
import org.elasticsearch.action.search.SearchScrollRequest;
37+
import org.elasticsearch.common.Strings;
3738
import org.elasticsearch.common.unit.TimeValue;
3839
import org.elasticsearch.common.xcontent.XContentBuilder;
3940
import org.elasticsearch.index.query.MatchQueryBuilder;
@@ -478,7 +479,7 @@ public void testSearchScroll() throws Exception {
478479

479480
for (int i = 0; i < 100; i++) {
480481
XContentBuilder builder = jsonBuilder().startObject().field("field", i).endObject();
481-
HttpEntity entity = new NStringEntity(builder.string(), ContentType.APPLICATION_JSON);
482+
HttpEntity entity = new NStringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
482483
client().performRequest(HttpPut.METHOD_NAME, "test/type1/" + Integer.toString(i), Collections.emptyMap(), entity);
483484
}
484485
client().performRequest(HttpPost.METHOD_NAME, "/test/_refresh");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,13 @@ public void testUpdate() throws Exception {
266266
assertSame(indexResponse.status(), RestStatus.CREATED);
267267

268268
XContentType xContentType = XContentType.JSON;
269-
String script = XContentBuilder.builder(xContentType.xContent())
269+
String script = Strings.toString(XContentBuilder.builder(xContentType.xContent())
270270
.startObject()
271271
.startObject("script")
272272
.field("lang", "painless")
273273
.field("code", "ctx._source.field += params.count")
274274
.endObject()
275-
.endObject().string();
275+
.endObject());
276276
HttpEntity body = new NStringEntity(script, ContentType.create(xContentType.mediaType()));
277277
Response response = client().performRequest(HttpPost.METHOD_NAME, "/_scripts/increment-field", emptyMap(), body);
278278
assertEquals(response.getStatusLine().getStatusCode(), RestStatus.OK.getStatus());

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.http.entity.ContentType;
3434
import org.apache.http.nio.entity.NStringEntity;
3535
import org.elasticsearch.cluster.health.ClusterHealthStatus;
36+
import org.elasticsearch.common.Strings;
3637
import org.elasticsearch.common.settings.Settings;
3738
import org.elasticsearch.common.xcontent.XContentFactory;
3839
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -75,7 +76,7 @@ public void testCreateIndex() throws IOException {
7576
.put(SETTING_NUMBER_OF_REPLICAS, 0)
7677
.build();
7778

78-
String payload = XContentFactory.jsonBuilder() // <2>
79+
String payload = Strings.toString(XContentFactory.jsonBuilder() // <2>
7980
.startObject()
8081
.startObject("settings") // <3>
8182
.value(indexSettings)
@@ -89,7 +90,7 @@ public void testCreateIndex() throws IOException {
8990
.endObject()
9091
.endObject()
9192
.endObject()
92-
.endObject().string();
93+
.endObject());
9394

9495
HttpEntity entity = new NStringEntity(payload, ContentType.APPLICATION_JSON); // <5>
9596

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorGetActionTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.ingest.common;
2121

22+
import org.elasticsearch.common.bytes.BytesReference;
2223
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2324
import org.elasticsearch.common.io.stream.StreamInput;
2425
import org.elasticsearch.common.xcontent.ToXContent;
@@ -63,7 +64,7 @@ public void testResponseToXContent() throws Exception {
6364
GrokProcessorGetAction.Response response = new GrokProcessorGetAction.Response(TEST_PATTERNS);
6465
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
6566
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
66-
Map<String, Object> converted = XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2();
67+
Map<String, Object> converted = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
6768
Map<String, String> patterns = (Map<String, String>) converted.get("patterns");
6869
assertThat(patterns.size(), equalTo(1));
6970
assertThat(patterns.get("PATTERN"), equalTo("foo"));

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.ingest.common;
2121

22+
import org.elasticsearch.common.bytes.BytesReference;
2223
import org.elasticsearch.common.xcontent.XContentBuilder;
2324
import org.elasticsearch.common.xcontent.XContentHelper;
2425
import org.elasticsearch.common.xcontent.XContentType;
@@ -48,7 +49,7 @@ public void testExecute() throws Exception {
4849

4950
Map<String, Object> randomJsonMap = RandomDocumentPicks.randomSource(random());
5051
XContentBuilder builder = JsonXContent.contentBuilder().map(randomJsonMap);
51-
String randomJson = XContentHelper.convertToJson(builder.bytes(), false, XContentType.JSON);
52+
String randomJson = XContentHelper.convertToJson(BytesReference.bytes(builder), false, XContentType.JSON);
5253
document.put(randomField, randomJson);
5354

5455
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.github.mustachejava.codes.DefaultMustache;
3131
import com.github.mustachejava.codes.IterableCode;
3232
import com.github.mustachejava.codes.WriteCode;
33+
import org.elasticsearch.common.Strings;
3334
import org.elasticsearch.common.xcontent.XContentBuilder;
3435
import org.elasticsearch.common.xcontent.XContentType;
3536

@@ -215,7 +216,7 @@ protected Function<String, String> createFunction(Object resolved) {
215216
// Do not handle as JSON
216217
return oh.stringify(resolved);
217218
}
218-
return builder.string();
219+
return Strings.toString(builder);
219220
} catch (IOException e) {
220221
throw new MustacheException("Failed to convert object to JSON", e);
221222
}

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.client.node.NodeClient;
2424
import org.elasticsearch.common.ParseField;
2525
import org.elasticsearch.common.ParsingException;
26+
import org.elasticsearch.common.Strings;
2627
import org.elasticsearch.common.settings.Settings;
2728
import org.elasticsearch.common.xcontent.ObjectParser;
2829
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -63,7 +64,7 @@ public class RestSearchTemplateAction extends BaseRestHandler {
6364
if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
6465
//convert the template to json which is the only supported XContentType (see CustomMustacheFactory#createEncoder)
6566
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
66-
request.setScript(builder.copyCurrentStructure(parser).string());
67+
request.setScript(Strings.toString(builder.copyCurrentStructure(parser)));
6768
} catch (IOException e) {
6869
throw new ParsingException(parser.getTokenLocation(), "Could not parse inline template", e);
6970
}

0 commit comments

Comments
 (0)