Skip to content

Commit

Permalink
Remove deprecated xcontent method (#84314)
Browse files Browse the repository at this point in the history
This removes one of the `createParser` methods that I deprecated in
 #79814, migrating callers to one of the new methods that it created.
  • Loading branch information
nik9000 committed Feb 24, 2022
1 parent b42f0d4 commit 6c70578
Show file tree
Hide file tree
Showing 63 changed files with 226 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.ToXContentFragment;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;

Expand Down Expand Up @@ -352,13 +351,7 @@ public PutIndexTemplateRequest aliases(String source) {
*/
public PutIndexTemplateRequest aliases(BytesReference source) {
// EMPTY is safe here because we never call namedObject
try (
XContentParser parser = XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
source
)
) {
try (XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source)) {
// move to the first alias
parser.nextToken();
while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) {
Expand Down Expand Up @@ -413,11 +406,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (mappings != null) {
builder.field("mappings");
try (
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
mappings.utf8ToString()
)
XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, mappings.utf8ToString())
) {
builder.copyCurrentStructure(parser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ default XContentGenerator createGenerator(OutputStream os) throws IOException {
*/
XContentParser createParser(XContentParserConfiguration config, String content) throws IOException;

/**
* Creates a parser over the provided string content.
* @deprecated Use {@link #createParser(XContentParserConfiguration, InputStream)}
*/
@Deprecated
default XContentParser createParser(NamedXContentRegistry registry, DeprecationHandler deprecationHandler, String content)
throws IOException {
return createParser(XContentParserConfiguration.EMPTY.withRegistry(registry).withDeprecationHandler(deprecationHandler), content);
}

/**
* Creates a parser over the provided input stream.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
Expand Down Expand Up @@ -98,10 +99,9 @@ static SearchRequest convert(
return null;
}

try (
XContentParser parser = XContentFactory.xContent(XContentType.JSON)
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, source)
) {
XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry)
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE);
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(parserConfig, source)) {
SearchSourceBuilder builder = SearchSourceBuilder.searchSource();
builder.parseXContent(parser, false);
builder.explain(searchTemplateRequest.isExplain());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

package org.elasticsearch.painless.api;

import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.json.JsonXContent;

import java.io.IOException;
Expand All @@ -21,11 +20,7 @@ public class Json {
* Load a string as the Java version of a JSON type, either List (JSON array), Map (JSON object), Number, Boolean or String
*/
public static Object load(String json) throws IOException {
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
json
);
XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, json);

return switch (parser.nextToken()) {
case START_ARRAY -> parser.list();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -115,7 +116,11 @@ public static void validateAliasFilter(
assert searchExecutionContext != null;
try (
XContentParser parser = XContentFactory.xContent(filter)
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, filter)
.createParser(
XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry)
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE),
filter
)
) {
validateAliasFilter(parser, searchExecutionContext);
} catch (Exception e) {
Expand All @@ -140,7 +145,11 @@ public static void validateAliasFilter(
InputStream inputStream = filter.streamInput();
XContentParser parser = XContentFactory.xContentType(inputStream)
.xContent()
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, filter.streamInput())
.createParser(
XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry)
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE),
filter.streamInput()
)
) {
validateAliasFilter(parser, searchExecutionContext);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
Expand Down Expand Up @@ -1740,10 +1739,7 @@ private static List<String> parseableStringToList(String parsableString) {
return List.of();
}
// fromXContent doesn't use named xcontent or deprecation.
try (
XContentParser xContentParser = XContentType.JSON.xContent()
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, parsableString)
) {
try (XContentParser xContentParser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, parsableString)) {
xContentParser.nextToken();
return XContentParserUtils.parseList(xContentParser, p -> {
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_STRING, p.currentToken(), p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@
import org.elasticsearch.common.unit.MemorySizeValue;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.common.util.StringLiteralDeduplicator;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.ToXContentFragment;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
Expand Down Expand Up @@ -1139,10 +1137,7 @@ public Builder loadFromMap(Map<String, ?> map) {
* Loads settings from the actual string content that represents them using {@link #fromXContent(XContentParser)}
*/
public Builder loadFromSource(String source, XContentType xContentType) {
try (
XContentParser parser = XContentFactory.xContent(xContentType)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)
) {
try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(XContentParserConfiguration.EMPTY, source)) {
this.put(fromXContent(parser, true, true));
} catch (Exception e) {
throw new SettingsException("Failed to load settings from [" + source + "]", e);
Expand Down Expand Up @@ -1172,10 +1167,7 @@ public Builder loadFromStream(String resourceName, InputStream is, boolean accep
throw new IllegalArgumentException("unable to detect content type from resource name [" + resourceName + "]");
}
// fromXContent doesn't use named xcontent or deprecation.
try (
XContentParser parser = XContentFactory.xContent(xContentType)
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, is)
) {
try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(XContentParserConfiguration.EMPTY, is)) {
if (parser.currentToken() == null) {
if (parser.nextToken() == null) {
return this; // empty file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,7 @@ public static Tuple<XContentType, Map<String, Object>> convertToMap(
* error.
*/
public static Map<String, Object> convertToMap(XContent xContent, String string, boolean ordered) throws ElasticsearchParseException {
// It is safe to use EMPTY here because this never uses namedObject
try (
XContentParser parser = xContent.createParser(
NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
string
)
) {
try (XContentParser parser = xContent.createParser(XContentParserConfiguration.EMPTY, string)) {
return ordered ? parser.mapOrdered() : parser.map();
} catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse content to map", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ public static Map<String, Object> parseMapping(NamedXContentRegistry xContentReg
// empty JSON is a common default value so it makes sense to optimize for it a little
return Map.of();
}
try (
XContentParser parser = XContentType.JSON.xContent()
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, mappingSource)
) {
try (XContentParser parser = XContentType.JSON.xContent().createParser(parserConfig(xContentRegistry), mappingSource)) {
return parser.map();
}
}
Expand All @@ -241,17 +238,16 @@ public static Map<String, Object> parseMapping(NamedXContentRegistry xContentReg
throws IOException {
try (
InputStream in = CompressorFactory.COMPRESSOR.threadLocalInputStream(mappingSource.compressedReference().streamInput());
XContentParser parser = XContentType.JSON.xContent()
.createParser(
XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry)
.withDeprecationHandler(LoggingDeprecationHandler.INSTANCE),
in
)
XContentParser parser = XContentType.JSON.xContent().createParser(parserConfig(xContentRegistry), in)
) {
return parser.map();
}
}

private static XContentParserConfiguration parserConfig(NamedXContentRegistry xContentRegistry) {
return XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry).withDeprecationHandler(LoggingDeprecationHandler.INSTANCE);
}

/**
* Update local mapping by applying the incoming mapping that have already been merged with the current one on the master
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.index.RandomCreateIndexGenerator;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -161,29 +157,6 @@ public void testAlias() throws IOException {
assertThat(e.getMessage(), containsString("Unknown token [START_ARRAY] in alias [filtered-data]"));
}

public static void assertMappingsEqual(Map<String, String> expected, Map<String, String> actual) throws IOException {
assertEquals(expected.keySet(), actual.keySet());

for (Map.Entry<String, String> expectedEntry : expected.entrySet()) {
String expectedValue = expectedEntry.getValue();
String actualValue = actual.get(expectedEntry.getKey());
try (
XContentParser expectedJson = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
expectedValue
);
XContentParser actualJson = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
actualValue
)
) {
assertEquals(expectedJson.map(), actualJson.map());
}
}
}

public static void assertAliasesEqual(Set<Alias> expected, Set<Alias> actual) throws IOException {
assertEquals(expected, actual);

Expand Down

0 comments on commit 6c70578

Please sign in to comment.