44import static io .neonbee .endpoint .odatav4 .internal .olingo .processor .EntityProcessor .findEntityByKeyPredicates ;
55import static io .neonbee .endpoint .odatav4 .internal .olingo .processor .NavigationPropertyHelper .chooseEntitySet ;
66import static io .neonbee .endpoint .odatav4 .internal .olingo .processor .NavigationPropertyHelper .fetchNavigationTargetEntities ;
7+ import static io .neonbee .endpoint .odatav4 .internal .olingo .processor .ProcessorHelper .ODATA_COUNT_SIZE_KEY ;
78import static io .neonbee .endpoint .odatav4 .internal .olingo .processor .ProcessorHelper .ODATA_EXPAND_KEY ;
89import static io .neonbee .endpoint .odatav4 .internal .olingo .processor .ProcessorHelper .ODATA_FILTER_KEY ;
910import static io .neonbee .endpoint .odatav4 .internal .olingo .processor .ProcessorHelper .ODATA_ORDER_BY_KEY ;
@@ -120,7 +121,7 @@ public void readEntityCollection(ODataRequest request, ODataResponse response, U
120121 .orElse (Boolean .FALSE );
121122 List <Entity > resultEntityList = filterExecuted ? ew .getEntities ()
122123 : applyFilterQueryOption (uriInfo .getFilterOption (), ew .getEntities ());
123- applyCountOption (uriInfo .getCountOption (), resultEntityList , entityCollection );
124+ applyCountOption (uriInfo .getCountOption (), resultEntityList , entityCollection , routingContext );
124125 if (!resultEntityList .isEmpty ()) {
125126 boolean orderByExecuted =
126127 ofNullable (routingContext .<Boolean >get (RESPONSE_HEADER_PREFIX + ODATA_ORDER_BY_KEY ))
@@ -144,7 +145,7 @@ public void readEntityCollection(ODataRequest request, ODataResponse response, U
144145 } else {
145146 responsePromise .complete (resultEntityList );
146147 }
147- } catch (ODataException e ) {
148+ } catch (ODataException | ClassCastException e ) {
148149 processPromise .fail (e );
149150 }
150151 } else {
@@ -183,14 +184,16 @@ public void readEntityCollection(ODataRequest request, ODataResponse response, U
183184 }
184185
185186 private void applyCountOption (CountOption countOption , List <Entity > filteredEntities ,
186- EntityCollection entityCollection ) {
187+ EntityCollection entityCollection , RoutingContext routingContext ) {
187188 // Apply $count system query option. The $count system query option with a value of true
188189 // specifies that the total count of items within a collection matching the request be returned
189190 // along with the result. The $count system query option ignores any $top, $skip, or $expand query
190191 // options, and returns the total count of results across all pages including only those results
191192 // matching any specified $filter and $search.
192193 if ((countOption != null ) && countOption .getValue ()) {
193- entityCollection .setCount (filteredEntities .size ());
194+ int countSize = ofNullable (routingContext .<Integer >get (RESPONSE_HEADER_PREFIX + ODATA_COUNT_SIZE_KEY ))
195+ .orElse (filteredEntities .size ());
196+ entityCollection .setCount (countSize );
194197 }
195198 }
196199
@@ -321,15 +324,19 @@ public void countEntityCollection(ODataRequest request, ODataResponse response,
321324 * Content negotiation using the Accept request header or the $format system query option is not allowed
322325 * with the path segment /$count.
323326 */
324- List <Entity > resultEntityList = applyFilterQueryOption (uriInfo .getFilterOption (), ew .getEntities ());
327+ Integer countSize =
328+ routingContext .<Integer >get (RESPONSE_HEADER_PREFIX + ODATA_COUNT_SIZE_KEY );
329+ if (countSize == null ) {
330+ countSize = applyFilterQueryOption (uriInfo .getFilterOption (), ew .getEntities ()).size ();
331+ }
325332
326333 ByteArrayInputStream serializerContent = new ByteArrayInputStream (
327- String .valueOf (resultEntityList . size () ).getBytes (StandardCharsets .UTF_8 ));
334+ String .valueOf (countSize ).getBytes (StandardCharsets .UTF_8 ));
328335 response .setContent (serializerContent );
329336 response .setHeader (HttpHeader .CONTENT_TYPE , ContentType .TEXT_PLAIN .toContentTypeString ());
330337 response .setStatusCode (HttpStatusCode .OK .getStatusCode ());
331338 processPromise .complete ();
332- } catch (ODataException e ) {
339+ } catch (ODataException | ClassCastException e ) {
333340 processPromise .fail (e );
334341 }
335342 });
0 commit comments