Skip to content

Commit

Permalink
Fixes breaking change introduced in elastic#95558.
Browse files Browse the repository at this point in the history
Match and unmatch fields in dynamic_templates (match, unmatch, path_match, path_unmatch)
can either be single-valued or a list of values. If a list of values is provided, the
values must all be strings. Numbers, boolean, other lists, etc. are not allowed and an
error will be returned to the user in that case.

However, previously (before elastic#95558) we allowed any JSON type for the match/unmatch
fields (not just string), so changing that would be a breaking change. Thus, this commit
no longer enforces string-only types for single-valued match/unmatch fields in dynamic_templates.
  • Loading branch information
quux00 committed May 2, 2023
1 parent 8e9469a commit 150360a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 7 additions & 1 deletion docs/reference/mapping/dynamic/templates.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ PUT my-index-000001/_doc/1
You can specify a list of patterns using a JSON array for either the
`match` or `unmatch` fields.

The next example matches all fields whose name starts with `ip_` or ends with `_ip`,
The next example matches all fields whose name starts with `ip_` or ends with `_ip`,
except for fields which start with `one` or end with `two` and maps them
as `ip` fields:

Expand Down Expand Up @@ -273,6 +273,12 @@ PUT my-index/_doc/1
<4> The `ip_four` field is mapped as type `ip`.


*Note:* when specifying patterns (for `match`, `unmatch`, etc.) in an array,
{es} enforces that all values must be strings. However, for backwards
compatibility, {es} allows non-string values (such as numbers) for
single-valued match (and unmatch) fields.


[[path-match-unmatch]]
==== `path_match` and `path_unmatch`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,20 @@ private static boolean matchPatternsAreDefined(List<String> match, List<String>
}

private static void addEntriesToPatternList(List<String> matchList, String propName, Map.Entry<String, Object> entry) {
if (entry.getValue() instanceof String s) {
matchList.add(s);
} else if (entry.getValue() instanceof List<?> ls) {
if (entry.getValue() instanceof List<?> ls) {
for (Object o : ls) {
if (o instanceof String s) {
matchList.add(s);
} else {
// for array-based matches, we enforce that only strings are allowed as un/match values in the array
throw new MapperParsingException(
Strings.format("[%s] values must either be a string or list of strings, but was [%s]", propName, entry.getValue())
);
}
}
} else {
throw new MapperParsingException(
Strings.format("[%s] values must either be a string or list of strings, but was [%s]", propName, entry.getValue())
);
// to preserve backwards compatability for single-valued matches, we convert whatever the user provided to a string
matchList.add(entry.getValue().toString());
}
}

Expand Down

0 comments on commit 150360a

Please sign in to comment.