Skip to content

Correctly handle non-integers in nested paths in the remove processor #127006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add failing tests
  • Loading branch information
joegallo committed Apr 17, 2025
commit b98aa4b76c6507488a38ec60774af77fedcae88c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.script.TemplateScript;
import org.elasticsearch.test.ESTestCase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -114,6 +115,53 @@ public void testIgnoreMissingAndNullInPath() throws Exception {
assertThat(document.hasField("some.map.path"), is(false));
}

public void testIgnoreMissingAndNonIntegerInPath() throws Exception {
Map<String, Object> source = new HashMap<>();
Map<String, Object> some = new HashMap<>();
List<Object> array = new ArrayList<>();
Map<String, Object> path = new HashMap<>();

switch (randomIntBetween(0, 6)) {
case 0 -> {
// empty source
}
case 1 -> {
source.put("some", null);
}
case 2 -> {
some.put("array", null);
source.put("some", some);
}
case 3 -> {
some.put("array", array);
source.put("some", some);
}
case 4 -> {
array.add(null);
some.put("array", array);
source.put("some", some);
}
case 5 -> {
array.add(path);
some.put("array", array);
source.put("some", some);
}
case 6 -> {
array.add("foobar");
some.put("array", array);
source.put("some", some);
}
default -> throw new AssertionError("failure, got illegal switch case");
}
IngestDocument document = RandomDocumentPicks.randomIngestDocument(random(), source);
Map<String, Object> config = new HashMap<>();
config.put("field", "some.array.path");
config.put("ignore_missing", true);
Processor processor = new RemoveProcessor.Factory(TestTemplateService.instance()).create(null, null, null, config, null);
processor.execute(document);
assertThat(document.hasField("some.array.path"), is(false));
}

public void testKeepFields() throws Exception {
Map<String, Object> address = new HashMap<>();
address.put("street", "Ipiranga Street");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,13 +860,21 @@ public void testRemoveFieldIgnoreMissing() {

// if ignoreMissing is false, we throw an exception for values that aren't found
IllegalArgumentException e;
switch (randomIntBetween(0, 1)) {
switch (randomIntBetween(0, 2)) {
case 0 -> {
document.setFieldValue("fizz.some", (Object) null);
e = expectThrows(IllegalArgumentException.class, () -> document.removeField("fizz.some.nonsense", false));
assertThat(e.getMessage(), is("cannot remove [nonsense] from null as part of path [fizz.some.nonsense]"));
}
case 1 -> {
document.setFieldValue("fizz.some", List.of("foo", "bar"));
e = expectThrows(IllegalArgumentException.class, () -> document.removeField("fizz.some.nonsense", false));
assertThat(
e.getMessage(),
is("[nonsense] is not an integer, cannot be used as an index as part of path [fizz.some.nonsense]")
);
}
case 2 -> {
e = expectThrows(IllegalArgumentException.class, () -> document.removeField("fizz.some.nonsense", false));
assertThat(e.getMessage(), is("field [some] not present as part of path [fizz.some.nonsense]"));
}
Expand Down