|
11 | 11 | import org.apache.lucene.index.IndexOptions;
|
12 | 12 | import org.apache.lucene.index.IndexableField;
|
13 | 13 | import org.apache.lucene.util.BytesRef;
|
| 14 | +import org.elasticsearch.Version; |
14 | 15 | import org.elasticsearch.index.mapper.ParseContext.Document;
|
| 16 | +import org.elasticsearch.test.VersionUtils; |
| 17 | + |
| 18 | +import java.io.IOException; |
15 | 19 |
|
16 | 20 | import static org.elasticsearch.test.StreamsUtils.copyToStringFromClasspath;
|
| 21 | +import static org.hamcrest.Matchers.containsString; |
17 | 22 | import static org.hamcrest.Matchers.equalTo;
|
18 | 23 | import static org.hamcrest.Matchers.notNullValue;
|
19 | 24 |
|
@@ -159,4 +164,78 @@ public void testSimpleWithXContentTraverse() throws Exception {
|
159 | 164 | fieldMapper = mapperService.documentMapper().mappers().getMapper("multi2.org");
|
160 | 165 | assertNotNull(fieldMapper);
|
161 | 166 | }
|
| 167 | + |
| 168 | + public void testDynamicMapperWithBadMapping() throws IOException { |
| 169 | + { |
| 170 | + // in 7.x versions this will issue a deprecation warning |
| 171 | + Version version = VersionUtils.randomCompatibleVersion(random(), Version.V_7_0_0); |
| 172 | + DocumentMapper mapper = createDocumentMapper(version, topMapping(b -> { |
| 173 | + b.startArray("dynamic_templates"); |
| 174 | + { |
| 175 | + b.startObject(); |
| 176 | + { |
| 177 | + b.startObject("test"); |
| 178 | + { |
| 179 | + b.field("match_mapping_type", "string"); |
| 180 | + b.startObject("mapping").field("badparam", false).endObject(); |
| 181 | + } |
| 182 | + b.endObject(); |
| 183 | + } |
| 184 | + b.endObject(); |
| 185 | + } |
| 186 | + b.endArray(); |
| 187 | + })); |
| 188 | + assertWarnings( |
| 189 | + "dynamic template [test] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{\"badparam\":false}}], " + |
| 190 | + "attempted to validate it with the following match_mapping_type: [string], last error: " + |
| 191 | + "[unknown parameter [badparam] on mapper [__dynamic__test] of type [null]]"); |
| 192 | + |
| 193 | + mapper.parse(source(b -> b.field("field", "foo"))); |
| 194 | + assertWarnings("Parameter [badparam] is used in a dynamic template mapping and has no effect on type [null]. " + |
| 195 | + "Usage will result in an error in future major versions and should be removed."); |
| 196 | + } |
| 197 | + |
| 198 | + { |
| 199 | + // in 8.x it will error out |
| 200 | + Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(topMapping(b -> { |
| 201 | + b.startArray("dynamic_templates"); |
| 202 | + { |
| 203 | + b.startObject(); |
| 204 | + { |
| 205 | + b.startObject("test"); |
| 206 | + { |
| 207 | + b.field("match_mapping_type", "string"); |
| 208 | + b.startObject("mapping").field("badparam", false).endObject(); |
| 209 | + } |
| 210 | + b.endObject(); |
| 211 | + } |
| 212 | + b.endObject(); |
| 213 | + } |
| 214 | + b.endArray(); |
| 215 | + }))); |
| 216 | + assertThat(e.getMessage(), containsString("dynamic template [test] has invalid content")); |
| 217 | + assertThat(e.getCause().getMessage(), containsString("badparam")); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + public void testDynamicRuntimeWithBadMapping() { |
| 222 | + Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(topMapping(b -> { |
| 223 | + b.startArray("dynamic_templates"); |
| 224 | + { |
| 225 | + b.startObject(); |
| 226 | + { |
| 227 | + b.startObject("test"); |
| 228 | + { |
| 229 | + b.field("match_mapping_type", "string"); |
| 230 | + b.startObject("runtime").field("badparam", false).endObject(); |
| 231 | + } |
| 232 | + b.endObject(); |
| 233 | + } |
| 234 | + b.endObject(); |
| 235 | + } |
| 236 | + b.endArray(); |
| 237 | + }))); |
| 238 | + assertThat(e.getMessage(), containsString("dynamic template [test] has invalid content")); |
| 239 | + assertThat(e.getCause().getMessage(), containsString("badparam")); |
| 240 | + } |
162 | 241 | }
|
0 commit comments