Skip to content

Commit 87ab933

Browse files
authored
Remove calls to deprecated xcontent method (elastic#84733)
This removes many calls to the last remaining `createParser` method that I deprecated in elastic#79814, migrating callers to one of the new methods that it created.
1 parent 4607182 commit 87ab933

File tree

28 files changed

+75
-173
lines changed

28 files changed

+75
-173
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/search/fetch/subphase/FetchSourcePhaseBenchmark.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
99
import org.elasticsearch.search.fetch.subphase.FetchSourcePhase;
1010
import org.elasticsearch.search.lookup.SourceLookup;
11-
import org.elasticsearch.xcontent.DeprecationHandler;
12-
import org.elasticsearch.xcontent.NamedXContentRegistry;
1311
import org.elasticsearch.xcontent.XContentBuilder;
1412
import org.elasticsearch.xcontent.XContentParser;
1513
import org.elasticsearch.xcontent.XContentParserConfiguration;
@@ -108,8 +106,7 @@ public BytesReference filterXContentOnBuilder() throws IOException {
108106
XContentType.JSON.toParsedMediaType()
109107
);
110108
try (
111-
XContentParser parser = XContentType.JSON.xContent()
112-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, sourceBytes.streamInput())
109+
XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, sourceBytes.streamInput())
113110
) {
114111
builder.copyCurrentStructure(parser);
115112
return BytesReference.bytes(builder);

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
import org.elasticsearch.xcontent.NamedXContentRegistry;
178178
import org.elasticsearch.xcontent.ParseField;
179179
import org.elasticsearch.xcontent.XContentParser;
180+
import org.elasticsearch.xcontent.XContentParserConfiguration;
180181
import org.elasticsearch.xcontent.XContentType;
181182

182183
import java.io.Closeable;
@@ -244,7 +245,7 @@ public class RestHighLevelClient implements Closeable {
244245

245246
// To be called using performClientRequest and performClientRequestAsync to ensure version compatibility check
246247
private final RestClient client;
247-
private final NamedXContentRegistry registry;
248+
private final XContentParserConfiguration parserConfig;
248249
private final CheckedConsumer<RestClient, IOException> doClose;
249250
private final boolean useAPICompatibility;
250251

@@ -297,11 +298,19 @@ protected RestHighLevelClient(
297298
) {
298299
this.client = Objects.requireNonNull(restClient, "restClient must not be null");
299300
this.doClose = Objects.requireNonNull(doClose, "doClose consumer must not be null");
300-
this.registry = new NamedXContentRegistry(
301+
NamedXContentRegistry registry = new NamedXContentRegistry(
301302
Stream.of(getDefaultNamedXContents().stream(), getProvidedNamedXContents().stream(), namedXContentEntries.stream())
302303
.flatMap(Function.identity())
303304
.collect(toList())
304305
);
306+
/*
307+
* Ignores deprecation warnings. This is appropriate because it is only
308+
* used to parse responses from Elasticsearch. Any deprecation warnings
309+
* emitted there just mean that you are talking to an old version of
310+
* Elasticsearch. There isn't anything you can do about the deprecation.
311+
*/
312+
this.parserConfig = XContentParserConfiguration.EMPTY.withRegistry(registry)
313+
.withDeprecationHandler(DeprecationHandler.IGNORE_DEPRECATIONS);
305314
if (useAPICompatibility == null && "true".equals(System.getenv(API_VERSIONING_ENV_VARIABLE))) {
306315
this.useAPICompatibility = true;
307316
} else {
@@ -1165,7 +1174,7 @@ protected final <Resp> Resp parseEntity(final HttpEntity entity, final CheckedFu
11651174
if (xContentType == null) {
11661175
throw new IllegalStateException("Unsupported Content-Type: " + entity.getContentType().getValue());
11671176
}
1168-
try (XContentParser parser = xContentType.xContent().createParser(registry, DEPRECATION_HANDLER, entity.getContent())) {
1177+
try (XContentParser parser = xContentType.xContent().createParser(parserConfig, entity.getContent())) {
11691178
return entityParser.apply(parser);
11701179
}
11711180
}
@@ -1506,14 +1515,6 @@ private Optional<String> getVersionValidation(Response response) throws IOExcept
15061515
return Optional.empty();
15071516
}
15081517

1509-
/**
1510-
* Ignores deprecation warnings. This is appropriate because it is only
1511-
* used to parse responses from Elasticsearch. Any deprecation warnings
1512-
* emitted there just mean that you are talking to an old version of
1513-
* Elasticsearch. There isn't anything you can do about the deprecation.
1514-
*/
1515-
private static final DeprecationHandler DEPRECATION_HANDLER = DeprecationHandler.IGNORE_DEPRECATIONS;
1516-
15171518
static List<NamedXContentRegistry.Entry> getDefaultNamedXContents() {
15181519
Map<String, ContextParser<Object, ? extends Aggregation>> map = new HashMap<>();
15191520
map.put(CardinalityAggregationBuilder.NAME, (p, c) -> ParsedCardinality.fromXContent(p, (String) c));

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentGenerator.java

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

2222
import org.elasticsearch.core.CheckedConsumer;
2323
import org.elasticsearch.core.Streams;
24-
import org.elasticsearch.xcontent.DeprecationHandler;
25-
import org.elasticsearch.xcontent.NamedXContentRegistry;
2624
import org.elasticsearch.xcontent.XContent;
2725
import org.elasticsearch.xcontent.XContentFactory;
2826
import org.elasticsearch.xcontent.XContentGenerationException;
2927
import org.elasticsearch.xcontent.XContentGenerator;
3028
import org.elasticsearch.xcontent.XContentParser;
29+
import org.elasticsearch.xcontent.XContentParserConfiguration;
3130
import org.elasticsearch.xcontent.XContentType;
3231
import org.elasticsearch.xcontent.provider.filtering.FilterPathBasedFilter;
3332

@@ -444,14 +443,7 @@ public void writeRawField(String name, InputStream content) throws IOException {
444443
@Override
445444
public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException {
446445
if (mayWriteRawData(contentType) == false) {
447-
// EMPTY is safe here because we never call namedObject when writing raw data
448-
try (
449-
XContentParser parser = XContentFactory.xContent(contentType)
450-
// It's okay to pass the throwing deprecation handler
451-
// because we should not be writing raw fields when
452-
// generating JSON
453-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content)
454-
) {
446+
try (XContentParser parser = XContentFactory.xContent(contentType).createParser(XContentParserConfiguration.EMPTY, content)) {
455447
parser.nextToken();
456448
writeFieldName(name);
457449
copyCurrentStructure(parser);
@@ -493,13 +485,7 @@ protected boolean supportsRawWrites() {
493485
}
494486

495487
protected void copyRawValue(InputStream stream, XContent xContent) throws IOException {
496-
// EMPTY is safe here because we never call namedObject
497-
try (
498-
XContentParser parser = xContent
499-
// It's okay to pass the throwing deprecation handler because we
500-
// should not be writing raw fields when generating JSON
501-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)
502-
) {
488+
try (XContentParser parser = xContent.createParser(XContentParserConfiguration.EMPTY, stream)) {
503489
copyCurrentStructure(parser);
504490
}
505491
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
import org.elasticsearch.ingest.ConfigurationUtils;
1515
import org.elasticsearch.ingest.IngestDocument;
1616
import org.elasticsearch.ingest.Processor;
17-
import org.elasticsearch.xcontent.DeprecationHandler;
18-
import org.elasticsearch.xcontent.NamedXContentRegistry;
1917
import org.elasticsearch.xcontent.XContentParser;
18+
import org.elasticsearch.xcontent.XContentParserConfiguration;
2019
import org.elasticsearch.xcontent.json.JsonXContent;
2120

2221
import java.io.IOException;
@@ -77,11 +76,7 @@ public static Object apply(Object fieldValue, boolean allowDuplicateKeys) {
7776
BytesReference bytesRef = fieldValue == null ? new BytesArray("null") : new BytesArray(fieldValue.toString());
7877
try (
7978
InputStream stream = bytesRef.streamInput();
80-
XContentParser parser = JsonXContent.jsonXContent.createParser(
81-
NamedXContentRegistry.EMPTY,
82-
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
83-
stream
84-
)
79+
XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, stream)
8580
) {
8681
parser.allowDuplicateKeys(allowDuplicateKeys);
8782
XContentParser.Token token = parser.nextToken();

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
@@ -19,9 +19,9 @@
1919
import org.elasticsearch.script.ScriptException;
2020
import org.elasticsearch.script.ScriptService;
2121
import org.elasticsearch.script.ScriptType;
22-
import org.elasticsearch.xcontent.NamedXContentRegistry;
2322
import org.elasticsearch.xcontent.XContentBuilder;
2423
import org.elasticsearch.xcontent.XContentParser;
24+
import org.elasticsearch.xcontent.XContentParserConfiguration;
2525
import org.elasticsearch.xcontent.XContentType;
2626
import org.elasticsearch.xcontent.json.JsonXContent;
2727

@@ -110,7 +110,7 @@ public ScriptProcessor create(
110110
XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent).map(config);
111111
InputStream stream = BytesReference.bytes(builder).streamInput();
112112
XContentParser parser = XContentType.JSON.xContent()
113-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)
113+
.createParser(XContentParserConfiguration.EMPTY.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE), stream)
114114
) {
115115
Script script = Script.parse(parser);
116116

modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/DeviceTypeParser.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
package org.elasticsearch.ingest.useragent;
1010

1111
import org.elasticsearch.ElasticsearchParseException;
12-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
13-
import org.elasticsearch.xcontent.NamedXContentRegistry;
12+
import org.elasticsearch.ingest.useragent.UserAgentParser.VersionedName;
1413
import org.elasticsearch.xcontent.XContentFactory;
1514
import org.elasticsearch.xcontent.XContentParser;
15+
import org.elasticsearch.xcontent.XContentParserConfiguration;
1616
import org.elasticsearch.xcontent.XContentType;
1717

1818
import java.io.IOException;
@@ -24,7 +24,6 @@
2424
import java.util.regex.Matcher;
2525
import java.util.regex.Pattern;
2626

27-
import static org.elasticsearch.ingest.useragent.UserAgentParser.VersionedName;
2827
import static org.elasticsearch.ingest.useragent.UserAgentParser.readParserConfigurations;
2928

3029
public class DeviceTypeParser {
@@ -40,9 +39,8 @@ public class DeviceTypeParser {
4039
private final HashMap<String, ArrayList<DeviceTypeSubPattern>> deviceTypePatterns = new HashMap<>();
4140

4241
public void init(InputStream regexStream) throws IOException {
43-
// EMPTY is safe here because we don't use namedObject
4442
XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML)
45-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, regexStream);
43+
.createParser(XContentParserConfiguration.EMPTY, regexStream);
4644

4745
XContentParser.Token token = yamlParser.nextToken();
4846

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
package org.elasticsearch.ingest.useragent;
1010

1111
import org.elasticsearch.ElasticsearchParseException;
12-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
13-
import org.elasticsearch.xcontent.NamedXContentRegistry;
1412
import org.elasticsearch.xcontent.XContentFactory;
1513
import org.elasticsearch.xcontent.XContentParser;
14+
import org.elasticsearch.xcontent.XContentParserConfiguration;
1615
import org.elasticsearch.xcontent.XContentType;
1716

1817
import java.io.IOException;
@@ -50,7 +49,7 @@ final class UserAgentParser {
5049
private void init(InputStream regexStream) throws IOException {
5150
// EMPTY is safe here because we don't use namedObject
5251
XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML)
53-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, regexStream);
52+
.createParser(XContentParserConfiguration.EMPTY, regexStream);
5453

5554
XContentParser.Token token = yamlParser.nextToken();
5655

modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/DeviceTypeParserTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
package org.elasticsearch.ingest.useragent;
1010

11-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
11+
import org.elasticsearch.ingest.useragent.UserAgentParser.VersionedName;
1212
import org.elasticsearch.test.ESTestCase;
13-
import org.elasticsearch.xcontent.NamedXContentRegistry;
1413
import org.elasticsearch.xcontent.XContentFactory;
1514
import org.elasticsearch.xcontent.XContentParser;
15+
import org.elasticsearch.xcontent.XContentParserConfiguration;
1616
import org.elasticsearch.xcontent.XContentType;
1717
import org.junit.BeforeClass;
1818

@@ -23,7 +23,6 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26-
import static org.elasticsearch.ingest.useragent.UserAgentParser.VersionedName;
2726
import static org.elasticsearch.ingest.useragent.UserAgentParser.readParserConfigurations;
2827
import static org.hamcrest.Matchers.is;
2928

@@ -33,7 +32,7 @@ public class DeviceTypeParserTests extends ESTestCase {
3332

3433
private ArrayList<HashMap<String, String>> readTestDevices(InputStream regexStream, String keyName) throws IOException {
3534
XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML)
36-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, regexStream);
35+
.createParser(XContentParserConfiguration.EMPTY, regexStream);
3736

3837
XContentParser.Token token = yamlParser.nextToken();
3938

modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextGeneratorCommon.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.painless.action.PainlessContextInstanceBindingInfo;
1616
import org.elasticsearch.painless.action.PainlessContextMethodInfo;
1717
import org.elasticsearch.xcontent.XContentParser;
18+
import org.elasticsearch.xcontent.XContentParserConfiguration;
1819
import org.elasticsearch.xcontent.json.JsonXContent;
1920

2021
import java.io.IOException;
@@ -36,7 +37,7 @@ public class ContextGeneratorCommon {
3637
public static List<PainlessContextInfo> getContextInfos() throws IOException {
3738
URLConnection getContextNames = new URL("http://" + System.getProperty("cluster.uri") + "/_scripts/painless/_context")
3839
.openConnection();
39-
XContentParser parser = JsonXContent.jsonXContent.createParser(null, null, getContextNames.getInputStream());
40+
XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, getContextNames.getInputStream());
4041
parser.nextToken();
4142
parser.nextToken();
4243
@SuppressWarnings("unchecked")
@@ -50,7 +51,7 @@ public static List<PainlessContextInfo> getContextInfos() throws IOException {
5051
URLConnection getContextInfo = new URL(
5152
"http://" + System.getProperty("cluster.uri") + "/_scripts/painless/_context?context=" + contextName
5253
).openConnection();
53-
parser = JsonXContent.jsonXContent.createParser(null, null, getContextInfo.getInputStream());
54+
parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, getContextInfo.getInputStream());
5455
contextInfos.add(PainlessContextInfo.fromXContent(parser));
5556
((HttpURLConnection) getContextInfo).disconnect();
5657
}

modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
import org.elasticsearch.common.bytes.BytesReference;
1111
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
12-
import org.elasticsearch.common.io.stream.StreamInput;
1312
import org.elasticsearch.common.io.stream.Writeable;
1413
import org.elasticsearch.common.settings.Settings;
15-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
1614
import org.elasticsearch.index.query.MatchAllQueryBuilder;
1715
import org.elasticsearch.index.query.QueryBuilder;
1816
import org.elasticsearch.painless.action.PainlessExecuteAction.Request.ContextSetup;
@@ -51,8 +49,7 @@ public final void testFromXContent() throws Exception {
5149

5250
try (XContentBuilder builder = XContentBuilder.builder(xContent)) {
5351
builder.value(testInstance);
54-
StreamInput instanceInput = BytesReference.bytes(builder).streamInput();
55-
try (XContentParser parser = xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, instanceInput)) {
52+
try (XContentParser parser = createParser(xContent, BytesReference.bytes(builder).streamInput())) {
5653
PainlessExecuteAction.Request result = PainlessExecuteAction.Request.parse(parser);
5754
assertThat(result, equalTo(testInstance));
5855
}

modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@
5454
import org.elasticsearch.script.Script;
5555
import org.elasticsearch.script.ScriptService;
5656
import org.elasticsearch.threadpool.ThreadPool;
57-
import org.elasticsearch.xcontent.DeprecationHandler;
58-
import org.elasticsearch.xcontent.NamedXContentRegistry;
5957
import org.elasticsearch.xcontent.XContentBuilder;
6058
import org.elasticsearch.xcontent.XContentParser;
59+
import org.elasticsearch.xcontent.XContentParserConfiguration;
6160
import org.elasticsearch.xcontent.XContentType;
6261

6362
import java.io.IOException;
@@ -323,8 +322,7 @@ protected RequestWrapper<IndexRequest> buildRequest(ScrollableHitSource.Hit doc)
323322
// we need to convert
324323
try (
325324
InputStream stream = doc.getSource().streamInput();
326-
XContentParser parser = sourceXContentType.xContent()
327-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream);
325+
XContentParser parser = sourceXContentType.xContent().createParser(XContentParserConfiguration.EMPTY, stream);
328326
XContentBuilder builder = XContentBuilder.builder(mainRequestXContentType.xContent())
329327
) {
330328
parser.nextToken();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
import org.elasticsearch.index.reindex.ScrollableHitSource;
3434
import org.elasticsearch.rest.RestStatus;
3535
import org.elasticsearch.threadpool.ThreadPool;
36-
import org.elasticsearch.xcontent.NamedXContentRegistry;
3736
import org.elasticsearch.xcontent.XContentParseException;
3837
import org.elasticsearch.xcontent.XContentParser;
38+
import org.elasticsearch.xcontent.XContentParserConfiguration;
3939
import org.elasticsearch.xcontent.XContentType;
4040

4141
import java.io.IOException;
@@ -192,7 +192,10 @@ public void onSuccess(org.elasticsearch.client.Response response) {
192192
// EMPTY is safe here because we don't call namedObject
193193
try (
194194
XContentParser xContentParser = xContentType.xContent()
195-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, content)
195+
.createParser(
196+
XContentParserConfiguration.EMPTY.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE),
197+
content
198+
)
196199
) {
197200
parsedResponse = parser.apply(xContentParser, xContentType);
198201
} catch (XContentParseException e) {

qa/ccs-rolling-upgrade-remote-cluster/src/javaRestTest/java/org/elasticsearch/upgrades/SearchStatesIT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
4444
import org.elasticsearch.test.rest.ESRestTestCase;
4545
import org.elasticsearch.test.rest.ObjectPath;
46-
import org.elasticsearch.xcontent.DeprecationHandler;
47-
import org.elasticsearch.xcontent.NamedXContentRegistry;
46+
import org.elasticsearch.test.rest.yaml.ObjectPath;
4847
import org.elasticsearch.xcontent.XContentParser;
48+
import org.elasticsearch.xcontent.XContentParserConfiguration;
4949
import org.elasticsearch.xcontent.json.JsonXContent;
5050

5151
import java.io.IOException;
@@ -189,8 +189,7 @@ void verifySearch(String localIndex, int localNumDocs, String remoteIndex, int r
189189
Response response = localClient.performRequest(request);
190190
try (
191191
XContentParser parser = JsonXContent.jsonXContent.createParser(
192-
NamedXContentRegistry.EMPTY,
193-
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
192+
XContentParserConfiguration.EMPTY,
194193
response.getEntity().getContent()
195194
)
196195
) {

0 commit comments

Comments
 (0)