Tests: Make $_path support dots in paths (#28917)
`$_path` is used by documentation tests to ignore a value from a response, for example: ``` [source,js] ---- { "count": 1, "datafeeds": [ { "datafeed_id": "datafeed-total-requests", "state": "started", "node": { ... "attributes": { "ml.machine_memory": "17179869184", "ml.max_open_jobs": "20", "ml.enabled": "true" } }, "assignment_explanation": "" } ] } ---- // TESTRESPONSE[s/"17179869184"/$body.$_path/] ``` That example shows `17179869184` in the compiled docs but when it runs the tests generated by that doc it ignores `17179869184` and asserts instead that there is a value in that field. This is required because we can't predict things like "how many milliseconds will this take?" and "how much memory will this take?". Before this change it was impossible to use `$_path` when any component of the path contained a `.`. This fixes the `$_path` evaluator to properly escape `.`. Closes #28770
This commit is contained in:
parent
5135484a27
commit
a813492fe3
|
@ -184,10 +184,10 @@ public class Stash implements ToXContentFragment {
|
|||
StringBuilder pathBuilder = new StringBuilder();
|
||||
Iterator<Object> element = path.iterator();
|
||||
if (element.hasNext()) {
|
||||
pathBuilder.append(element.next());
|
||||
pathBuilder.append(element.next().toString().replace(".", "\\."));
|
||||
while (element.hasNext()) {
|
||||
pathBuilder.append('.');
|
||||
pathBuilder.append(element.next());
|
||||
pathBuilder.append(element.next().toString().replace(".", "\\."));
|
||||
}
|
||||
}
|
||||
String builtPath = Matcher.quoteReplacement(pathBuilder.toString());
|
||||
|
|
|
@ -119,16 +119,22 @@ public class StashTests extends ESTestCase {
|
|||
|
||||
public void testPathInList() throws IOException {
|
||||
Stash stash = new Stash();
|
||||
stash.stashValue("body", singletonMap("foo", Arrays.asList("a", "b")));
|
||||
String topLevelKey;
|
||||
if (randomBoolean()) {
|
||||
topLevelKey = randomAlphaOfLength(2) + "." + randomAlphaOfLength(2);
|
||||
} else {
|
||||
topLevelKey = randomAlphaOfLength(5);
|
||||
}
|
||||
stash.stashValue("body", singletonMap(topLevelKey, Arrays.asList("a", "b")));
|
||||
|
||||
Map<String, Object> expected;
|
||||
Map<String, Object> map;
|
||||
if (randomBoolean()) {
|
||||
expected = singletonMap("foo", Arrays.asList("test", "boooooh!"));
|
||||
map = singletonMap("foo", Arrays.asList("test", "${body.$_path}oooooh!"));
|
||||
expected = singletonMap(topLevelKey, Arrays.asList("test", "boooooh!"));
|
||||
map = singletonMap(topLevelKey, Arrays.asList("test", "${body.$_path}oooooh!"));
|
||||
} else {
|
||||
expected = singletonMap("foo", Arrays.asList("test", "b"));
|
||||
map = singletonMap("foo", Arrays.asList("test", "$body.$_path"));
|
||||
expected = singletonMap(topLevelKey, Arrays.asList("test", "b"));
|
||||
map = singletonMap(topLevelKey, Arrays.asList("test", "$body.$_path"));
|
||||
}
|
||||
|
||||
Map<String, Object> actual = stash.replaceStashedValues(map);
|
||||
|
@ -138,21 +144,26 @@ public class StashTests extends ESTestCase {
|
|||
|
||||
public void testPathInMapValue() throws IOException {
|
||||
Stash stash = new Stash();
|
||||
stash.stashValue("body", singletonMap("foo", singletonMap("a", "b")));
|
||||
String topLevelKey;
|
||||
if (randomBoolean()) {
|
||||
topLevelKey = randomAlphaOfLength(2) + "." + randomAlphaOfLength(2);
|
||||
} else {
|
||||
topLevelKey = randomAlphaOfLength(5);
|
||||
}
|
||||
stash.stashValue("body", singletonMap(topLevelKey, singletonMap("a", "b")));
|
||||
|
||||
Map<String, Object> expected;
|
||||
Map<String, Object> map;
|
||||
if (randomBoolean()) {
|
||||
expected = singletonMap("foo", singletonMap("a", "boooooh!"));
|
||||
map = singletonMap("foo", singletonMap("a", "${body.$_path}oooooh!"));
|
||||
expected = singletonMap(topLevelKey, singletonMap("a", "boooooh!"));
|
||||
map = singletonMap(topLevelKey, singletonMap("a", "${body.$_path}oooooh!"));
|
||||
} else {
|
||||
expected = singletonMap("foo", singletonMap("a", "b"));
|
||||
map = singletonMap("foo", singletonMap("a", "$body.$_path"));
|
||||
expected = singletonMap(topLevelKey, singletonMap("a", "b"));
|
||||
map = singletonMap(topLevelKey, singletonMap("a", "$body.$_path"));
|
||||
}
|
||||
|
||||
Map<String, Object> actual = stash.replaceStashedValues(map);
|
||||
assertEquals(expected, actual);
|
||||
assertThat(actual, not(sameInstance(map)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue