Skip to content

Commit 4786ef0

Browse files
committed
adding non existent field verification to copy to
1 parent 109abdc commit 4786ef0

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ public final void validate(MappingLookup mappers) {
341341
if (mappers.isObjectField(copyTo)) {
342342
throw new IllegalArgumentException("Cannot copy to field [" + copyTo + "] since it is mapped as an object");
343343
}
344+
if (mappers.getMapper(copyTo) == null){
345+
throw new IllegalArgumentException("Cannot copy to field [" + copyTo + "] since it is a non existent field");
346+
}
344347

345348
final String targetScope = mappers.nestedLookup().getNestedParent(copyTo);
346349
checkNestedScopeCompatibility(sourceScope, targetScope);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,18 @@ 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");
651+
{
652+
b.field("type", "text");
653+
b.array("copy_to", "missing_field");
654+
}
655+
b.endObject();
656+
})));
657+
assertThat(e.getMessage(), Matchers.containsString("Cannot copy to field [missing_field] since it is a non existent field"));
658+
}
659+
648660
public void testCopyToDateRangeFailure() throws Exception {
649661
DocumentMapper docMapper = createDocumentMapper(topMapping(b -> {
650662
b.startObject("properties");

0 commit comments

Comments
 (0)