From edfe2c5c6dda5fecc09e76d8f6626c175fb3751b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Tue, 7 Jan 2025 16:34:19 +0100 Subject: [PATCH] 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. --- .../mapping/MalformedDynamicTemplateIT.java | 78 ------------------- .../index/mapper/DynamicTemplatesTests.java | 38 +++++++++ 2 files changed, 38 insertions(+), 78 deletions(-) delete mode 100644 server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java deleted file mode 100644 index 9fd657b809f27..0000000000000 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.indices.mapping; - -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.UpdateForV9; -import org.elasticsearch.index.IndexVersion; -import org.elasticsearch.index.IndexVersions; -import org.elasticsearch.index.mapper.MapperParsingException; -import org.elasticsearch.test.ESIntegTestCase; -import org.elasticsearch.test.index.IndexVersionUtils; -import org.elasticsearch.xcontent.XContentType; - -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. - */ - @UpdateForV9(owner = UpdateForV9.Owner.DATA_MANAGEMENT) - @AwaitsFix(bugUrl = "this is testing 7.x specific compatibility which may be n/a now after 9.0 bump") - public void testBWCMalformedDynamicTemplate() { - // this parameter is not supported by "keyword" field type - String mapping = """ - { "dynamic_templates": [ - { - "my_template": { - "mapping": { - "ignore_malformed": true, - "type": "keyword" - }, - "path_match": "*" - } - } - ] - } - }}"""; - String indexName = "malformed_dynamic_template"; - assertAcked( - prepareCreate(indexName).setSettings( - Settings.builder() - .put(indexSettings()) - .put("number_of_shards", 1) - .put("index.version.created", IndexVersionUtils.randomPreviousCompatibleVersion(random(), IndexVersions.V_8_0_0)) - ).setMapping(mapping) - ); - prepareIndex(indexName).setSource("{\"foo\" : \"bar\"}", XContentType.JSON).get(); - assertNoFailures((indicesAdmin().prepareRefresh(indexName)).get()); - assertHitCount(prepareSearch(indexName), 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", IndexVersion.current()) - ).setMapping(mapping) - ); - assertThat(ex.getMessage(), containsString("dynamic template [my_template] has invalid content")); - } - -} diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java index 7e9a196faaa26..c77b434168492 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java @@ -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; @@ -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();