-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bwc for parsing mappings from dynamic templates #67763
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.indices.mapping; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.index.mapper.MapperParsingException; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
import org.elasticsearch.test.VersionUtils; | ||
|
||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class MalformedDynamicTemplateIT extends ESIntegTestCase { | ||
|
||
@Override | ||
protected boolean forbidPrivateIndexSettings() { | ||
return false; | ||
} | ||
|
||
/** | ||
* Check that we can index a document into an 7.x index with a matching dynamic template that | ||
* contains unknown parameters. We were able to create those templates in 7.x still, so we need | ||
* to be able to index new documents into them. Indexing should issue a deprecation warning though. | ||
*/ | ||
public void testBWCMalformedDynamicTemplate() { | ||
String mapping = "{ \"dynamic_templates\": [\n" | ||
+ " {\n" | ||
+ " \"my_template\": {\n" | ||
+ " \"mapping\": {\n" | ||
+ " \"ignore_malformed\": true,\n" // this parameter is not supported by "keyword" field type | ||
+ " \"type\": \"keyword\"\n" | ||
+ " },\n" | ||
+ " \"path_match\": \"*\"\n" | ||
+ " }\n" | ||
+ " }\n" | ||
+ " ]\n" | ||
+ " }\n" | ||
+ "}}"; | ||
String indexName = "malformed_dynamic_template"; | ||
assertAcked(prepareCreate(indexName).setSettings(Settings.builder().put(indexSettings()) | ||
.put("number_of_shards", 1) | ||
.put("index.version.created", VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0)) | ||
).setMapping(mapping).get()); | ||
client().prepareIndex(indexName).setSource("{\"foo\" : \"bar\"}", XContentType.JSON).get(); | ||
assertNoFailures((client().admin().indices().prepareRefresh(indexName)).get()); | ||
assertHitCount(client().prepareSearch(indexName).get(), 1); | ||
|
||
MapperParsingException ex = expectThrows( | ||
MapperParsingException.class, | ||
() -> prepareCreate("malformed_dynamic_template_8.0").setSettings( | ||
Settings.builder().put(indexSettings()).put("number_of_shards", 1).put("index.version.created", Version.CURRENT) | ||
).setMapping(mapping).get() | ||
); | ||
assertThat(ex.getMessage(), containsString("dynamic template [my_template] has invalid content")); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,23 +19,6 @@ | |
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import org.apache.lucene.document.Field; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.Explicit; | ||
|
@@ -53,6 +36,23 @@ | |
import org.elasticsearch.index.mapper.FieldNamesFieldMapper.FieldNamesFieldType; | ||
import org.elasticsearch.index.mapper.Mapper.TypeParser.ParserContext; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public abstract class FieldMapper extends Mapper implements Cloneable { | ||
public static final Setting<Boolean> IGNORE_MALFORMED_SETTING = | ||
Setting.boolSetting("index.mapping.ignore_malformed", false, Property.IndexScope); | ||
|
@@ -990,8 +990,21 @@ public final void parse(String name, ParserContext parserContext, Map<String, Ob | |
iterator.remove(); | ||
continue; | ||
} | ||
throw new MapperParsingException("unknown parameter [" + propName | ||
+ "] on mapper [" + name + "] of type [" + type + "]"); | ||
if (parserContext.isFromDynamicTemplate() && parserContext.indexVersionCreated().before(Version.V_8_0_0)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we can have a dynamic template in an index >8.0.0 with a malformed parameter since we cannot add it, but I put the version check here regardless for safety. @romseygeek wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ |
||
// The parameter is unknown, but this mapping is from a dynamic template. | ||
// Until 7.x it was possible to use unknown parameters there, so for bwc we need to ignore it | ||
deprecationLogger.deprecate(DeprecationCategory.API, propName, | ||
"Parameter [{}] is used in a dynamic template mapping and has no effect on type [{}]. " | ||
+ "Usage will result in an error in future major versions and should be removed.", | ||
propName, | ||
type | ||
); | ||
iterator.remove(); | ||
continue; | ||
} | ||
throw new MapperParsingException( | ||
"unknown parameter [" + propName + "] on mapper [" + name + "] of type [" + type + "]" | ||
); | ||
} | ||
if (parameter.deprecated) { | ||
deprecationLogger.deprecate(DeprecationCategory.API, propName, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we assert that we get a deprecation warning from the index action?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking at how to do that in Integ tests but couldn't find any pointers. ESIntegTestCase#enableWarningsCheck makes me think its not possible here, but if you have pointers to tests doing this I'm happy to add it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can call
getWarnings()
on the responseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this IndexResponse is no
org.elasticsearch.client.Response
, will check if there are other ways...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's no way of doing it then let's get this merged anyway.