Skip to content

Commit edfe2c5

Browse files
authored
Convert MalformedDynamicTemplateIT to unit test (#119647)
Most of MalformedDynamicTemplateIT deals with ignoring unknown parameters in indices created previous to version 8. While we still need to check that we can parse those "bad" mappings, we no longer need to check that we can index into them since v7 indices are read-only. This change moves the check for leniency and the check for errors post-v8 to the DynamicTemplatesTests unit test.
1 parent 5e29730 commit edfe2c5

File tree

2 files changed

+38
-78
lines changed

2 files changed

+38
-78
lines changed

server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import org.elasticsearch.common.settings.Settings;
2222
import org.elasticsearch.common.xcontent.XContentHelper;
2323
import org.elasticsearch.index.IndexVersion;
24+
import org.elasticsearch.index.IndexVersions;
2425
import org.elasticsearch.plugins.internal.XContentMeteringParserDecorator;
2526
import org.elasticsearch.test.XContentTestUtils;
27+
import org.elasticsearch.test.index.IndexVersionUtils;
2628
import org.elasticsearch.xcontent.XContentBuilder;
2729
import org.elasticsearch.xcontent.XContentFactory;
2830
import org.elasticsearch.xcontent.XContentParser;
@@ -1376,6 +1378,42 @@ public void testSubobjectsFalseWithInnerNestedFromDynamicTemplate() {
13761378
);
13771379
}
13781380

1381+
/**
1382+
* test that bad mapping is accepted for indices created before 8.0.0.
1383+
* We still need to test this in v9 because of N-2 read compatibility
1384+
*/
1385+
public void testMalformedDynamicMapping_v7() throws IOException {
1386+
String mapping = """
1387+
{
1388+
"_doc": {
1389+
"dynamic_templates": [
1390+
{
1391+
"my_template": {
1392+
"mapping": {
1393+
"ignore_malformed": true,
1394+
"type": "keyword"
1395+
},
1396+
"path_match": "*"
1397+
}
1398+
}
1399+
]
1400+
}
1401+
}
1402+
""";
1403+
MapperParsingException ex = expectThrows(MapperParsingException.class, () -> createMapperService(mapping));
1404+
assertThat(ex.getMessage(), containsString("dynamic template [my_template] has invalid content"));
1405+
1406+
// the previous exception should be ignored by indices with v7 index versions
1407+
IndexVersion indexVersion = IndexVersionUtils.randomPreviousCompatibleVersion(random(), IndexVersions.V_8_0_0);
1408+
Settings settings = Settings.builder().put("number_of_shards", 1).put("index.version.created", indexVersion).build();
1409+
MapperService mapperService = createMapperService(indexVersion, settings, () -> randomBoolean());
1410+
merge(mapperService, mapping);
1411+
assertWarnings(
1412+
"Parameter [ignore_malformed] is used in a dynamic template mapping and has no effect on type [keyword]. "
1413+
+ "Usage will result in an error in future major versions and should be removed."
1414+
);
1415+
}
1416+
13791417
public void testSubobjectsAutoFlatPaths() throws IOException {
13801418
assumeTrue("only test when feature flag for subobjects auto is enabled", ObjectMapper.SUB_OBJECTS_AUTO_FEATURE_FLAG.isEnabled());
13811419
MapperService mapperService = createDynamicTemplateAutoSubobjects();

0 commit comments

Comments
 (0)