Skip to content

Commit 4bf1225

Browse files
authored
Correctly handle non-integers in nested paths in the remove processor (elastic#127006) (elastic#127064)
1 parent 0ab1fc3 commit 4bf1225

File tree

4 files changed

+77
-9
lines changed

4 files changed

+77
-9
lines changed

docs/changelog/127006.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 127006
2+
summary: Correctly handle non-integers in nested paths in the remove processor
3+
area: Ingest Node
4+
type: bug
5+
issues: []

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorTests.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.script.TemplateScript;
1717
import org.elasticsearch.test.ESTestCase;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.HashMap;
2122
import java.util.List;
@@ -103,6 +104,7 @@ public void testIgnoreMissingAndNullInPath() throws Exception {
103104
some.put("map", map);
104105
source.put("some", some);
105106
}
107+
default -> throw new AssertionError("failure, got illegal switch case");
106108
}
107109
IngestDocument document = RandomDocumentPicks.randomIngestDocument(random(), source);
108110
Map<String, Object> config = new HashMap<>();
@@ -113,6 +115,53 @@ public void testIgnoreMissingAndNullInPath() throws Exception {
113115
assertThat(document.hasField("some.map.path"), is(false));
114116
}
115117

118+
public void testIgnoreMissingAndNonIntegerInPath() throws Exception {
119+
Map<String, Object> source = new HashMap<>();
120+
Map<String, Object> some = new HashMap<>();
121+
List<Object> array = new ArrayList<>();
122+
Map<String, Object> path = new HashMap<>();
123+
124+
switch (randomIntBetween(0, 6)) {
125+
case 0 -> {
126+
// empty source
127+
}
128+
case 1 -> {
129+
source.put("some", null);
130+
}
131+
case 2 -> {
132+
some.put("array", null);
133+
source.put("some", some);
134+
}
135+
case 3 -> {
136+
some.put("array", array);
137+
source.put("some", some);
138+
}
139+
case 4 -> {
140+
array.add(null);
141+
some.put("array", array);
142+
source.put("some", some);
143+
}
144+
case 5 -> {
145+
array.add(path);
146+
some.put("array", array);
147+
source.put("some", some);
148+
}
149+
case 6 -> {
150+
array.add("foobar");
151+
some.put("array", array);
152+
source.put("some", some);
153+
}
154+
default -> throw new AssertionError("failure, got illegal switch case");
155+
}
156+
IngestDocument document = RandomDocumentPicks.randomIngestDocument(random(), source);
157+
Map<String, Object> config = new HashMap<>();
158+
config.put("field", "some.array.path");
159+
config.put("ignore_missing", true);
160+
Processor processor = new RemoveProcessor.Factory(TestTemplateService.instance()).create(null, null, null, config);
161+
processor.execute(document);
162+
assertThat(document.hasField("some.array.path"), is(false));
163+
}
164+
116165
public void testKeepFields() throws Exception {
117166
Map<String, Object> address = new HashMap<>();
118167
address.put("street", "Ipiranga Street");

server/src/main/java/org/elasticsearch/ingest/IngestDocument.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,13 @@ public void removeField(String path, boolean ignoreMissing) {
359359
throw new IllegalArgumentException(Errors.notPresent(path, leafKey));
360360
}
361361
} else if (context instanceof List<?> list) {
362-
int index;
362+
int index = -1;
363363
try {
364364
index = Integer.parseInt(leafKey);
365365
} catch (NumberFormatException e) {
366-
throw new IllegalArgumentException(Errors.notInteger(path, leafKey), e);
366+
if (ignoreMissing == false) {
367+
throw new IllegalArgumentException(Errors.notInteger(path, leafKey), e);
368+
}
367369
}
368370
if (index < 0 || index >= list.size()) {
369371
if (ignoreMissing == false) {

server/src/test/java/org/elasticsearch/ingest/IngestDocumentTests.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -860,13 +860,25 @@ public void testRemoveFieldIgnoreMissing() {
860860

861861
// if ignoreMissing is false, we throw an exception for values that aren't found
862862
IllegalArgumentException e;
863-
if (randomBoolean()) {
864-
document.setFieldValue("fizz.some", (Object) null);
865-
e = expectThrows(IllegalArgumentException.class, () -> document.removeField("fizz.some.nonsense", false));
866-
assertThat(e.getMessage(), is("cannot remove [nonsense] from null as part of path [fizz.some.nonsense]"));
867-
} else {
868-
e = expectThrows(IllegalArgumentException.class, () -> document.removeField("fizz.some.nonsense", false));
869-
assertThat(e.getMessage(), is("field [some] not present as part of path [fizz.some.nonsense]"));
863+
switch (randomIntBetween(0, 2)) {
864+
case 0 -> {
865+
document.setFieldValue("fizz.some", (Object) null);
866+
e = expectThrows(IllegalArgumentException.class, () -> document.removeField("fizz.some.nonsense", false));
867+
assertThat(e.getMessage(), is("cannot remove [nonsense] from null as part of path [fizz.some.nonsense]"));
868+
}
869+
case 1 -> {
870+
document.setFieldValue("fizz.some", List.of("foo", "bar"));
871+
e = expectThrows(IllegalArgumentException.class, () -> document.removeField("fizz.some.nonsense", false));
872+
assertThat(
873+
e.getMessage(),
874+
is("[nonsense] is not an integer, cannot be used as an index as part of path [fizz.some.nonsense]")
875+
);
876+
}
877+
case 2 -> {
878+
e = expectThrows(IllegalArgumentException.class, () -> document.removeField("fizz.some.nonsense", false));
879+
assertThat(e.getMessage(), is("field [some] not present as part of path [fizz.some.nonsense]"));
880+
}
881+
default -> throw new AssertionError("failure, got illegal switch case");
870882
}
871883

872884
// but no exception is thrown if ignoreMissing is true

0 commit comments

Comments
 (0)