From f68d905226fbeaf16f05b9549ecacca9f9bb779e Mon Sep 17 00:00:00 2001 From: karneet1212 <112980019+karneet1212@users.noreply.github.com> Date: Tue, 1 Nov 2022 21:25:21 -0400 Subject: [PATCH] 4207-getpagesoffset-set-to-total-number-of-resources-results-in-inconsistent-amount-of-entries-when-requests-are-sent-consecutively (#4209) * Added test * Added solution * Changelog * Changes made based on comments --- ...-when-requests-are-sent-consecutively.yaml | 6 ++++ .../BaseResourceReturningMethodBinding.java | 12 +++---- .../ca/uhn/fhir/rest/server/PagingTest.java | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/4207-getpagesoffset-set-to-total-number-of-resources-results-in-inconsistent-amount-of-entries-when-requests-are-sent-consecutively.yaml diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/4207-getpagesoffset-set-to-total-number-of-resources-results-in-inconsistent-amount-of-entries-when-requests-are-sent-consecutively.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/4207-getpagesoffset-set-to-total-number-of-resources-results-in-inconsistent-amount-of-entries-when-requests-are-sent-consecutively.yaml new file mode 100644 index 00000000000..06391321901 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/4207-getpagesoffset-set-to-total-number-of-resources-results-in-inconsistent-amount-of-entries-when-requests-are-sent-consecutively.yaml @@ -0,0 +1,6 @@ +--- +type: fix +issue: 4207 +title: "Previously to improve performance, if the total number of resources was less than the _getpageoffset, + the results would default to last resource offset. This is especially evident when requests are consecutive + resulting in one entry being displayed in some requests. This issue is now fixed." diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/BaseResourceReturningMethodBinding.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/BaseResourceReturningMethodBinding.java index cb81c181887..a284ea7b1c7 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/BaseResourceReturningMethodBinding.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/BaseResourceReturningMethodBinding.java @@ -369,17 +369,15 @@ public abstract class BaseResourceReturningMethodBinding extends BaseMethodBindi count = result.preferredPageSize(); } - Integer offsetI = RestfulServerUtils.tryToExtractNamedParameter(theRequest, Constants.PARAM_PAGINGOFFSET); - if (offsetI == null || offsetI < 0) { - offsetI = 0; + Integer offset = RestfulServerUtils.tryToExtractNamedParameter(theRequest, Constants.PARAM_PAGINGOFFSET); + if (offset == null || offset < 0) { + offset = 0; } Integer resultSize = result.size(); - int start; + int start = offset; if (resultSize != null) { - start = Math.max(0, Math.min(offsetI, resultSize - 1)); - } else { - start = offsetI; + start = Math.max(0, Math.min(offset, resultSize)); } ResponseEncoding responseEncoding = RestfulServerUtils.determineResponseEncodingNoDefault(theRequest, theServer.getDefaultResponseEncoding()); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PagingTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PagingTest.java index 6f4f1ee87b2..47b0352e606 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PagingTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PagingTest.java @@ -146,6 +146,37 @@ public class PagingTest { } } + @Test() + public void testSendingSameRequestConsecutivelyResultsInSameResponse() throws Exception { + initBundleProvider(10); + myServerExtension.getRestfulServer().registerProvider(new DummyPatientResourceProvider()); + myServerExtension.getRestfulServer().setPagingProvider(pagingProvider); + + when(pagingProvider.canStoreSearchResults()).thenReturn(true); + when(pagingProvider.getDefaultPageSize()).thenReturn(10); + when(pagingProvider.getMaximumPageSize()).thenReturn(50); + when(pagingProvider.storeResultList(any(RequestDetails.class), any(IBundleProvider.class))).thenReturn("ABCD"); + when(pagingProvider.retrieveResultList(any(RequestDetails.class), anyString())).thenReturn(ourBundleProvider); + + String nextLink; + String base = "http://localhost:" + myServerExtension.getPort(); + HttpGet get = new HttpGet(base + "/Patient?_getpagesoffset=10"); + String responseContent; + try (CloseableHttpResponse resp = ourClient.execute(get)) { + assertEquals(200, resp.getStatusLine().getStatusCode()); + responseContent = IOUtils.toString(resp.getEntity().getContent(), Charsets.UTF_8); + + Bundle bundle = ourContext.newJsonParser().parseResource(Bundle.class, responseContent); + assertEquals(0, bundle.getEntry().size()); + } + try (CloseableHttpResponse resp = ourClient.execute(get)) { + assertEquals(200, resp.getStatusLine().getStatusCode()); + responseContent = IOUtils.toString(resp.getEntity().getContent(), Charsets.UTF_8); + + Bundle bundle = ourContext.newJsonParser().parseResource(Bundle.class, responseContent); + assertEquals(0, bundle.getEntry().size()); + } + } private void checkParam(String theUri, String theCheckedParam, String theExpectedValue) { Optional paramValue = URLEncodedUtils.parse(theUri, CHARSET_UTF8).stream() .filter(nameValuePair -> nameValuePair.getName().equals(theCheckedParam))