Skip to content

Commit eebff4d

Browse files
authored
Use non deprecated xcontenthelper (elastic#28503)
* Move to non-deprecated XContentHelper.createParser(...) This moves away from one of the now-deprecated XContentHelper.createParser methods in favor of specifying the deprecation logger at parser creation time. Relates to elastic#28449 Note that this doesn't move all the `createParser` calls because some of them use the already-deprecated method that doesn't specify the XContentType. * Remove the deprecated (and now non-needed) createParser method
1 parent 5c1d3aa commit eebff4d

File tree

17 files changed

+66
-31
lines changed

17 files changed

+66
-31
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.elasticsearch.common.bytes.BytesReference;
5959
import org.elasticsearch.common.lucene.uid.Versions;
6060
import org.elasticsearch.common.unit.TimeValue;
61+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
6162
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
6263
import org.elasticsearch.common.xcontent.ToXContent;
6364
import org.elasticsearch.common.xcontent.XContent;
@@ -316,7 +317,8 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
316317
BytesReference indexSource = indexRequest.source();
317318
XContentType indexXContentType = indexRequest.getContentType();
318319

319-
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, indexSource, indexXContentType)) {
320+
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
321+
LoggingDeprecationHandler.INSTANCE, indexSource, indexXContentType)) {
320322
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
321323
builder.copyCurrentStructure(parser);
322324
source = builder.bytes().toBytesRef();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.elasticsearch.common.CheckedFunction;
5252
import org.elasticsearch.common.ParseField;
5353
import org.elasticsearch.common.xcontent.ContextParser;
54+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
5455
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
5556
import org.elasticsearch.common.xcontent.XContentParser;
5657
import org.elasticsearch.common.xcontent.XContentType;
@@ -637,7 +638,8 @@ protected final <Resp> Resp parseEntity(final HttpEntity entity,
637638
if (xContentType == null) {
638639
throw new IllegalStateException("Unsupported Content-Type: " + entity.getContentType().getValue());
639640
}
640-
try (XContentParser parser = xContentType.xContent().createParser(registry, entity.getContent())) {
641+
try (XContentParser parser = xContentType.xContent().createParser(registry,
642+
LoggingDeprecationHandler.INSTANCE, entity.getContent())) {
641643
return entityParser.apply(parser);
642644
}
643645
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

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

24+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2425
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2526
import org.elasticsearch.common.xcontent.XContentBuilder;
2627
import org.elasticsearch.common.xcontent.XContentParser;
@@ -97,7 +98,8 @@ public Factory(ScriptService scriptService) {
9798
public ScriptProcessor create(Map<String, Processor.Factory> registry, String processorTag,
9899
Map<String, Object> config) throws Exception {
99100
XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent).map(config);
100-
XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, builder.bytes().streamInput());
101+
XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY,
102+
LoggingDeprecationHandler.INSTANCE, builder.bytes().streamInput());
101103
Script script = Script.parse(parser);
102104

103105
Arrays.asList("id", "source", "inline", "lang", "params", "options").forEach(config::remove);

modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.common.bytes.BytesArray;
3232
import org.elasticsearch.common.inject.Inject;
3333
import org.elasticsearch.common.settings.Settings;
34+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3435
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3536
import org.elasticsearch.common.xcontent.XContentParser;
3637
import org.elasticsearch.common.xcontent.XContentType;
@@ -105,7 +106,8 @@ protected void doExecute(RankEvalRequest request, ActionListener<RankEvalRespons
105106
String templateId = ratedRequest.getTemplateId();
106107
TemplateScript.Factory templateScript = scriptsWithoutParams.get(templateId);
107108
String resolvedRequest = templateScript.newInstance(params).execute();
108-
try (XContentParser subParser = createParser(namedXContentRegistry, new BytesArray(resolvedRequest), XContentType.JSON)) {
109+
try (XContentParser subParser = createParser(namedXContentRegistry,
110+
LoggingDeprecationHandler.INSTANCE, new BytesArray(resolvedRequest), XContentType.JSON)) {
109111
ratedSearchSource = SearchSourceBuilder.fromXContent(subParser);
110112
} catch (IOException e) {
111113
// if we fail parsing, put the exception into the errors map and continue

modules/reindex/src/main/java/org/elasticsearch/index/reindex/remote/RemoteScrollableHitSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.ElasticsearchStatusException;
3131
import org.elasticsearch.Version;
3232
import org.elasticsearch.action.bulk.BackoffPolicy;
33+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3334
import org.elasticsearch.index.reindex.ScrollableHitSource;
3435
import org.elasticsearch.action.search.SearchRequest;
3536
import org.elasticsearch.client.ResponseException;
@@ -196,7 +197,7 @@ public void onSuccess(org.elasticsearch.client.Response response) {
196197
}
197198
// EMPTY is safe here because we don't call namedObject
198199
try (XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY,
199-
content)) {
200+
LoggingDeprecationHandler.INSTANCE, content)) {
200201
parsedResponse = parser.apply(xContentParser, xContentType);
201202
} catch (ParsingException e) {
202203
/* Because we're streaming the response we can't get a copy of it here. The best we can do is hint that it

plugins/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.ingest.useragent;
2121

2222
import org.elasticsearch.ElasticsearchParseException;
23+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2324
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2425
import org.elasticsearch.common.xcontent.XContentFactory;
2526
import org.elasticsearch.common.xcontent.XContentParser;
@@ -54,7 +55,8 @@ final class UserAgentParser {
5455

5556
private void init(InputStream regexStream) throws IOException {
5657
// EMPTY is safe here because we don't use namedObject
57-
XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML).createParser(NamedXContentRegistry.EMPTY, regexStream);
58+
XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML).createParser(NamedXContentRegistry.EMPTY,
59+
LoggingDeprecationHandler.INSTANCE, regexStream);
5860

5961
XContentParser.Token token = yamlParser.nextToken();
6062

server/src/main/java/org/elasticsearch/action/admin/indices/alias/Alias.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,16 @@ public static Alias fromXContent(XContentParser parser) throws IOException {
205205
if (token == XContentParser.Token.FIELD_NAME) {
206206
currentFieldName = parser.currentName();
207207
} else if (token == XContentParser.Token.START_OBJECT) {
208-
if (FILTER.match(currentFieldName)) {
208+
if (FILTER.match(currentFieldName, parser.getDeprecationHandler())) {
209209
Map<String, Object> filter = parser.mapOrdered();
210210
alias.filter(filter);
211211
}
212212
} else if (token == XContentParser.Token.VALUE_STRING) {
213-
if (ROUTING.match(currentFieldName)) {
213+
if (ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
214214
alias.routing(parser.text());
215-
} else if (INDEX_ROUTING.match(currentFieldName)) {
215+
} else if (INDEX_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
216216
alias.indexRouting(parser.text());
217-
} else if (SEARCH_ROUTING.match(currentFieldName)) {
217+
} else if (SEARCH_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
218218
alias.searchRouting(parser.text());
219219
}
220220
}

server/src/main/java/org/elasticsearch/action/update/UpdateRequest.java

Lines changed: 5 additions & 2 deletions
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.lucene.uid.Versions;
34+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3435
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3536
import org.elasticsearch.common.xcontent.ToXContentObject;
3637
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -864,7 +865,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
864865
}
865866
if (doc != null) {
866867
XContentType xContentType = doc.getContentType();
867-
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, doc.source(), xContentType)) {
868+
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
869+
LoggingDeprecationHandler.INSTANCE, doc.source(), xContentType)) {
868870
builder.field("doc");
869871
builder.copyCurrentStructure(parser);
870872
}
@@ -874,7 +876,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
874876
}
875877
if (upsertRequest != null) {
876878
XContentType xContentType = upsertRequest.getContentType();
877-
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, upsertRequest.source(), xContentType)) {
879+
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
880+
LoggingDeprecationHandler.INSTANCE, upsertRequest.source(), xContentType)) {
878881
builder.field("upsert");
879882
builder.copyCurrentStructure(parser);
880883
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class XContentHelper {
4040

4141
/**
4242
* Creates a parser based on the bytes provided
43-
* @deprecated use {@link #createParser(NamedXContentRegistry, BytesReference, XContentType)} to avoid content type auto-detection
43+
* @deprecated use {@link #createParser(NamedXContentRegistry, DeprecationHandler, BytesReference, XContentType)} to avoid content type auto-detection
4444
*/
4545
@Deprecated
4646
public static XContentParser createParser(NamedXContentRegistry xContentRegistry, BytesReference bytes) throws IOException {
@@ -60,16 +60,16 @@ public static XContentParser createParser(NamedXContentRegistry xContentRegistry
6060
/**
6161
* Creates a parser for the bytes using the supplied content-type
6262
*/
63-
public static XContentParser createParser(NamedXContentRegistry xContentRegistry, BytesReference bytes,
64-
XContentType xContentType) throws IOException {
63+
public static XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler,
64+
BytesReference bytes, XContentType xContentType) throws IOException {
6565
Objects.requireNonNull(xContentType);
6666
Compressor compressor = CompressorFactory.compressor(bytes);
6767
if (compressor != null) {
6868
InputStream compressedInput = compressor.streamInput(bytes.streamInput());
6969
if (compressedInput.markSupported() == false) {
7070
compressedInput = new BufferedInputStream(compressedInput);
7171
}
72-
return XContentFactory.xContent(xContentType).createParser(xContentRegistry, compressedInput);
72+
return XContentFactory.xContent(xContentType).createParser(xContentRegistry, deprecationHandler, compressedInput);
7373
} else {
7474
return xContentType.xContent().createParser(xContentRegistry, bytes.streamInput());
7575
}
@@ -131,7 +131,7 @@ public static Map<String, Object> convertToMap(XContent xContent, String string,
131131
public static Map<String, Object> convertToMap(XContent xContent, InputStream input, boolean ordered)
132132
throws ElasticsearchParseException {
133133
// It is safe to use EMPTY here because this never uses namedObject
134-
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, input)) {
134+
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, input)) {
135135
return ordered ? parser.mapOrdered() : parser.map();
136136
} catch (IOException e) {
137137
throw new ElasticsearchParseException("Failed to parse content to map", e);
@@ -161,7 +161,7 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, b
161161

162162
// It is safe to use EMPTY here because this never uses namedObject
163163
try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY,
164-
bytes.streamInput())) {
164+
LoggingDeprecationHandler.INSTANCE, bytes.streamInput())) {
165165
parser.nextToken();
166166
XContentBuilder builder = XContentFactory.jsonBuilder();
167167
if (prettyPrint) {

server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.common.Strings;
2626
import org.elasticsearch.common.collect.Tuple;
2727
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
28+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2829
import org.elasticsearch.common.xcontent.XContentHelper;
2930
import org.elasticsearch.common.xcontent.XContentParser;
3031
import org.elasticsearch.common.xcontent.XContentType;
@@ -61,7 +62,8 @@ ParsedDocument parseDocument(SourceToParse source) throws MapperParsingException
6162
final ParseContext.InternalParseContext context;
6263
final XContentType xContentType = source.getXContentType();
6364

64-
try (XContentParser parser = XContentHelper.createParser(docMapperParser.getXContentRegistry(), source.source(), xContentType)) {
65+
try (XContentParser parser = XContentHelper.createParser(docMapperParser.getXContentRegistry(),
66+
LoggingDeprecationHandler.INSTANCE, source.source(), xContentType)) {
6567
context = new ParseContext.InternalParseContext(indexSettings.getSettings(), docMapperParser, docMapper, source, parser);
6668
validateStart(parser);
6769
internalParseDocument(mapping, context, parser);

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.elasticsearch.common.unit.ByteSizeValue;
6969
import org.elasticsearch.common.util.iterable.Iterables;
7070
import org.elasticsearch.common.util.set.Sets;
71+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
7172
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
7273
import org.elasticsearch.common.xcontent.ToXContent;
7374
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -605,7 +606,8 @@ public RepositoryData getRepositoryData() {
605606
BytesStreamOutput out = new BytesStreamOutput();
606607
Streams.copy(blob, out);
607608
// EMPTY is safe here because RepositoryData#fromXContent calls namedObject
608-
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, out.bytes(), XContentType.JSON)) {
609+
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
610+
LoggingDeprecationHandler.INSTANCE, out.bytes(), XContentType.JSON)) {
609611
repositoryData = RepositoryData.snapshotsFromXContent(parser, indexGen);
610612
} catch (NotXContentException e) {
611613
logger.warn("[{}] index blob is not valid x-content [{} bytes]", snapshotsIndexBlobName, out.bytes().length());
@@ -617,7 +619,8 @@ public RepositoryData getRepositoryData() {
617619
try (InputStream blob = snapshotsBlobContainer.readBlob(INCOMPATIBLE_SNAPSHOTS_BLOB)) {
618620
BytesStreamOutput out = new BytesStreamOutput();
619621
Streams.copy(blob, out);
620-
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, out.bytes(), XContentType.JSON)) {
622+
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
623+
LoggingDeprecationHandler.INSTANCE, out.bytes(), XContentType.JSON)) {
621624
repositoryData = repositoryData.incompatibleSnapshotsFromXContent(parser);
622625
}
623626
} catch (NoSuchFileException e) {

server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.common.io.stream.StreamInput;
2626
import org.elasticsearch.common.io.stream.StreamOutput;
2727
import org.elasticsearch.common.unit.Fuzziness;
28+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2829
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2930
import org.elasticsearch.common.xcontent.ObjectParser;
3031
import org.elasticsearch.common.xcontent.ToXContent;
@@ -311,7 +312,8 @@ public SuggestionContext build(QueryShardContext context) throws IOException {
311312

312313
static Map<String, List<ContextMapping.InternalQueryContext>> parseContextBytes(BytesReference contextBytes,
313314
NamedXContentRegistry xContentRegistry, ContextMappings contextMappings) throws IOException {
314-
try (XContentParser contextParser = XContentHelper.createParser(xContentRegistry, contextBytes, CONTEXT_BYTES_XCONTENT_TYPE)) {
315+
try (XContentParser contextParser = XContentHelper.createParser(xContentRegistry,
316+
LoggingDeprecationHandler.INSTANCE, contextBytes, CONTEXT_BYTES_XCONTENT_TYPE)) {
315317
contextParser.nextToken();
316318
Map<String, List<ContextMapping.InternalQueryContext>> queryContexts = new HashMap<>(contextMappings.size());
317319
assert contextParser.currentToken() == XContentParser.Token.START_OBJECT;

0 commit comments

Comments
 (0)