@@ -177,21 +177,31 @@ private void handleBatch(HttpServletResponse resp, JsonNode rootNode, int maxRes
177177 for (int i = 0 ; i < rootNode .size (); i ++) {
178178 JsonNode subRequest = rootNode .get (i );
179179
180- if (!subRequest .isObject ()) {
181- batchResult .add (buildErrorNode (JsonRpcError .INVALID_REQUEST , "Invalid Request" , null ));
182- continue ;
183- }
184-
185180 if (overflow ) {
186- // Notifications (no "id") do not get a response even on overflow.
187- if (subRequest .has ("id" )) {
181+ if (!subRequest .isObject ()) {
182+ batchResult .add (buildErrorNode (JsonRpcError .INVALID_REQUEST , "Invalid Request" , null ));
183+ } else if (subRequest .has ("id" )) {
184+ // Notifications (no "id") do not get a response even on overflow.
188185 batchResult .add (buildErrorNode (JsonRpcError .RESPONSE_TOO_LARGE ,
189186 "Response exceeds the limit of " + maxResponseSize + " bytes" ,
190187 subRequest .get ("id" )));
191188 }
192189 continue ;
193190 }
194191
192+ if (!subRequest .isObject ()) {
193+ ObjectNode errNode = buildErrorNode (JsonRpcError .INVALID_REQUEST , "Invalid Request" , null );
194+ byte [] errBytes = MAPPER .writeValueAsBytes (errNode );
195+ int addition = errBytes .length + (!batchResult .isEmpty () ? 1 : 0 );
196+ if (maxResponseSize > 0 && accumulatedSize + addition > maxResponseSize ) {
197+ overflow = true ;
198+ } else {
199+ accumulatedSize += addition ;
200+ }
201+ batchResult .add (errNode );
202+ continue ;
203+ }
204+
195205 byte [] subBody ;
196206 try {
197207 subBody = MAPPER .writeValueAsBytes (subRequest );
0 commit comments