Skip to content

Commit

Permalink
Convert MalformedDynamicTemplateIT to unit test (elastic#119647)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbuescher authored Jan 7, 2025
1 parent 5e29730 commit edfe2c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 78 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.plugins.internal.XContentMeteringParserDecorator;
import org.elasticsearch.test.XContentTestUtils;
import org.elasticsearch.test.index.IndexVersionUtils;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentParser;
Expand Down Expand Up @@ -1376,6 +1378,42 @@ public void testSubobjectsFalseWithInnerNestedFromDynamicTemplate() {
);
}

/**
* test that bad mapping is accepted for indices created before 8.0.0.
* We still need to test this in v9 because of N-2 read compatibility
*/
public void testMalformedDynamicMapping_v7() throws IOException {
String mapping = """
{
"_doc": {
"dynamic_templates": [
{
"my_template": {
"mapping": {
"ignore_malformed": true,
"type": "keyword"
},
"path_match": "*"
}
}
]
}
}
""";
MapperParsingException ex = expectThrows(MapperParsingException.class, () -> createMapperService(mapping));
assertThat(ex.getMessage(), containsString("dynamic template [my_template] has invalid content"));

// the previous exception should be ignored by indices with v7 index versions
IndexVersion indexVersion = IndexVersionUtils.randomPreviousCompatibleVersion(random(), IndexVersions.V_8_0_0);
Settings settings = Settings.builder().put("number_of_shards", 1).put("index.version.created", indexVersion).build();
MapperService mapperService = createMapperService(indexVersion, settings, () -> randomBoolean());
merge(mapperService, mapping);
assertWarnings(
"Parameter [ignore_malformed] is used in a dynamic template mapping and has no effect on type [keyword]. "
+ "Usage will result in an error in future major versions and should be removed."
);
}

public void testSubobjectsAutoFlatPaths() throws IOException {
assumeTrue("only test when feature flag for subobjects auto is enabled", ObjectMapper.SUB_OBJECTS_AUTO_FEATURE_FLAG.isEnabled());
MapperService mapperService = createDynamicTemplateAutoSubobjects();
Expand Down

0 comments on commit edfe2c5

Please sign in to comment.