@@ -403,25 +403,22 @@ static List<Tuple<String, Boolean>> alterateQueries(Set<String> queries, Set<Str
403
403
int mutation = 0 ;
404
404
405
405
while (true ) {
406
- // Track the objects hierarchy
407
- Deque <String > hierarchy = new LinkedList <>();
406
+ boolean expectException = true ;
408
407
409
408
BytesStreamOutput out = new BytesStreamOutput ();
410
409
try (
411
410
XContentGenerator generator = XContentType .JSON .xContent ().createGenerator (out );
412
411
XContentParser parser = XContentHelper .createParser (new BytesArray (query ));
413
412
) {
414
- // Parse the valid query and inserts a new object level called "newField"
415
413
int objectIndex = -1 ;
414
+ Deque <String > levels = new LinkedList <>();
416
415
416
+ // Parse the valid query and inserts a new object level called "newField"
417
417
XContentParser .Token token ;
418
418
while ((token = parser .nextToken ()) != null ) {
419
419
if (token == XContentParser .Token .START_OBJECT ) {
420
420
objectIndex ++;
421
-
422
- if (objectIndex <= mutation ) {
423
- hierarchy .push (parser .currentName ());
424
- }
421
+ levels .addLast (parser .currentName ());
425
422
426
423
if (objectIndex == mutation ) {
427
424
// 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
430
427
XContentHelper .copyCurrentStructure (generator , parser );
431
428
generator .writeEndObject ();
432
429
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
+
433
442
// Jump to next token
434
443
continue ;
435
444
}
445
+ } else if (token == XContentParser .Token .END_OBJECT ) {
446
+ levels .removeLast ();
436
447
}
437
448
438
449
// 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
451
462
}
452
463
}
453
464
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
-
466
465
results .add (new Tuple <>(out .bytes ().utf8ToString (), expectException ));
467
466
}
468
467
}
0 commit comments