diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java index a213fb5f76e..53358593948 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java @@ -272,7 +272,7 @@ public class RestfulServerUtils { boolean first = true; Map parameters = theRequest.getParameters(); for (String nextParamName : new TreeSet<>(parameters.keySet())) { - if (excludedParameterNames != null && !excludedParameterNames.contains(nextParamName)) { + if (excludedParameterNames == null || !excludedParameterNames.contains(nextParamName)) { for (String nextParamValue : parameters.get(nextParamName)) { if (first) { b.append('?'); diff --git a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/RestfulServerUtilsTest.java b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/RestfulServerUtilsTest.java index 6e22f74fb6c..1d215bf29aa 100644 --- a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/RestfulServerUtilsTest.java +++ b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/RestfulServerUtilsTest.java @@ -3,8 +3,19 @@ package ca.uhn.fhir.rest.server; import ca.uhn.fhir.rest.api.PreferHandlingEnum; import ca.uhn.fhir.rest.api.PreferHeader; import ca.uhn.fhir.rest.api.PreferReturnEnum; +import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static ca.uhn.fhir.rest.api.RequestTypeEnum.GET; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*; public class RestfulServerUtilsTest{ @@ -53,4 +64,37 @@ public class RestfulServerUtilsTest{ assertFalse(header.getRespondAsync()); assertEquals(PreferHandlingEnum.LENIENT, header.getHanding()); } + + @Test + public void testCreateSelfLinks() { + //Given + String baseUrl = "http://localhost:8000"; + Map parameters = new HashMap<>(); + parameters.put("_format", new String[]{"json"}); + parameters.put("_count", new String[]{"10"}); + parameters.put("_offset", new String[]{"100"}); + List paramsToRemove = Arrays.asList("_count", "_offset"); + + ServletRequestDetails servletRequestDetails = new ServletRequestDetails(); + servletRequestDetails.setFhirServerBase("http://localhost:8000"); + servletRequestDetails.setRequestPath("$my-operation"); + servletRequestDetails.setRequestType(GET); + servletRequestDetails.setParameters(parameters); + + //When + String linkSelf = RestfulServerUtils.createLinkSelf(baseUrl, servletRequestDetails); + //Then + assertThat(linkSelf, is(containsString("http://localhost:8000/$my-operation?"))); + assertThat(linkSelf, is(containsString("_format=json"))); + assertThat(linkSelf, is(containsString("_count=10"))); + assertThat(linkSelf, is(containsString("_offset=100"))); + + + //When + String linkSelfWithoutGivenParameters = RestfulServerUtils.createLinkSelfWithoutGivenParameters(baseUrl, servletRequestDetails, paramsToRemove); + //Then + assertThat(linkSelfWithoutGivenParameters, is(containsString("http://localhost:8000/$my-operation?"))); + assertThat(linkSelfWithoutGivenParameters, is(containsString("_format=json"))); + + } }