Skip to content

Commit 933f8da

Browse files
Addressing feedback
1 parent e265931 commit 933f8da

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/common/MapPathExtractor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Uses a subset of the JSONPath schema to extract fields from a map.
2121
* For more information <a href="https://en.wikipedia.org/wiki/JSONPath">see here</a>.
2222
*
23-
* This implementation differs in out it handles lists in that JSONPath will flatten inner lists. This implementation
23+
* This implementation differs in how it handles lists in that JSONPath will flatten inner lists. This implementation
2424
* preserves inner lists.
2525
*
2626
* Examples of the schema:
@@ -133,9 +133,10 @@ public static Object extract(Map<String, Object> data, String path) {
133133

134134
var cleanedPath = path.trim();
135135

136-
// Remove the prefix if it exists
137136
if (cleanedPath.startsWith(DOLLAR)) {
138137
cleanedPath = cleanedPath.substring(DOLLAR.length());
138+
} else {
139+
throw new IllegalArgumentException(Strings.format("Path [%s] must start with a dollar sign ($)", cleanedPath));
139140
}
140141

141142
return navigate(data, cleanedPath);

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/common/MapPathExtractorTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public void testExtract_ReturnsNull_WhenPathIsWhiteSpace() {
6666
assertNull(MapPathExtractor.extract(Map.of("key", "value"), " "));
6767
}
6868

69+
public void testExtract_ThrowsException_WhenPathDoesNotStartWithDollarSign() {
70+
var exception = expectThrows(IllegalArgumentException.class, () -> MapPathExtractor.extract(Map.of("key", "value"), ".key"));
71+
assertThat(exception.getMessage(), is("Path [.key] must start with a dollar sign ($)"));
72+
}
73+
6974
public void testExtract_ThrowsException_WhenCannotFindField() {
7075
Map<String, Object> input = Map.of("result", "key");
7176

@@ -76,7 +81,7 @@ public void testExtract_ThrowsException_WhenCannotFindField() {
7681
public void testExtract_ThrowsAnException_WhenThePathIsInvalid() {
7782
Map<String, Object> input = Map.of("result", "key");
7883

79-
var exception = expectThrows(IllegalArgumentException.class, () -> MapPathExtractor.extract(input, "awesome"));
84+
var exception = expectThrows(IllegalArgumentException.class, () -> MapPathExtractor.extract(input, "$awesome"));
8085
assertThat(exception.getMessage(), is("Invalid path received [awesome], unable to extract a field name."));
8186
}
8287

0 commit comments

Comments
 (0)