Skip to content

Commit 066da28

Browse files
gaobinlongharshavamsi
authored andcommitted
Fix array_index_out_of_bounds_exception when indexing documents with field name containing only dot (opensearch-project#15126)
* Fix array_index_out_of_bounds_exception when indexing documents with field name containing only dot Signed-off-by: Gao Binlong <[email protected]> * Modify change log Signed-off-by: Gao Binlong <[email protected]> * Optimize error message Signed-off-by: Gao Binlong <[email protected]> --------- Signed-off-by: Gao Binlong <[email protected]>
1 parent 69d1a1f commit 066da28

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6969
- Fix NPE when bulk ingest with empty pipeline ([#15033](https://github.com/opensearch-project/OpenSearch/pull/15033))
7070
- Fix missing value of FieldSort for unsigned_long ([#14963](https://github.com/opensearch-project/OpenSearch/pull/14963))
7171
- Fix delete index template failed when the index template matches a data stream but is unused ([#15080](https://github.com/opensearch-project/OpenSearch/pull/15080))
72+
- Fix array_index_out_of_bounds_exception when indexing documents with field name containing only dot ([#15126](https://github.com/opensearch-project/OpenSearch/pull/15126))
7273

7374
### Security
7475

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
"Index documents with field name containing only dot fail with an IllegalArgumentException":
3+
- skip:
4+
version: " - 2.99.99"
5+
reason: "introduced in 3.0.0"
6+
7+
- do:
8+
indices.create:
9+
index: test_1
10+
11+
- do:
12+
catch: /field name cannot contain only the character \[.\]/
13+
index:
14+
index: test_1
15+
id: 1
16+
body: {
17+
.: bar
18+
}
19+
20+
- do:
21+
catch: /field name cannot contain only the character \[.\]/
22+
index:
23+
index: test_1
24+
id: 1
25+
body: {
26+
..: bar
27+
}
28+
29+
- do:
30+
catch: /field name cannot contain only the character \[.\]/
31+
index:
32+
index: test_1
33+
id: 1
34+
body: {
35+
foo: {
36+
.: bar
37+
}
38+
}

server/src/main/java/org/opensearch/index/mapper/DocumentParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ private static MapperParsingException wrapInMapperParsingException(SourceToParse
206206
private static String[] splitAndValidatePath(String fullFieldPath) {
207207
if (fullFieldPath.contains(".")) {
208208
String[] parts = fullFieldPath.split("\\.");
209+
if (parts.length == 0) {
210+
throw new IllegalArgumentException("field name cannot contain only the character [.]");
211+
}
209212
for (String part : parts) {
210213
if (Strings.hasText(part) == false) {
211214
// check if the field name contains only whitespace

server/src/test/java/org/opensearch/index/mapper/DocumentParserTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,38 @@ public void testDynamicFieldsStartingAndEndingWithDot() throws Exception {
17001700
);
17011701
}
17021702

1703+
public void testDynamicFieldsWithOnlyDot() throws Exception {
1704+
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));
1705+
1706+
MapperParsingException e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> {
1707+
b.startArray("top");
1708+
{
1709+
b.startObject();
1710+
{
1711+
b.startObject("inner").field(".", 2).endObject();
1712+
}
1713+
b.endObject();
1714+
}
1715+
b.endArray();
1716+
})));
1717+
1718+
assertThat(e.getCause(), notNullValue());
1719+
assertThat(e.getCause().getMessage(), containsString("field name cannot contain only the character [.]"));
1720+
1721+
e = expectThrows(
1722+
MapperParsingException.class,
1723+
() -> mapper.parse(source(b -> { b.startObject("..").field("foo", 2).endObject(); }))
1724+
);
1725+
1726+
assertThat(e.getCause(), notNullValue());
1727+
assertThat(e.getCause().getMessage(), containsString("field name cannot contain only the character [.]"));
1728+
1729+
e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.field(".", "1234"))));
1730+
1731+
assertThat(e.getCause(), notNullValue());
1732+
assertThat(e.getCause().getMessage(), containsString("field name cannot contain only the character [.]"));
1733+
}
1734+
17031735
public void testDynamicFieldsEmptyName() throws Exception {
17041736

17051737
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));

0 commit comments

Comments
 (0)