Skip to content

Commit 1843499

Browse files
committed
adding verification for dynamic mapping
1 parent 4786ef0 commit 1843499

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,16 @@ public final void validate(MappingLookup mappers) {
334334
}
335335

336336
final String sourceScope = mappers.nestedLookup().getNestedParent(this.fullPath());
337+
ObjectMapper.Dynamic dynamic = mappers.getMapping().getRoot().dynamic;
338+
boolean needsFieldVerification = dynamic != null && dynamic.equals(ObjectMapper.Dynamic.FALSE);
337339
for (String copyTo : this.copyTo().copyToFields()) {
338340
if (mappers.isMultiField(copyTo)) {
339341
throw new IllegalArgumentException("[copy_to] may not be used to copy to a multi-field: [" + copyTo + "]");
340342
}
341343
if (mappers.isObjectField(copyTo)) {
342344
throw new IllegalArgumentException("Cannot copy to field [" + copyTo + "] since it is mapped as an object");
343345
}
344-
if (mappers.getMapper(copyTo) == null){
346+
if (needsFieldVerification && mappers.getMapper(copyTo) == null){
345347
throw new IllegalArgumentException("Cannot copy to field [" + copyTo + "] since it is a non existent field");
346348
}
347349

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,18 +645,38 @@ public void testCopyFromMultiField() {
645645
assertThat(e.getMessage(), Matchers.containsString("[copy_to] may not be used to copy from a multi-field: [field.bar]"));
646646
}
647647

648-
public void testCopyToNonExistentField() {
649-
Exception e = expectThrows(IllegalArgumentException.class, () -> createDocumentMapper(fieldMapping(b -> {
650-
b.startObject("test_field");
648+
public void testCopyToNonExistentFieldDynamicFalse() {
649+
Exception e = expectThrows(IllegalArgumentException.class, () -> createDocumentMapper(topMapping(b -> {
650+
b.field("dynamic", "false");
651+
b.startObject("properties");
651652
{
652-
b.field("type", "text");
653-
b.array("copy_to", "missing_field");
653+
b.startObject("test_field");
654+
{
655+
b.field("type", "text");
656+
b.field("copy_to", "missing_field");
657+
}
658+
b.endObject();
654659
}
655660
b.endObject();
656661
})));
657662
assertThat(e.getMessage(), Matchers.containsString("Cannot copy to field [missing_field] since it is a non existent field"));
658663
}
659664

665+
public void testCopyToNonExistentFieldDynamicTrue() throws IOException {
666+
DocumentMapper mapper = createDocumentMapper(topMapping(b -> {
667+
b.field("dynamic", "true");
668+
b.startObject("properties");
669+
{
670+
b.startObject("test_field");
671+
{
672+
b.field("type", "text");
673+
b.field("copy_to", "missing_field");
674+
}
675+
b.endObject();
676+
}
677+
b.endObject();}));
678+
}
679+
660680
public void testCopyToDateRangeFailure() throws Exception {
661681
DocumentMapper docMapper = createDocumentMapper(topMapping(b -> {
662682
b.startObject("properties");

0 commit comments

Comments
 (0)