Skip to content

Commit b59b1cf

Browse files
authored
Move more XContent.createParser calls to non-deprecated version (#28672)
* Move more XContent.createParser calls to non-deprecated version Part 2 This moves more of the callers to pass in the DeprecationHandler. Relates to #28504 * Use parser's deprecation handler where appropriate * Use logging handler in test that uses deprecated field on purpose
1 parent 7c1f5f5 commit b59b1cf

File tree

19 files changed

+78
-33
lines changed

19 files changed

+78
-33
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.ElasticsearchParseException;
2525
import org.elasticsearch.common.bytes.BytesArray;
2626
import org.elasticsearch.common.bytes.BytesReference;
27+
import org.elasticsearch.common.xcontent.DeprecationHandler;
2728
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2829
import org.elasticsearch.common.xcontent.XContentHelper;
2930
import org.elasticsearch.common.xcontent.XContentParser;
@@ -76,7 +77,8 @@ boolean isAddToRoot() {
7677
public void execute(IngestDocument document) throws Exception {
7778
Object fieldValue = document.getFieldValue(field, Object.class);
7879
BytesReference bytesRef = (fieldValue == null) ? new BytesArray("null") : new BytesArray(fieldValue.toString());
79-
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, bytesRef)) {
80+
try (XContentParser parser = JsonXContent.jsonXContent
81+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytesRef)) {
8082
XContentParser.Token token = parser.nextToken();
8183
Object value = null;
8284
if (token == XContentParser.Token.VALUE_NULL) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.action.GenericAction;
2323
import org.elasticsearch.action.search.SearchRequest;
2424
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2526
import org.elasticsearch.common.xcontent.XContentBuilder;
2627
import org.elasticsearch.common.xcontent.XContentFactory;
2728
import org.elasticsearch.common.xcontent.XContentParser;
@@ -89,7 +90,8 @@ private XContentParser extractRequestSpecificFields(RestRequest restRequest,
8990
consumer.getValue().accept(value);
9091
}
9192
}
92-
return parser.contentType().xContent().createParser(parser.getXContentRegistry(), builder.map(body).bytes());
93+
return parser.contentType().xContent().createParser(parser.getXContentRegistry(),
94+
parser.getDeprecationHandler(), builder.map(body).bytes());
9395
}
9496
}
9597
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.common.bytes.BytesReference;
2828
import org.elasticsearch.common.settings.Settings;
2929
import org.elasticsearch.common.unit.TimeValue;
30+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3031
import org.elasticsearch.common.xcontent.ObjectParser;
3132
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
3233
import org.elasticsearch.common.xcontent.ToXContent;
@@ -73,7 +74,8 @@ public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexReq
7374
request.setRemoteInfo(buildRemoteInfo(source));
7475
XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType());
7576
builder.map(source);
76-
try (XContentParser innerParser = parser.contentType().xContent().createParser(parser.getXContentRegistry(), builder.bytes())) {
77+
try (XContentParser innerParser = parser.contentType().xContent()
78+
.createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), builder.bytes())) {
7779
request.getSearchRequest().source().parseXContent(innerParser);
7880
}
7981
};

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.elasticsearch.action.ActionRequestValidationException;
3838
import org.elasticsearch.action.bulk.BackoffPolicy;
3939
import org.elasticsearch.action.bulk.BulkItemResponse.Failure;
40+
import org.elasticsearch.common.xcontent.DeprecationHandler;
41+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
4042
import org.elasticsearch.index.reindex.ScrollableHitSource.SearchFailure;
4143
import org.elasticsearch.action.index.IndexRequest;
4244
import org.elasticsearch.action.search.SearchRequest;
@@ -336,7 +338,8 @@ protected RequestWrapper<IndexRequest> buildRequest(ScrollableHitSource.Hit doc)
336338
final XContentType mainRequestXContentType = mainRequest.getDestination().getContentType();
337339
if (mainRequestXContentType != null && doc.getXContentType() != mainRequestXContentType) {
338340
// we need to convert
339-
try (XContentParser parser = sourceXContentType.xContent().createParser(NamedXContentRegistry.EMPTY, doc.getSource());
341+
try (XContentParser parser = sourceXContentType.xContent()
342+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, doc.getSource());
340343
XContentBuilder builder = XContentBuilder.builder(mainRequestXContentType.xContent())) {
341344
parser.nextToken();
342345
builder.copyCurrentStructure(parser);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.common.logging.Loggers;
4141
import org.elasticsearch.common.lucene.uid.Versions;
4242
import org.elasticsearch.common.unit.TimeValue;
43+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
4344
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
4445
import org.elasticsearch.common.xcontent.XContent;
4546
import org.elasticsearch.common.xcontent.XContentParser;
@@ -304,7 +305,9 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
304305

305306
// now parse the action
306307
// EMPTY is safe here because we never call namedObject
307-
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, data.slice(from, nextMarker - from))) {
308+
try (XContentParser parser = xContent
309+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE,
310+
data.slice(from, nextMarker - from))) {
308311
// move pointers
309312
from = nextMarker + 1;
310313

@@ -429,7 +432,7 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
429432
.parent(parent);
430433
// EMPTY is safe here because we never call namedObject
431434
try (XContentParser sliceParser = xContent.createParser(NamedXContentRegistry.EMPTY,
432-
sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType))) {
435+
LoggingDeprecationHandler.INSTANCE, sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType))) {
433436
updateRequest.fromXContent(sliceParser);
434437
}
435438
if (fetchSourceContext != null) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.common.bytes.BytesReference;
2828
import org.elasticsearch.common.io.stream.StreamInput;
2929
import org.elasticsearch.common.io.stream.StreamOutput;
30+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3031
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3132
import org.elasticsearch.common.xcontent.ToXContent;
3233
import org.elasticsearch.common.xcontent.XContent;
@@ -206,7 +207,8 @@ public static void readMultiLineFormat(BytesReference data,
206207
IndicesOptions defaultOptions = SearchRequest.DEFAULT_INDICES_OPTIONS;
207208
// now parse the action
208209
if (nextMarker - from > 0) {
209-
try (XContentParser parser = xContent.createParser(registry, data.slice(from, nextMarker - from))) {
210+
try (XContentParser parser = xContent
211+
.createParser(registry, LoggingDeprecationHandler.INSTANCE, data.slice(from, nextMarker - from))) {
210212
Map<String, Object> source = parser.map();
211213
for (Map.Entry<String, Object> entry : source.entrySet()) {
212214
Object value = entry.getValue();
@@ -242,7 +244,7 @@ public static void readMultiLineFormat(BytesReference data,
242244
break;
243245
}
244246
BytesReference bytes = data.slice(from, nextMarker - from);
245-
try (XContentParser parser = xContent.createParser(registry, bytes)) {
247+
try (XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, bytes)) {
246248
consumer.accept(searchRequest, parser);
247249
}
248250
// move pointers

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
3535
import org.elasticsearch.common.unit.DistanceUnit;
3636
import org.elasticsearch.common.unit.TimeValue;
37+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3738
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3839
import org.elasticsearch.common.xcontent.XContentBuilder;
3940
import org.elasticsearch.common.xcontent.XContentFactory;
@@ -181,7 +182,8 @@ protected int doHashCode() {
181182
protected ScoreFunction doToFunction(QueryShardContext context) throws IOException {
182183
AbstractDistanceScoreFunction scoreFunction;
183184
// EMPTY is safe because parseVariable doesn't use namedObject
184-
try (XContentParser parser = XContentFactory.xContent(functionBytes).createParser(NamedXContentRegistry.EMPTY, functionBytes)) {
185+
try (XContentParser parser = XContentFactory.xContent(functionBytes)
186+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, functionBytes)) {
185187
scoreFunction = parseVariable(fieldName, parser, context, multiValueMode);
186188
}
187189
return scoreFunction;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.common.collect.Tuple;
3131
import org.elasticsearch.common.unit.ByteSizeValue;
3232
import org.elasticsearch.common.unit.TimeValue;
33+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3334
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3435
import org.elasticsearch.common.xcontent.ToXContent;
3536
import org.elasticsearch.common.xcontent.XContentParser;
@@ -342,7 +343,7 @@ public NamedXContentRegistry getXContentRegistry() {
342343
*/
343344
public final XContentParser contentParser() throws IOException {
344345
BytesReference content = requiredContent(); // will throw exception if body or content type missing
345-
return xContentType.get().xContent().createParser(xContentRegistry, content);
346+
return xContentType.get().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content);
346347
}
347348

348349
/**
@@ -371,7 +372,7 @@ public final boolean hasContentOrSourceParam() {
371372
*/
372373
public final XContentParser contentOrSourceParamParser() throws IOException {
373374
Tuple<XContentType, BytesReference> tuple = contentOrSourceParam();
374-
return tuple.v1().xContent().createParser(xContentRegistry, tuple.v2());
375+
return tuple.v1().xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, tuple.v2());
375376
}
376377

377378
/**
@@ -384,7 +385,8 @@ public final void withContentOrSourceParamParserOrNull(CheckedConsumer<XContentP
384385
Tuple<XContentType, BytesReference> tuple = contentOrSourceParam();
385386
BytesReference content = tuple.v2();
386387
XContentType xContentType = tuple.v1();
387-
try (XContentParser parser = xContentType.xContent().createParser(xContentRegistry, content)) {
388+
try (XContentParser parser = xContentType.xContent()
389+
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content)) {
388390
withParser.accept(parser);
389391
}
390392
} else {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.common.io.stream.StreamOutput;
2727
import org.elasticsearch.common.io.stream.Writeable;
2828
import org.elasticsearch.common.settings.Settings;
29+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2930
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3031
import org.elasticsearch.common.xcontent.ObjectParser;
3132
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
@@ -281,7 +282,8 @@ public static Script parse(Settings settings) {
281282
builder.startObject();
282283
settings.toXContent(builder, ToXContent.EMPTY_PARAMS);
283284
builder.endObject();
284-
return parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, builder.bytes()));
285+
return parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
286+
LoggingDeprecationHandler.INSTANCE, builder.bytes()));
285287
} catch (IOException e) {
286288
// it should not happen since we are not actually reading from a stream but an in-memory byte[]
287289
throw new IllegalStateException(e);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.common.io.stream.StreamInput;
3232
import org.elasticsearch.common.io.stream.StreamOutput;
3333
import org.elasticsearch.common.io.stream.Writeable;
34+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3435
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3536
import org.elasticsearch.common.xcontent.ObjectParser;
3637
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
@@ -242,7 +243,8 @@ private StoredScriptSource build() {
242243
* @return The parsed {@link StoredScriptSource}.
243244
*/
244245
public static StoredScriptSource parse(BytesReference content, XContentType xContentType) {
245-
try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, content)) {
246+
try (XContentParser parser = xContentType.xContent()
247+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, content)) {
246248
Token token = parser.nextToken();
247249

248250
if (token != Token.START_OBJECT) {

0 commit comments

Comments
 (0)