@@ -363,6 +363,8 @@ private static void innerParseObject(DocumentParserContext context) throws IOExc
363363 XContentParser .Token token = parser .currentToken ();
364364 String currentFieldName = null ;
365365 assert token == XContentParser .Token .FIELD_NAME || token == XContentParser .Token .END_OBJECT ;
366+ int countArray = 0 ;
367+ long arrayObjectsLimit = context .indexSettings ().getMappingArrayObjectsLimit ();
366368 while (token != XContentParser .Token .END_OBJECT ) {
367369 if (token == null ) {
368370 throwEOF (context .parent (), context );
@@ -378,7 +380,7 @@ private static void innerParseObject(DocumentParserContext context) throws IOExc
378380 }
379381 break ;
380382 case START_OBJECT :
381- parseObject (context , currentFieldName );
383+ countArray = parseObject (context , currentFieldName , arrayObjectsLimit , countArray );
382384 break ;
383385 case START_ARRAY :
384386 parseArray (context , currentFieldName );
@@ -519,15 +521,30 @@ private static void throwOnCopyToOnObject(Mapper mapper, List<String> copyToFiel
519521 );
520522 }
521523
522- private static void parseObject (final DocumentParserContext context , String currentFieldName ) throws IOException {
524+ private static int parseObject (final DocumentParserContext context ,
525+ String currentFieldName ,
526+ long arrayObjectsLimit ,
527+ int alreadyCounted ) throws IOException {
528+ int newCount = 0 ;
523529 assert currentFieldName != null ;
524530 context .setImmediateXContentParent (context .parser ().currentToken ());
525531 Mapper objectMapper = context .getMapper (currentFieldName );
526532 if (objectMapper != null ) {
527533 doParseObject (context , currentFieldName , objectMapper );
528534 } else {
535+ newCount = alreadyCounted + 1 ;
536+ if (newCount > arrayObjectsLimit ) {
537+ throw new IllegalStateException (
538+ "The number of array objects has exceeded the allowed limit of ["
539+ + arrayObjectsLimit
540+ + "]. This limit can be set by changing the ["
541+ + MapperService .INDEX_MAPPING_ARRAY_OBJECTS_LIMIT_SETTING .getKey ()
542+ + "] index level setting."
543+ );
544+ }
529545 parseObjectDynamic (context , currentFieldName );
530546 }
547+ return newCount ;
531548 }
532549
533550 private static void doParseObject (DocumentParserContext context , String currentFieldName , Mapper objectMapper ) throws IOException {
@@ -754,18 +771,8 @@ private static void parseNonDynamicArray(
754771 long arrayObjectsLimit = context .indexSettings ().getMappingArrayObjectsLimit ();
755772 while ((token = parser .nextToken ()) != XContentParser .Token .END_ARRAY ) {
756773 if (token == XContentParser .Token .START_OBJECT ) {
757- if (countArray ++ >= arrayObjectsLimit ) {
758- throw new IllegalStateException (
759- "The number of array objects has exceeded the allowed limit of ["
760- + arrayObjectsLimit
761- + "]. "
762- + "This limit can be set by changing the ["
763- + MapperService .INDEX_MAPPING_ARRAY_OBJECTS_LIMIT_SETTING .getKey ()
764- + "] index level setting."
765- );
766- }
767774 elements = 2 ;
768- parseObject (context , lastFieldName );
775+ countArray = parseObject (context , lastFieldName , arrayObjectsLimit , countArray );
769776 } else if (token == XContentParser .Token .START_ARRAY ) {
770777 elements = 2 ;
771778 parseArray (context , lastFieldName );
0 commit comments