Skip to content

Commit 178681f

Browse files
authored
Merge pull request #333 from IBM/johntimm-master
Issue #329 - skip resource query if pageSize == 0
2 parents 18bef93 + 88e9c87 commit 178681f

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

fhir-persistence-jdbc/src/main/java/com/ibm/fhir/persistence/jdbc/impl/FHIRPersistenceJDBCImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ public MultiResourceResult<Resource> search(FHIRPersistenceContext context, Clas
426426
}
427427
}
428428

429-
// For _summary=count, we don't need to return any resource
430-
if (searchResultCount > 0 && !SummaryValueSet.COUNT.equals(searchContext.getSummaryParameter())) {
429+
// For _summary=count or pageSize == 0, we return only the count
430+
if (searchResultCount > 0 && !SummaryValueSet.COUNT.equals(searchContext.getSummaryParameter()) && searchContext.getPageSize() > 0) {
431431
query = queryBuilder.buildQuery(resourceType, searchContext);
432432

433433
List<String> elements = searchContext.getElementsParameters();
@@ -749,7 +749,8 @@ public <T extends Resource> MultiResourceResult<T> history(FHIRPersistenceContex
749749
}
750750

751751
/**
752-
* Validate pageSize and pageNumber in the FHIRPagingContext and update invalid paging context parameters.
752+
* Validate pageSize and pageNumber in the FHIRPagingContext instance and update
753+
* paging context parameters accordingly.
753754
*
754755
* @param pagingContext
755756
* the FHIRPagingContext instance (FHIRSearchContext or FHIRHistoryContext)

fhir-persistence/src/test/java/com/ibm/fhir/persistence/test/common/AbstractPagingTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,21 @@ public void testHistoryPaging() throws Exception {
228228
assertEquals(outcome.getIssue().get(0).getCode(), IssueType.INVALID);
229229
}
230230

231+
public void testPageSizeEqualsZero() throws Exception {
232+
Map<String, List<String>> queryParameters;
233+
queryParameters = new HashMap<>();
234+
queryParameters.put("_sort", Collections.singletonList("integer"));
235+
queryParameters.put("_tag", Collections.singletonList("pagingTest"));
236+
queryParameters.put("_page", Collections.singletonList("1"));
237+
queryParameters.put("_count", Collections.singletonList("0"));
238+
FHIRSearchContext searchContext = SearchUtil.parseQueryParameters(Basic.class, queryParameters);
239+
searchContext.setLenient(true);
240+
MultiResourceResult<Resource> result = runQueryTest(searchContext, Basic.class, queryParameters, 1);
241+
assertTrue(result.isSuccess());
242+
assertTrue(result.getResource().isEmpty());
243+
assertTrue(result.getOutcome() == null);
244+
}
245+
231246
@Test
232247
public void testInvalidPage0() throws Exception {
233248
Map<String, List<String>> queryParameters;

fhir-server/src/main/java/com/ibm/fhir/server/resources/FHIRResource.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,10 +3875,13 @@ private Boolean isUpdateCreateEnabled() {
38753875

38763876
private Bundle addLinks(FHIRPagingContext context, Bundle bundle, String requestUri) {
38773877
String selfUri = null;
3878+
SummaryValueSet summaryParameter = null;
38783879
Bundle.Builder bundleBuilder = bundle.toBuilder();
38793880
if (context instanceof FHIRSearchContext) {
3881+
FHIRSearchContext searchContext = (FHIRSearchContext) context;
3882+
summaryParameter = searchContext.getSummaryParameter();
38803883
try {
3881-
selfUri = SearchUtil.buildSearchSelfUri(requestUri, (FHIRSearchContext) context);
3884+
selfUri = SearchUtil.buildSearchSelfUri(requestUri, searchContext);
38823885
} catch (Exception e) {
38833886
log.log(Level.WARNING, "Unable to construct self link for search result bundle; using the request URI instead.", e);
38843887
}
@@ -3891,10 +3894,8 @@ private Bundle addLinks(FHIRPagingContext context, Bundle bundle, String request
38913894
Bundle.Link.builder().relation(string("self")).url(Url.of(selfUri)).build();
38923895
bundleBuilder.link(selfLink);
38933896

3894-
// If for search with _summary=count, then don't add previous and next links.
3895-
if (!(context instanceof FHIRSearchContext
3896-
&& ((FHIRSearchContext) context).getSummaryParameter() != null
3897-
&& ((FHIRSearchContext) context).getSummaryParameter().equals(SummaryValueSet.COUNT))) {
3897+
// If for search with _summary=count or pageSize == 0, then don't add previous and next links.
3898+
if (!SummaryValueSet.COUNT.equals(summaryParameter) && context.getPageSize() > 0) {
38983899
int nextPageNumber = context.getPageNumber() + 1;
38993900
if (nextPageNumber <= context.getLastPageNumber()) {
39003901

0 commit comments

Comments
 (0)