reinstate near param count validation

This commit is contained in:
Ken Stevens 2020-02-02 21:21:37 -05:00
parent c10d44327c
commit 63f2f38689
3 changed files with 33 additions and 18 deletions

View File

@ -53,7 +53,6 @@ import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.api.server.IPreResourceAccessDetails;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.QuantityParam;
import ca.uhn.fhir.rest.param.ReferenceParam;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
@ -162,7 +161,9 @@ public class SearchBuilder implements ISearchBuilder {
// Remove any empty parameters
theParams.clean();
setLocationDistance(theParams);
if (myResourceType == Location.class) {
theParams.setLocationDistance();
}
/*
* Check if there is a unique key associated with the set
@ -185,21 +186,6 @@ public class SearchBuilder implements ISearchBuilder {
}
private void setLocationDistance(SearchParameterMap theParams) {
if (myResourceType == Location.class && theParams.containsKey(Location.SP_NEAR_DISTANCE)) {
List<List<IQueryParameterType>> paramList = theParams.get(Location.SP_NEAR_DISTANCE);
// Set nearDistanceParam on the SearchParameterMap so it's available to the near predicate
paramList.stream()
.flatMap(List::stream)
.findFirst()
.map(QuantityParam.class::cast)
.ifPresent(theParams::setNearDistanceParam);
// Need to remove near-distance or it we'll get a hashcode predicate for it
theParams.remove(Location.SP_NEAR_DISTANCE);
}
}
@Override
public Iterator<Long> createCountQuery(SearchParameterMap theParams, String theSearchUuid, RequestDetails theRequest) {
init(theParams, theSearchUuid);

View File

@ -58,6 +58,7 @@ public class MatchUrlServiceTest extends BaseJpaTest {
Location.SP_NEAR + "=1000.0:2000.0" +
"&" +
Location.SP_NEAR_DISTANCE + "=" + kmDistance + "|http://unitsofmeasure.org|km", ourCtx.getResourceDefinition("Location"));
map.setLocationDistance();
QuantityParam nearDistanceParam = map.getNearDistanceParam();
assertEquals(1, map.size());
@ -74,6 +75,8 @@ public class MatchUrlServiceTest extends BaseJpaTest {
"&" +
Location.SP_NEAR_DISTANCE + "=2|http://unitsofmeasure.org|km",
ourCtx.getResourceDefinition("Location"));
map.setLocationDistance();
fail();
} catch (IllegalArgumentException e) {
assertEquals("Only one " + Location.SP_NEAR_DISTANCE + " parameter may be present", e.getMessage());
@ -89,7 +92,8 @@ public class MatchUrlServiceTest extends BaseJpaTest {
"," +
"2|http://unitsofmeasure.org|km",
ourCtx.getResourceDefinition("Location"));
map.setLoadSynchronous(true);
map.setLocationDistance();
fail();
} catch (IllegalArgumentException e) {
assertEquals("Only one " + Location.SP_NEAR_DISTANCE + " parameter may be present", e.getMessage());

View File

@ -15,6 +15,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hl7.fhir.dstu3.model.Location;
import java.io.Serializable;
import java.util.*;
@ -504,6 +505,30 @@ public class SearchParameterMap implements Serializable {
return myNearDistanceParam;
}
public void setLocationDistance() {
if (containsKey(Location.SP_NEAR_DISTANCE)) {
List<List<IQueryParameterType>> paramAndList = get(Location.SP_NEAR_DISTANCE);
if (paramAndList.isEmpty()) {
return;
}
if (paramAndList.size() > 1) {
throw new IllegalArgumentException("Only one " + ca.uhn.fhir.model.dstu2.resource.Location.SP_NEAR_DISTANCE + " parameter may be present");
}
List<IQueryParameterType> paramOrList = paramAndList.get(0);
if (paramOrList.isEmpty()) {
return;
}
if (paramOrList.size() > 1) {
throw new IllegalArgumentException("Only one " + ca.uhn.fhir.model.dstu2.resource.Location.SP_NEAR_DISTANCE + " parameter may be present");
}
setNearDistanceParam((QuantityParam) paramOrList.get(0));
// Need to remove near-distance or it we'll get a hashcode predicate for it
remove(Location.SP_NEAR_DISTANCE);
}
}
public enum EverythingModeEnum {
/*
* Don't reorder! We rely on the ordinals