Skip to content

Commit d7eae4b

Browse files
authored
Pass InputStream when creating XContent parser (#28754)
* Pass InputStream when creating XContent parser Rather than passing the raw `BytesReference` in when creating the xcontent parser, this passes the StreamInput (which is an InputStream), this allows us to decouple XContent from BytesReference. This also removes the use of `commons.Booleans` so it doesn't require more external commons classes. Related to #28504 * Undo boolean removal * Enhance deprecation javadoc
1 parent 260d68a commit d7eae4b

File tree

21 files changed

+42
-34
lines changed

21 files changed

+42
-34
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void execute(IngestDocument document) throws Exception {
7878
Object fieldValue = document.getFieldValue(field, Object.class);
7979
BytesReference bytesRef = (fieldValue == null) ? new BytesArray("null") : new BytesArray(fieldValue.toString());
8080
try (XContentParser parser = JsonXContent.jsonXContent
81-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytesRef)) {
81+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytesRef.streamInput())) {
8282
XContentParser.Token token = parser.nextToken();
8383
Object value = null;
8484
if (token == XContentParser.Token.VALUE_NULL) {

modules/reindex/src/main/java/org/elasticsearch/index/reindex/AbstractBulkByQueryRestHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private XContentParser extractRequestSpecificFields(RestRequest restRequest,
9191
}
9292
}
9393
return parser.contentType().xContent().createParser(parser.getXContentRegistry(),
94-
parser.getDeprecationHandler(), builder.map(body).bytes());
94+
parser.getDeprecationHandler(), builder.map(body).bytes().streamInput());
9595
}
9696
}
9797
}

modules/reindex/src/main/java/org/elasticsearch/index/reindex/RestReindexAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexReq
7575
XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType());
7676
builder.map(source);
7777
try (XContentParser innerParser = parser.contentType().xContent()
78-
.createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), builder.bytes())) {
78+
.createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), builder.bytes().streamInput())) {
7979
request.getSearchRequest().source().parseXContent(innerParser);
8080
}
8181
};

modules/reindex/src/main/java/org/elasticsearch/index/reindex/TransportReindexAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ protected RequestWrapper<IndexRequest> buildRequest(ScrollableHitSource.Hit doc)
339339
if (mainRequestXContentType != null && doc.getXContentType() != mainRequestXContentType) {
340340
// we need to convert
341341
try (XContentParser parser = sourceXContentType.xContent()
342-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, doc.getSource());
342+
.createParser(NamedXContentRegistry.EMPTY,
343+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, doc.getSource().streamInput());
343344
XContentBuilder builder = XContentBuilder.builder(mainRequestXContentType.xContent())) {
344345
parser.nextToken();
345346
builder.copyCurrentStructure(parser);

server/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
307307
// EMPTY is safe here because we never call namedObject
308308
try (XContentParser parser = xContent
309309
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE,
310-
data.slice(from, nextMarker - from))) {
310+
data.slice(from, nextMarker - from).streamInput())) {
311311
// move pointers
312312
from = nextMarker + 1;
313313

@@ -432,7 +432,7 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
432432
.parent(parent);
433433
// EMPTY is safe here because we never call namedObject
434434
try (XContentParser sliceParser = xContent.createParser(NamedXContentRegistry.EMPTY,
435-
LoggingDeprecationHandler.INSTANCE, sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType))) {
435+
LoggingDeprecationHandler.INSTANCE, sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType).streamInput())) {
436436
updateRequest.fromXContent(sliceParser);
437437
}
438438
if (fetchSourceContext != null) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ public static void readMultiLineFormat(BytesReference data,
208208
// now parse the action
209209
if (nextMarker - from > 0) {
210210
try (XContentParser parser = xContent
211-
.createParser(registry, LoggingDeprecationHandler.INSTANCE, data.slice(from, nextMarker - from))) {
211+
.createParser(registry, LoggingDeprecationHandler.INSTANCE,
212+
data.slice(from, nextMarker - from).streamInput())) {
212213
Map<String, Object> source = parser.map();
213214
for (Map.Entry<String, Object> entry : source.entrySet()) {
214215
Object value = entry.getValue();
@@ -244,7 +245,7 @@ public static void readMultiLineFormat(BytesReference data,
244245
break;
245246
}
246247
BytesReference bytes = data.slice(from, nextMarker - from);
247-
try (XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, bytes)) {
248+
try (XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, bytes.streamInput())) {
248249
consumer.accept(searchRequest, parser);
249250
}
250251
// move pointers

server/src/main/java/org/elasticsearch/common/xcontent/XContent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ XContentParser createParser(NamedXContentRegistry xContentRegistry,
106106

107107
/**
108108
* Creates a parser over the provided bytes.
109+
* @deprecated use {@link #createParser(NamedXContentRegistry, DeprecationHandler, InputStream)} instead,
110+
* the BytesReference coupling in this class will be removed in a future commit
109111
*/
112+
@Deprecated
110113
XContentParser createParser(NamedXContentRegistry xContentRegistry,
111114
DeprecationHandler deprecationHandler, BytesReference bytes) throws IOException;
112115

server/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ protected ScoreFunction doToFunction(QueryShardContext context) throws IOExcepti
183183
AbstractDistanceScoreFunction scoreFunction;
184184
// EMPTY is safe because parseVariable doesn't use namedObject
185185
try (XContentParser parser = XContentFactory.xContent(functionBytes)
186-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, functionBytes)) {
186+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, functionBytes.streamInput())) {
187187
scoreFunction = parseVariable(fieldName, parser, context, multiValueMode);
188188
}
189189
return scoreFunction;

server/src/main/java/org/elasticsearch/rest/RestRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public NamedXContentRegistry getXContentRegistry() {
343343
*/
344344
public final XContentParser contentParser() throws IOException {
345345
BytesReference content = requiredContent(); // will throw exception if body or content type missing
346-
return xContentType.get().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content);
346+
return xContentType.get().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content.streamInput());
347347
}
348348

349349
/**
@@ -372,7 +372,7 @@ public final boolean hasContentOrSourceParam() {
372372
*/
373373
public final XContentParser contentOrSourceParamParser() throws IOException {
374374
Tuple<XContentType, BytesReference> tuple = contentOrSourceParam();
375-
return tuple.v1().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, tuple.v2());
375+
return tuple.v1().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, tuple.v2().streamInput());
376376
}
377377

378378
/**
@@ -386,7 +386,7 @@ public final void withContentOrSourceParamParserOrNull(CheckedConsumer<XContentP
386386
BytesReference content = tuple.v2();
387387
XContentType xContentType = tuple.v1();
388388
try (XContentParser parser = xContentType.xContent()
389-
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content)) {
389+
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content.streamInput())) {
390390
withParser.accept(parser);
391391
}
392392
} else {

server/src/main/java/org/elasticsearch/script/Script.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public static Script parse(Settings settings) {
283283
settings.toXContent(builder, ToXContent.EMPTY_PARAMS);
284284
builder.endObject();
285285
return parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
286-
LoggingDeprecationHandler.INSTANCE, builder.bytes()));
286+
LoggingDeprecationHandler.INSTANCE, builder.bytes().streamInput()));
287287
} catch (IOException e) {
288288
// it should not happen since we are not actually reading from a stream but an in-memory byte[]
289289
throw new IllegalStateException(e);

server/src/main/java/org/elasticsearch/script/StoredScriptSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private StoredScriptSource build() {
244244
*/
245245
public static StoredScriptSource parse(BytesReference content, XContentType xContentType) {
246246
try (XContentParser parser = xContentType.xContent()
247-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, content)) {
247+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, content.streamInput())) {
248248
Token token = parser.nextToken();
249249

250250
if (token != Token.START_OBJECT) {

server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ public void testNamedObject() throws IOException {
10151015
new NamedXContentRegistry.Entry(Object.class, new ParseField("str"), p -> p.text())));
10161016
XContentBuilder b = XContentBuilder.builder(xcontentType().xContent());
10171017
b.value("test");
1018-
XContentParser p = xcontentType().xContent().createParser(registry, LoggingDeprecationHandler.INSTANCE, b.bytes());
1018+
XContentParser p = xcontentType().xContent().createParser(registry, LoggingDeprecationHandler.INSTANCE, b.bytes().streamInput());
10191019
assertEquals(test1, p.namedObject(Object.class, "test1", null));
10201020
assertEquals(test2, p.namedObject(Object.class, "test2", null));
10211021
assertEquals(test2, p.namedObject(Object.class, "deprecated", null));

server/src/test/java/org/elasticsearch/common/xcontent/XContentParserUtilsTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void testParseTypedKeysObject() throws IOException {
167167
BytesReference bytes = toXContent((builder, params) -> builder.startObject("name").field("field", 0).endObject(), xContentType,
168168
randomBoolean());
169169
try (XContentParser parser = xContentType.xContent()
170-
.createParser(namedXContentRegistry, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes)) {
170+
.createParser(namedXContentRegistry, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
171171
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
172172
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
173173
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
@@ -183,7 +183,7 @@ public void testParseTypedKeysObject() throws IOException {
183183
bytes = toXContent((builder, params) -> builder.startObject("type" + delimiter + "name").field("bool", true).endObject(),
184184
xContentType, randomBoolean());
185185
try (XContentParser parser = xContentType.xContent()
186-
.createParser(namedXContentRegistry, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes)) {
186+
.createParser(namedXContentRegistry, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
187187
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
188188
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
189189
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
@@ -203,7 +203,7 @@ public void testParseTypedKeysObject() throws IOException {
203203
}, xContentType, randomBoolean());
204204

205205
try (XContentParser parser = xContentType.xContent()
206-
.createParser(namedXContentRegistry, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes)) {
206+
.createParser(namedXContentRegistry, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
207207
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
208208

209209
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
@@ -230,7 +230,7 @@ public void testParseTypedKeysObjectErrors() throws IOException {
230230
BytesReference bytes = toXContent((builder, params) -> builder.startObject("name").field("field", 0).endObject(), xContentType,
231231
randomBoolean());
232232
try (XContentParser parser = xContentType.xContent()
233-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes)) {
233+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
234234
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
235235
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
236236
ParsingException exception = expectThrows(ParsingException.class,
@@ -243,7 +243,7 @@ public void testParseTypedKeysObjectErrors() throws IOException {
243243
BytesReference bytes = toXContent((builder, params) -> builder.startObject("").field("field", 0).endObject(), xContentType,
244244
randomBoolean());
245245
try (XContentParser parser = xContentType.xContent()
246-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes)) {
246+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
247247
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
248248
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
249249
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);

server/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testToXContent() throws IOException {
9595
} else {
9696
BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, randomBoolean());
9797
try (XContentParser parser = xContentType.xContent()
98-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes)) {
98+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
9999
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
100100
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
101101
assertTrue(parser.nextToken().isValue());

server/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractXContentFilteringTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ static void assertXContentBuilderAsBytes(final XContentBuilder expected, final X
7676
try {
7777
XContent xContent = XContentFactory.xContent(actual.contentType());
7878
XContentParser jsonParser =
79-
xContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, expected.bytes());
79+
xContent.createParser(NamedXContentRegistry.EMPTY,
80+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, expected.bytes().streamInput());
8081
XContentParser testParser =
81-
xContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, actual.bytes());
82+
xContent.createParser(NamedXContentRegistry.EMPTY,
83+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, actual.bytes().streamInput());
8284

8385
while (true) {
8486
XContentParser.Token token1 = jsonParser.nextToken();

server/src/test/java/org/elasticsearch/ingest/PipelineConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void testParser() throws IOException {
6262
}
6363

6464
XContentParser xContentParser = xContentType.xContent()
65-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes);
65+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput());
6666
PipelineConfiguration parsed = parser.parse(xContentParser, null);
6767
assertEquals(xContentType, parsed.getXContentType());
6868
assertEquals("{}", XContentHelper.convertToJson(parsed.getConfig(), false, parsed.getXContentType()));

server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public void testFromXContentLoading() throws Exception {
3939
XContentBuilder builder = XContentFactory.jsonBuilder();
4040
builder.startObject().field("lang0#id0", "script0").field("lang1#id0", "script1").endObject();
4141
XContentParser parser0 = XContentType.JSON.xContent()
42-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, builder.bytes());
42+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, builder.bytes().streamInput());
4343
expectThrows(IllegalArgumentException.class, () -> ScriptMetaData.fromXContent(parser0));
4444

4545
// failure to load a new namespace script and old namespace script with the same id but different langs
4646
builder = XContentFactory.jsonBuilder();
4747
builder.startObject().field("lang0#id0", "script0")
4848
.startObject("id0").field("lang", "lang1").field("source", "script1").endObject().endObject();
4949
XContentParser parser1 = XContentType.JSON.xContent()
50-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, builder.bytes());
50+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, builder.bytes().streamInput());
5151
expectThrows(IllegalArgumentException.class, () -> ScriptMetaData.fromXContent(parser1));
5252

5353
// failure to load a new namespace script and old namespace script with the same id but different langs with additional scripts
@@ -56,15 +56,15 @@ public void testFromXContentLoading() throws Exception {
5656
.startObject("id1").field("lang", "lang0").field("source", "script0").endObject()
5757
.startObject("id0").field("lang", "lang1").field("source", "script1").endObject().endObject();
5858
XContentParser parser2 = XContentType.JSON.xContent()
59-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, builder.bytes());
59+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, builder.bytes().streamInput());
6060
expectThrows(IllegalArgumentException.class, () -> ScriptMetaData.fromXContent(parser2));
6161

6262
// okay to load the same script from the new and old namespace if the lang is the same
6363
builder = XContentFactory.jsonBuilder();
6464
builder.startObject().field("lang0#id0", "script0")
6565
.startObject("id0").field("lang", "lang0").field("source", "script1").endObject().endObject();
6666
XContentParser parser3 = XContentType.JSON.xContent()
67-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, builder.bytes());
67+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, builder.bytes().streamInput());
6868
ScriptMetaData.fromXContent(parser3);
6969
}
7070

test/framework/src/main/java/org/elasticsearch/search/RandomSearchRequestGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ public static SearchSourceBuilder randomSearchSourceBuilder(
311311
jsonBuilder.endArray();
312312
jsonBuilder.endObject();
313313
XContentParser parser = XContentFactory.xContent(XContentType.JSON)
314-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, jsonBuilder.bytes());
314+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
315+
jsonBuilder.bytes().streamInput());
315316
parser.nextToken();
316317
parser.nextToken();
317318
parser.nextToken();

0 commit comments

Comments
 (0)