Skip to content

Commit 4f4e7a4

Browse files
committed
Throw on square brackets in field path
1 parent d3dc2a9 commit 4f4e7a4

File tree

1 file changed

+13
-93
lines changed

1 file changed

+13
-93
lines changed

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

Lines changed: 13 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.Set;
4545
import java.util.function.BiConsumer;
4646
import java.util.stream.Collectors;
47-
import java.util.stream.Stream;
4847

4948
/**
5049
* Represents a single document being captured before indexing and holds the source and metadata (like id, type and index).
@@ -1290,55 +1289,6 @@ public String getFieldName() {
12901289

12911290
private static final class FieldPath {
12921291

1293-
private record Element(String fieldName, Integer arrayIndex) {
1294-
private static final String EMPTY_STRING = "";
1295-
1296-
/**
1297-
* Creates a new FieldPath Element which corresponds to a regular part of a field path.
1298-
* @param fieldName name of the field to access, or a string to use for array indexing if running in CLASSIC access pattern.
1299-
* @return A field name element
1300-
*/
1301-
static Element field(String fieldName) {
1302-
Objects.requireNonNull(fieldName, "fieldName cannot be null");
1303-
if (fieldName.isEmpty()) {
1304-
throw new IllegalArgumentException("fieldName cannot be empty");
1305-
}
1306-
return new Element(fieldName, null);
1307-
}
1308-
1309-
/**
1310-
* Creates a new FieldPath Element which corresponds to an array index specified by the square bracket syntax available
1311-
* when using the {@link IngestPipelineFieldAccessPattern#FLEXIBLE} access pattern.
1312-
* @param arrayIndex array index specified in square brackets
1313-
* @return An array index element
1314-
*/
1315-
static Element index(int arrayIndex) {
1316-
if (arrayIndex < 0) {
1317-
throw new IndexOutOfBoundsException(arrayIndex);
1318-
}
1319-
return new Element(EMPTY_STRING, arrayIndex);
1320-
}
1321-
1322-
/**
1323-
* @return true if this element is for accessing a regular field
1324-
*/
1325-
boolean isFieldName() {
1326-
return fieldName.isEmpty() == false && arrayIndex == null;
1327-
}
1328-
1329-
/**
1330-
* @return true if this element is for an array index used for the FLEXIBLE access pattern
1331-
*/
1332-
boolean isArrayIndex() {
1333-
return isFieldName() == false;
1334-
}
1335-
1336-
@Override
1337-
public String toString() {
1338-
return isFieldName() ? fieldName : "[" + arrayIndex + "]";
1339-
}
1340-
}
1341-
13421292
/**
13431293
* A compound cache key for tracking previously parsed field paths
13441294
* @param path The field path as given by the caller
@@ -1373,7 +1323,7 @@ static FieldPath of(String path, IngestPipelineFieldAccessPattern accessPattern)
13731323
return res;
13741324
}
13751325

1376-
private final Element[] pathElements;
1326+
private final String[] pathElements;
13771327
private final boolean useIngestContext;
13781328

13791329
// you shouldn't call this directly, use the FieldPath.of method above instead!
@@ -1394,12 +1344,12 @@ private FieldPath(String path, IngestPipelineFieldAccessPattern accessPattern) {
13941344
this.pathElements = processPathParts(path, pathParts, accessPattern);
13951345
}
13961346

1397-
private static Element[] processPathParts(String fullPath, String[] pathParts, IngestPipelineFieldAccessPattern accessPattern) {
1347+
private static String[] processPathParts(String fullPath, String[] pathParts, IngestPipelineFieldAccessPattern accessPattern) {
13981348
if (pathParts.length == 1 && pathParts[0].isEmpty()) {
13991349
throw new IllegalArgumentException("path [" + fullPath + "] is not valid");
14001350
}
14011351
return switch (accessPattern) {
1402-
case CLASSIC -> Arrays.stream(pathParts).map(Element::field).toArray(Element[]::new);
1352+
case CLASSIC -> pathParts;
14031353
case FLEXIBLE -> parseFlexibleFields(fullPath, pathParts);
14041354
};
14051355
}
@@ -1411,46 +1361,16 @@ private static Element[] processPathParts(String fullPath, String[] pathParts, I
14111361
* @param pathParts The tokenized field path to parse
14121362
* @return An array of Elements
14131363
*/
1414-
private static Element[] parseFlexibleFields(String fullPath, String[] pathParts) {
1415-
return Arrays.stream(pathParts)
1416-
.flatMap(pathPart -> {
1417-
int openBracket = pathPart.indexOf('[');
1418-
if (openBracket == -1) {
1419-
return Stream.of(Element.field(pathPart));
1420-
} else if (openBracket == 0) {
1421-
throw new IllegalArgumentException("path [" + fullPath + "] is not valid");
1422-
} else {
1423-
List<Element> resultElements = new ArrayList<>();
1424-
String rootField = pathPart.substring(0, openBracket);
1425-
resultElements.add(Element.field(rootField));
1426-
1427-
boolean elementsRemain = true;
1428-
while (elementsRemain) {
1429-
int closeBracket = pathPart.indexOf(']', openBracket);
1430-
if (closeBracket <= openBracket) {
1431-
throw new IllegalArgumentException("path [" + fullPath + "] is not valid");
1432-
}
1433-
1434-
String rawIndex = pathPart.substring(openBracket + 1, closeBracket);
1435-
try {
1436-
resultElements.add(Element.index(Integer.parseInt(rawIndex)));
1437-
} catch (NumberFormatException numberFormatException) {
1438-
throw new IllegalArgumentException("path [" + fullPath + "] is not valid");
1439-
}
1440-
1441-
if (closeBracket == pathPart.length() - 1) {
1442-
elementsRemain = false;
1443-
} else {
1444-
if (pathPart.charAt(closeBracket + 1) != '[') {
1445-
throw new IllegalArgumentException("path [" + fullPath + "] is not valid");
1446-
}
1447-
openBracket = closeBracket + 1;
1448-
}
1449-
}
1450-
return resultElements.stream();
1451-
}
1452-
})
1453-
.toArray(Element[]::new);
1364+
private static String[] parseFlexibleFields(String fullPath, String[] pathParts) {
1365+
boolean invalidPath = Arrays.stream(pathParts).anyMatch(pathPart -> {
1366+
int openBracket = pathPart.indexOf('[');
1367+
int closedBracket = pathPart.indexOf(']');
1368+
return openBracket != -1 && closedBracket != -1;
1369+
});
1370+
if (invalidPath) {
1371+
throw new IllegalArgumentException("path [" + fullPath + "] is not valid");
1372+
}
1373+
return pathParts;
14541374
}
14551375

14561376
public Object initialContext(IngestDocument document) {

0 commit comments

Comments
 (0)