Skip to content

Commit

Permalink
fix: calculation of system query count
Browse files Browse the repository at this point in the history
Mistakenly it was assumed that also filters are ignored when calculating
the system query count.
  • Loading branch information
pk-work committed Apr 7, 2021
1 parent d90c40f commit 35bd1dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public void readEntityCollection(ODataRequest request, ODataResponse response, U
// Fetch the data from backend
fetchEntities(request, edmEntityType, ew -> {
try {
applyCountOption(uriInfo.getCountOption(), ew.getEntities(), entityCollection);
List<Entity> resultEntityList = applyFilterQueryOption(uriInfo.getFilterOption(), ew.getEntities());
applyCountOption(uriInfo.getCountOption(), resultEntityList, entityCollection);
if (!resultEntityList.isEmpty()) {
applyOrderByQueryOption(uriInfo.getOrderByOption(), resultEntityList);
resultEntityList = applySkipQueryOption(uriInfo.getSkipOption(), resultEntityList);
Expand Down Expand Up @@ -161,15 +161,15 @@ private void fetchEntities(ODataRequest request, EdmEntityType edmEntityType,
new DataContextImpl(routingContext)).onFailure(getProcessPromise()::fail).onSuccess(resultHandler);
}

private void applyCountOption(CountOption countOption, List<Entity> unfilteredEntities,
private void applyCountOption(CountOption countOption, List<Entity> filteredEntities,
EntityCollection entityCollection) {
// Apply $count system query option. The $count system query option with a value of true
// specifies that the total count of items within a collection matching the request be returned
// along with the result. The $count system query option ignores any $top, $skip, or $expand query
// options, and returns the total count of results across all pages including only those results
// matching any specified $filter and $search.
if ((countOption != null) && countOption.getValue()) {
entityCollection.setCount(unfilteredEntities.size());
entityCollection.setCount(filteredEntities.size());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ void existingEntitiesWithInlineCountTest(VertxTestContext testContext) {
.onComplete(testContext.succeedingThenComplete());
}

@Test
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
@DisplayName("Respond with 200 if the service is existing and has test entities including correct inline count with applied filters")
void existingEntitiesWithInlineCountAndFilterTest(VertxTestContext testContext) {
List<JsonObject> expectedEntities = List.of(EXPECTED_ENTITY_DATA_2, EXPECTED_ENTITY_DATA_4);
Map<String, String> query = Map.of("$filter", "KeyPropertyString in ('id.3', 'id-1')", "$count", "true");

Future<HttpResponse<Buffer>> response = requestOData(new ODataRequest(TEST_ENTITY_SET_FQN).setQuery(query));
assertOData(response, body -> assertThat(body.toJsonObject().getMap()).containsAtLeast("@odata.count", 2),
testContext).compose(v -> assertODataEntitySetContainsExactly(response, expectedEntities, testContext))
.onComplete(testContext.succeedingThenComplete());
}

@Test
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
@DisplayName("Respond with 200 and the correct count of entities")
Expand Down

0 comments on commit 35bd1dc

Please sign in to comment.