Skip to content

Commit 8e8fdc4

Browse files
authored
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 ef6fc1e commit 8e8fdc4

File tree

301 files changed

+3563
-3369
lines changed

Some content is hidden

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

301 files changed

+3563
-3369
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
@@ -328,7 +328,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
328328
}
329329
metadata.endObject();
330330

331-
BytesRef metadataSource = metadata.bytes().toBytesRef();
331+
BytesRef metadataSource = BytesReference.bytes(metadata).toBytesRef();
332332
content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length);
333333
content.write(separator);
334334
}
@@ -343,7 +343,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
343343
LoggingDeprecationHandler.INSTANCE, indexSource, indexXContentType)) {
344344
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
345345
builder.copyCurrentStructure(parser);
346-
source = builder.bytes().toBytesRef();
346+
source = BytesReference.bytes(builder).toBytesRef();
347347
}
348348
}
349349
} 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.elasticsearch.client.Response;
3434
import org.elasticsearch.client.RestHighLevelClient;
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/main/java/org/elasticsearch/ingest/common/ScriptProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
import com.fasterxml.jackson.core.JsonFactory;
2323

24+
import org.elasticsearch.common.bytes.BytesReference;
2425
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2526
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2627
import org.elasticsearch.common.xcontent.XContentBuilder;
2728
import org.elasticsearch.common.xcontent.XContentParser;
2829
import org.elasticsearch.common.xcontent.XContentType;
2930
import org.elasticsearch.common.xcontent.json.JsonXContent;
30-
import org.elasticsearch.common.xcontent.json.JsonXContentParser;
3131
import org.elasticsearch.ingest.AbstractProcessor;
3232
import org.elasticsearch.ingest.IngestDocument;
3333
import org.elasticsearch.ingest.Processor;
@@ -99,7 +99,7 @@ public Factory(ScriptService scriptService) {
9999
public ScriptProcessor create(Map<String, Processor.Factory> registry, String processorTag,
100100
Map<String, Object> config) throws Exception {
101101
try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent).map(config);
102-
InputStream stream = builder.bytes().streamInput();
102+
InputStream stream = BytesReference.bytes(builder).streamInput();
103103
XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY,
104104
LoggingDeprecationHandler.INSTANCE, stream)) {
105105
Script script = Script.parse(parser);

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
}

0 commit comments

Comments
 (0)