Add test for matching middle key bug (#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 #129600. Adding this test to confirm. See #129600 for a full explanation.
This commit is contained in:
Parker Timmins 2025-07-01 14:24:48 -05:00 committed by GitHub
parent 416405877f
commit 3a69d45892
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 29 additions and 0 deletions

View File

@ -931,6 +931,35 @@ public class FlattenedFieldMapperTests extends MapperTestCase {
{"field":{"key1":"foo"}}"""));
}
public void testSyntheticSourceWithMatchesInNestedPath() throws IOException {
DocumentMapper mapper = createSytheticSourceMapperService(
mapping(b -> { b.startObject("field").field("type", "flattened").endObject(); })
).documentMapper();
// This test covers a scenario that previously had a bug.
// Since a.b.c and b.b.d have a matching middle key `b`, and b.b.d starts with a `b`,
// startObject was not called for the first `b` in b.b.d.
// For a full explanation see this comment: https://github.com/elastic/elasticsearch/pull/129600#issuecomment-3024476134
var syntheticSource = syntheticSource(mapper, b -> {
b.startObject("field");
{
b.startObject("a");
{
b.startObject("b").field("c", "1").endObject();
}
b.endObject();
b.startObject("b");
{
b.startObject("b").field("d", "2").endObject();
}
b.endObject();
}
b.endObject();
});
assertThat(syntheticSource, equalTo("""
{"field":{"a":{"b":{"c":"1"}},"b":{"b":{"d":"2"}}}}"""));
}
@Override
protected boolean supportsCopyTo() {
return false;