Skip to content

Commit 68b943d

Browse files
committed
Fix MoreLikeThisQueryBuilderTests.testUnknownObjectException()
Objects hierarchy must be tracked when entering/leaving an object so that it better knows if the "newField" has been inserted into an arbitrary holding object. Can be reproduced with gradle :core:test -Dtests.seed=760F8BD0F7E46D45 -Dtests.class=org.elasticsearch.index.query.MoreLikeThisQueryBuilderTests -Dtests.method="testUnknownObjectException" -Dtests.security.manager=true -Dtests.locale=ko -Dtests.timezone=Etc/Zulu
1 parent 5a48ad6 commit 68b943d

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -403,25 +403,22 @@ static List<Tuple<String, Boolean>> alterateQueries(Set<String> queries, Set<Str
403403
int mutation = 0;
404404

405405
while (true) {
406-
// Track the objects hierarchy
407-
Deque<String> hierarchy = new LinkedList<>();
406+
boolean expectException = true;
408407

409408
BytesStreamOutput out = new BytesStreamOutput();
410409
try (
411410
XContentGenerator generator = XContentType.JSON.xContent().createGenerator(out);
412411
XContentParser parser = XContentHelper.createParser(new BytesArray(query));
413412
) {
414-
// Parse the valid query and inserts a new object level called "newField"
415413
int objectIndex = -1;
414+
Deque<String> levels = new LinkedList<>();
416415

416+
// Parse the valid query and inserts a new object level called "newField"
417417
XContentParser.Token token;
418418
while ((token = parser.nextToken()) != null) {
419419
if (token == XContentParser.Token.START_OBJECT) {
420420
objectIndex++;
421-
422-
if (objectIndex <= mutation) {
423-
hierarchy.push(parser.currentName());
424-
}
421+
levels.addLast(parser.currentName());
425422

426423
if (objectIndex == mutation) {
427424
// We reached the place in the object tree where we want to insert a new object level
@@ -430,9 +427,23 @@ static List<Tuple<String, Boolean>> alterateQueries(Set<String> queries, Set<Str
430427
XContentHelper.copyCurrentStructure(generator, parser);
431428
generator.writeEndObject();
432429

430+
if (hasArbitraryContent) {
431+
// The query has one or more fields that hold arbitrary content. If the current
432+
// field is one (or a child) of those, no exception is expected when parsing the mutated query.
433+
String h = Arrays.toString(levels.toArray());
434+
for (String marker : arbitraryMarkers) {
435+
if (levels.contains(marker)) {
436+
expectException = false;
437+
break;
438+
}
439+
}
440+
}
441+
433442
// Jump to next token
434443
continue;
435444
}
445+
} else if (token == XContentParser.Token.END_OBJECT) {
446+
levels.removeLast();
436447
}
437448

438449
// We are walking through the object tree, so we can safely copy the current node
@@ -451,18 +462,6 @@ static List<Tuple<String, Boolean>> alterateQueries(Set<String> queries, Set<Str
451462
}
452463
}
453464

454-
boolean expectException = true;
455-
if (hasArbitraryContent) {
456-
// The query has one or more fields that hold arbitrary content. If the current
457-
// field is one (or a child) of those, no exception is expected when parsing the mutated query.
458-
for (String marker : arbitraryMarkers) {
459-
if (hierarchy.contains(marker)) {
460-
expectException = false;
461-
break;
462-
}
463-
}
464-
}
465-
466465
results.add(new Tuple<>(out.bytes().utf8ToString(), expectException));
467466
}
468467
}

0 commit comments

Comments
 (0)