Skip to content

Commit

Permalink
Fix array_index_out_of_bounds_exception when indexing documents with …
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
gaobinlong authored and wangdongyu.danny committed Aug 22, 2024
1 parent 30bd5da commit 49158f4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix NPE when bulk ingest with empty pipeline ([#15033](https://github.com/opensearch-project/OpenSearch/pull/15033))
- Fix missing value of FieldSort for unsigned_long ([#14963](https://github.com/opensearch-project/OpenSearch/pull/14963))
- 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))
- 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))

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
"Index documents with field name containing only dot fail with an IllegalArgumentException":
- skip:
version: " - 2.99.99"
reason: "introduced in 3.0.0"

- do:
indices.create:
index: test_1

- do:
catch: /field name cannot contain only the character \[.\]/
index:
index: test_1
id: 1
body: {
.: bar
}

- do:
catch: /field name cannot contain only the character \[.\]/
index:
index: test_1
id: 1
body: {
..: bar
}

- do:
catch: /field name cannot contain only the character \[.\]/
index:
index: test_1
id: 1
body: {
foo: {
.: bar
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ private static MapperParsingException wrapInMapperParsingException(SourceToParse
private static String[] splitAndValidatePath(String fullFieldPath) {
if (fullFieldPath.contains(".")) {
String[] parts = fullFieldPath.split("\\.");
if (parts.length == 0) {
throw new IllegalArgumentException("field name cannot contain only the character [.]");
}
for (String part : parts) {
if (Strings.hasText(part) == false) {
// check if the field name contains only whitespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,38 @@ public void testDynamicFieldsStartingAndEndingWithDot() throws Exception {
);
}

public void testDynamicFieldsWithOnlyDot() throws Exception {
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));

MapperParsingException e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> {
b.startArray("top");
{
b.startObject();
{
b.startObject("inner").field(".", 2).endObject();
}
b.endObject();
}
b.endArray();
})));

assertThat(e.getCause(), notNullValue());
assertThat(e.getCause().getMessage(), containsString("field name cannot contain only the character [.]"));

e = expectThrows(
MapperParsingException.class,
() -> mapper.parse(source(b -> { b.startObject("..").field("foo", 2).endObject(); }))
);

assertThat(e.getCause(), notNullValue());
assertThat(e.getCause().getMessage(), containsString("field name cannot contain only the character [.]"));

e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.field(".", "1234"))));

assertThat(e.getCause(), notNullValue());
assertThat(e.getCause().getMessage(), containsString("field name cannot contain only the character [.]"));
}

public void testDynamicFieldsEmptyName() throws Exception {

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

0 comments on commit 49158f4

Please sign in to comment.