Skip to content

Commit

Permalink
Improve error message in case of invalid dynamic templates
Browse files Browse the repository at this point in the history
Include the attempted 'match_mapping_type' into the message,
so that it is clearer that multiple validation attempts have occurred.

Dynamic template validation was recently added via elastic#51233 and
there was some confusion over the deprecation message itself.
(in 7.x only deprecation warning will be omitted and from 8.0
 an error will be returned)
  • Loading branch information
martijnvg committed Aug 10, 2020
1 parent bf80aa1 commit b3eb3c7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
15 changes: 8 additions & 7 deletions docs/reference/mapping/dynamic/templates.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ Dynamic templates are specified as an array of named objects:
<3> The mapping that the matched field should use.

If a provided mapping contains an invalid mapping snippet, a validation error
is returned. Validation occurs when applying the dynamic template at index time,
and, in most cases, when the dynamic template is updated. Providing an invalid mapping
is returned. Validation occurs when applying the dynamic template at index time,
and, in most cases, when the dynamic template is updated. Providing an invalid mapping
snippet may cause the update or validation of a dynamic template to fail under certain conditions:

* If no `match_mapping_type` has been specified but the template is valid for at least one predefined mapping type,
the mapping snippet is considered valid. However, a validation error is returned at index time if a field matching
the template is indexed as a different type. For example, configuring a dynamic template with no `match_mapping_type`
is considered valid as string type, but if a field matching the dynamic template is indexed as a long, a validation
error is returned at index time.
* If no `match_mapping_type` has been specified but the template is valid for at least one predefined mapping type,
the mapping snippet is considered valid. However, a validation error is returned at index time if a field matching
the template is indexed as a different type. For example, configuring a dynamic template with no `match_mapping_type`
is considered valid as string type, but if a field matching the dynamic template is indexed as a long, a validation
error is returned at index time. It is recommended to configure the `match_mapping_type` to the expected json type or
configure the desired `type` in the mapping snippet.

* If the `{name}` placeholder is used in the mapping snippet, validation is skipped when updating the dynamic
template. This is because the field name is unknown at that time. Instead, validation occurs when the template is applied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -408,14 +409,15 @@ private static void validateDynamicTemplate(Mapper.TypeParser.ParserContext pars

final boolean failInvalidDynamicTemplates = parserContext.indexVersionCreated().onOrAfter(Version.V_8_0_0);
if (dynamicTemplateInvalid) {
String message = String.format(Locale.ROOT, "dynamic template [%s] has invalid content [%s]",
dynamicTemplate.getName(), Strings.toString(dynamicTemplate));
String format = "dynamic template [%s] has invalid content [%s], attempted to validate it with the following [match_mapping_type]: [%s]";
String message = String.format(Locale.ROOT, format, dynamicTemplate.getName(), Strings.toString(dynamicTemplate),
Arrays.toString(types));
if (failInvalidDynamicTemplates) {
throw new IllegalArgumentException(message, lastError);
} else {
final String deprecationMessage;
if (lastError != null) {
deprecationMessage = String.format(Locale.ROOT, "%s, caused by [%s]", message, lastError.getMessage());
deprecationMessage = String.format(Locale.ROOT, "%s, last error: [%s]", message, lastError.getMessage());
} else {
deprecationMessage = message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ public void testIllegalDynamicTemplate7DotXIndex() throws Exception {
DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE);
assertThat(mapper.mappingSource().toString(), containsString("\"type\":\"string\""));
assertWarnings("dynamic template [my_template] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{\"type\":" +
"\"string\"}}], caused by [No mapper found for type [string]]");
"\"string\"}}], attempted to validate it with the following [match_mapping_type]: [[string]], " +
"last error: [No mapper found for type [string]]");
}
}

0 comments on commit b3eb3c7

Please sign in to comment.