Skip to content

Commit 0b6fe7a

Browse files
parkertimminsmridula-s109
authored andcommitted
Add test for matching middle key bug (elastic#130396)
There was a bug in previous version where flattened fields would produce incorrect synthetic source with too few opening braces. This bug was fixed as a side effect of elastic#129600. Adding this test to confirm. See elastic#129600 for a full explanation.
1 parent 928ea95 commit 0b6fe7a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapperTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,35 @@ public void testSyntheticSourceWithEmptyObject() throws IOException {
931931
{"field":{"key1":"foo"}}"""));
932932
}
933933

934+
public void testSyntheticSourceWithMatchesInNestedPath() throws IOException {
935+
DocumentMapper mapper = createSytheticSourceMapperService(
936+
mapping(b -> { b.startObject("field").field("type", "flattened").endObject(); })
937+
).documentMapper();
938+
939+
// This test covers a scenario that previously had a bug.
940+
// Since a.b.c and b.b.d have a matching middle key `b`, and b.b.d starts with a `b`,
941+
// startObject was not called for the first `b` in b.b.d.
942+
// For a full explanation see this comment: https://github.com/elastic/elasticsearch/pull/129600#issuecomment-3024476134
943+
var syntheticSource = syntheticSource(mapper, b -> {
944+
b.startObject("field");
945+
{
946+
b.startObject("a");
947+
{
948+
b.startObject("b").field("c", "1").endObject();
949+
}
950+
b.endObject();
951+
b.startObject("b");
952+
{
953+
b.startObject("b").field("d", "2").endObject();
954+
}
955+
b.endObject();
956+
}
957+
b.endObject();
958+
});
959+
assertThat(syntheticSource, equalTo("""
960+
{"field":{"a":{"b":{"c":"1"}},"b":{"b":{"d":"2"}}}}"""));
961+
}
962+
934963
@Override
935964
protected boolean supportsCopyTo() {
936965
return false;

0 commit comments

Comments
 (0)