Skip to content

Commit

Permalink
Move more XContent.createParser calls to non-deprecated version
Browse files Browse the repository at this point in the history
This moves more of the callers to pass in the DeprecationHandler.

Relates to elastic#28504
  • Loading branch information
dakrone committed Feb 13, 2018
1 parent 7c201a6 commit cb43bb3
Show file tree
Hide file tree
Showing 24 changed files with 84 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -108,7 +109,8 @@ static SearchRequest convert(SearchTemplateRequest searchTemplateRequest, Search
return null;
}

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xContentRegistry, source)) {
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, source)) {
SearchSourceBuilder builder = SearchSourceBuilder.searchSource();
builder.parseXContent(parser);
builder.explain(searchTemplateRequest.isExplain());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -731,8 +732,9 @@ static PercolateQuery.QueryStore createStore(MappedFieldType queryBuilderFieldTy
BytesRef qbSource = binaryDocValues.binaryValue();
if (qbSource.length > 0) {
XContent xContent = PercolatorFieldMapper.QUERY_BUILDER_CONTENT_TYPE.xContent();
try (XContentParser sourceParser = xContent.createParser(context.getXContentRegistry(), qbSource.bytes,
qbSource.offset, qbSource.length)) {
try (XContentParser sourceParser = xContent
.createParser(context.getXContentRegistry(), LoggingDeprecationHandler.INSTANCE,
qbSource.bytes, qbSource.offset, qbSource.length)) {
return parseQuery(context, mapUnmappedFieldsAsString, sourceParser);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.Build;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -90,6 +91,7 @@ public void testTransportClient() throws URISyntaxException, IOException {
XContentParser parser =
JsonXContent.jsonXContent.createParser(
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()),
DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
response.getEntity().getContent())) {
final Map<String, Object> map = parser.map();
assertThat(map.get("first_name"), equalTo("John"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
Expand Down Expand Up @@ -120,7 +121,8 @@ void validateAliasStandalone(String alias, String indexRouting) {
public void validateAliasFilter(String alias, String filter, QueryShardContext queryShardContext,
NamedXContentRegistry xContentRegistry) {
assert queryShardContext != null;
try (XContentParser parser = XContentFactory.xContent(filter).createParser(xContentRegistry, filter)) {
try (XContentParser parser = XContentFactory.xContent(filter)
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, filter)) {
validateAliasFilter(parser, queryShardContext);
} catch (Exception e) {
throw new IllegalArgumentException("failed to parse filter for alias [" + alias + "]", e);
Expand All @@ -135,7 +137,8 @@ public void validateAliasFilter(String alias, String filter, QueryShardContext q
public void validateAliasFilter(String alias, byte[] filter, QueryShardContext queryShardContext,
NamedXContentRegistry xContentRegistry) {
assert queryShardContext != null;
try (XContentParser parser = XContentFactory.xContent(filter).createParser(xContentRegistry, filter)) {
try (XContentParser parser = XContentFactory.xContent(filter)
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, filter)) {
validateAliasFilter(parser, queryShardContext);
} catch (Exception e) {
throw new IllegalArgumentException("failed to parse filter for alias [" + alias + "]", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.elasticsearch.common.unit.SizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -1116,7 +1117,8 @@ private void processLegacyLists(Map<String, Object> 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, source)) {
try (XContentParser parser = XContentFactory.xContent(xContentType)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)) {
this.put(fromXContent(parser, true, true));
} catch (Exception e) {
throw new SettingsException("Failed to load settings from [" + source + "]", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void register(T value, ParseField parseField) {
* @return The value being looked up. Never null.
* @throws ParsingException if the named thing isn't in the registry or the name was deprecated and deprecated names aren't supported.
*/
public T lookup(String name, XContentLocation xContentLocation) {
T value = lookupReturningNullIfNotFound(name);
public T lookup(String name, XContentLocation xContentLocation, DeprecationHandler deprecationHandler) {
T value = lookupReturningNullIfNotFound(name, deprecationHandler);
if (value == null) {
throw new ParsingException(xContentLocation, "no [" + registryName + "] registered for [" + name + "]");
}
Expand All @@ -92,14 +92,14 @@ public T lookup(String name, XContentLocation xContentLocation) {
* @return The value being looked up or null if it wasn't found.
* @throws ParsingException if the named thing isn't in the registry or the name was deprecated and deprecated names aren't supported.
*/
public T lookupReturningNullIfNotFound(String name) {
public T lookupReturningNullIfNotFound(String name, DeprecationHandler deprecationHandler) {
Tuple<ParseField, T> parseFieldAndValue = registry.get(name);
if (parseFieldAndValue == null) {
return null;
}
ParseField parseField = parseFieldAndValue.v1();
T value = parseFieldAndValue.v2();
boolean match = parseField.match(name, LoggingDeprecationHandler.INSTANCE);
boolean match = parseField.match(name, deprecationHandler);
//this is always expected to match, ParseField is useful for deprecation warnings etc. here
assert match : "ParseField did not match registered name [" + name + "][" + registryName + "]";
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,6 @@ default XContentParser createParser(NamedXContentRegistry xContentRegistry, byte
return createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, data);
}

/**
* Creates a parser over the provided bytes using
* {@link LoggingDeprecationHandler}.
* @deprecated This is a temporary shim so we can migrate all calls to createParser incrementally.
* Use {@link #createParser(NamedXContentRegistry, DeprecationHandler, byte[], int, int)} instead.
*/
@Deprecated
default XContentParser createParser(NamedXContentRegistry xContentRegistry, byte[] data, int offset, int length) throws IOException {
return createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, data, offset, length);
}

/**
* Creates a parser over the provided bytes using
* {@link LoggingDeprecationHandler}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static XContentParser createParser(NamedXContentRegistry xContentRegistry
}
return XContentFactory.xContent(xContentType).createParser(xContentRegistry, deprecationHandler, compressedInput);
} else {
return xContentType.xContent().createParser(xContentRegistry, bytes.streamInput());
return xContentType.xContent().createParser(xContentRegistry, deprecationHandler, bytes.streamInput());
}
}

Expand Down Expand Up @@ -117,7 +117,8 @@ public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReferen
*/
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, string)) {
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, string)) {
return ordered ? parser.mapOrdered() : parser.map();
} catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse content to map", e);
Expand All @@ -131,7 +132,8 @@ public static Map<String, Object> convertToMap(XContent xContent, String string,
public static Map<String, Object> convertToMap(XContent xContent, InputStream input, boolean ordered)
throws ElasticsearchParseException {
// It is safe to use EMPTY here because this never uses namedObject
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, input)) {
try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, input)) {
return ordered ? parser.mapOrdered() : parser.map();
} catch (IOException e) {
throw new ElasticsearchParseException("Failed to parse content to map", e);
Expand Down Expand Up @@ -161,7 +163,7 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, b

// It is safe to use EMPTY here because this never uses namedObject
try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, bytes.streamInput())) {
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
parser.nextToken();
XContentBuilder builder = XContentFactory.jsonBuilder();
if (prettyPrint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentFactory;
Expand Down Expand Up @@ -314,7 +315,8 @@ public void writeRawField(String name, InputStream content) throws IOException {
public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException {
if (mayWriteRawData(contentType) == false) {
// EMPTY is safe here because we never call namedObject when writing raw data
try (XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, content)) {
try (XContentParser parser = XContentFactory.xContent(contentType)
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, content)) {
parser.nextToken();
writeFieldName(name);
copyCurrentStructure(parser);
Expand Down Expand Up @@ -392,7 +394,8 @@ protected boolean supportsRawWrites() {
protected void copyRawValue(BytesReference content, XContent xContent) throws IOException {
// EMPTY is safe here because we never call namedObject
try (StreamInput input = content.streamInput();
XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, input)) {
XContentParser parser = xContent
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, input)) {
copyCurrentStructure(parser);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.lucene.store.IndexOutputOutputStream;
import org.elasticsearch.common.lucene.store.InputStreamIndexInput;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
Expand Down Expand Up @@ -192,8 +193,9 @@ public final T read(NamedXContentRegistry namedXContentRegistry, Path file) thro
long filePointer = indexInput.getFilePointer();
long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
try (IndexInput slice = indexInput.slice("state_xcontent", filePointer, contentSize)) {
try (XContentParser parser = XContentFactory.xContent(FORMAT).createParser(namedXContentRegistry,
new InputStreamIndexInput(slice, contentSize))) {
try (XContentParser parser = XContentFactory.xContent(FORMAT)
.createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE,
new InputStreamIndexInput(slice, contentSize))) {
return fromXContent(parser);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -162,7 +163,8 @@ private static String getRemainingFields(Map<?, ?> map) {

private Tuple<String, Map<String, Object>> extractMapping(String type, String source) throws MapperParsingException {
Map<String, Object> root;
try (XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, source)) {
try (XContentParser parser = XContentType.JSON.xContent()
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, source)) {
root = parser.mapOrdered();
} catch (Exception e) {
throw new MapperParsingException("failed to parse mapping definition", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -200,7 +201,8 @@ public DocumentMapperParser documentMapperParser() {
* Parses the mappings (formatted as JSON) into a map
*/
public static Map<String, Object> parseMapping(NamedXContentRegistry xContentRegistry, String mappingSource) throws Exception {
try (XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, mappingSource)) {
try (XContentParser parser = XContentType.JSON.xContent()
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, mappingSource)) {
return parser.map();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -158,7 +159,8 @@ protected boolean doEquals(WrapperQueryBuilder other) {

@Override
protected QueryBuilder doRewrite(QueryRewriteContext context) throws IOException {
try (XContentParser qSourceParser = XContentFactory.xContent(source).createParser(context.getXContentRegistry(), source)) {
try (XContentParser qSourceParser = XContentFactory.xContent(source)
.createParser(context.getXContentRegistry(), LoggingDeprecationHandler.INSTANCE, source)) {

final QueryBuilder queryBuilder = parseInnerQueryBuilder(qSourceParser).rewrite(context);
if (boost() != DEFAULT_BOOST || queryName() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.common.util.iterable.Iterables;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -1236,7 +1237,8 @@ public AliasFilter buildAliasFilter(ClusterState state, String index, String...
/* Being static, parseAliasFilter doesn't have access to whatever guts it needs to parse a query. Instead of passing in a bunch
* of dependencies we pass in a function that can perform the parsing. */
CheckedFunction<byte[], QueryBuilder, IOException> filterParser = bytes -> {
try (XContentParser parser = XContentFactory.xContent(bytes).createParser(xContentRegistry, bytes)) {
try (XContentParser parser = XContentFactory.xContent(bytes)
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, bytes)) {
return parseInnerQueryBuilder(parser);
}
};
Expand Down

0 comments on commit cb43bb3

Please sign in to comment.