From 619a21032b572010d7b06a7809c26ff6a39d2a42 Mon Sep 17 00:00:00 2001 From: Anthony Sute Date: Tue, 5 Nov 2019 17:46:33 -0500 Subject: [PATCH] (1) Fixes to address _filter-based _id retrievals not being restricted to the specified resource type (2) Import fix for FhirServerConfig.java in hapi-fhir-jpaserver-example --- .../ca/uhn/fhir/jpa/dao/SearchBuilder.java | 13 +++++-- .../dao/r4/FhirResourceDaoR4FilterTest.java | 35 +++++++++++++++++++ .../demo/elasticsearch/FhirServerConfig.java | 2 +- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java index 916e855c181..ffcbfb9c076 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java @@ -834,13 +834,18 @@ public class SearchBuilder implements ISearchBuilder { SearchFilterParser.CompareOperation operation = defaultIfNull(theOperation, SearchFilterParser.CompareOperation.eq); assert operation == SearchFilterParser.CompareOperation.eq || operation == SearchFilterParser.CompareOperation.ne; + List codePredicates = new ArrayList<>(); switch (operation) { default: case eq: - nextPredicate = theRoot.get("myId").as(Long.class).in(allOrPids); + codePredicates.add(theRoot.get("myId").as(Long.class).in(allOrPids)); + codePredicates.add(myBuilder.equal(myResourceTableRoot.get("myResourceType"), theResourceName)); + nextPredicate = myBuilder.and(toArray(codePredicates)); break; case ne: - nextPredicate = theRoot.get("myId").as(Long.class).in(allOrPids).not(); + codePredicates.add(theRoot.get("myId").as(Long.class).in(allOrPids).not()); + codePredicates.add(myBuilder.equal(myResourceTableRoot.get("myResourceType"), theResourceName)); + nextPredicate = myBuilder.and(toArray(codePredicates)); break; } @@ -2718,7 +2723,9 @@ public class SearchBuilder implements ISearchBuilder { RuntimeSearchParam searchParam = mySearchParamRegistry.getActiveSearchParam(theResourceName, theFilter.getParamPath().getName()); - if (searchParam.getName().equals(IAnyResource.SP_RES_ID)) { + if (searchParam == null) { + throw new InvalidRequestException("Invalid search parameter specified, " + theFilter.getParamPath().getName() + ", for resource type " + theResourceName); + } else if (searchParam.getName().equals(IAnyResource.SP_RES_ID)) { if (searchParam.getParamType() == RestSearchParameterTypeEnum.TOKEN) { TokenParam param = new TokenParam(); param.setValueAsQueryToken(null, diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4FilterTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4FilterTest.java index 75778d1902d..a7d3b665bb8 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4FilterTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4FilterTest.java @@ -169,6 +169,41 @@ public class FhirResourceDaoR4FilterTest extends BaseJpaR4Test { } } + @Test + public void testRetrieveDifferentTypeEq() { + + Patient p = new Patient(); + p.addName().setFamily("Smith").addGiven("John"); + p.setActive(true); + String id1 = myPatientDao.create(p).getId().toUnqualifiedVersionless().getValue(); + String idVal = id1.split("/")[1]; + + SearchParameterMap map; + List found; + + map = new SearchParameterMap(); + map.setLoadSynchronous(true); + map.add(Constants.PARAM_FILTER, new StringParam(String.format("status eq active or _id eq %s", + idVal))); + found = toUnqualifiedVersionlessIdValues(myEncounterDao.search(map)); + assertThat(found, Matchers.empty()); + + map = new SearchParameterMap(); + map.setLoadSynchronous(true); + map.add(Constants.PARAM_FILTER, new StringParam(String.format("_id eq %s", + idVal))); + found = toUnqualifiedVersionlessIdValues(myEncounterDao.search(map)); + assertThat(found, Matchers.empty()); + + map = new SearchParameterMap(); + map.setLoadSynchronous(true); + map.add(Constants.PARAM_FILTER, new StringParam(String.format("_id eq %s", + idVal))); + found = toUnqualifiedVersionlessIdValues(myPatientDao.search(map)); + assertThat(found, containsInAnyOrder(id1)); + + } + @AfterClass public static void afterClassClearContext() { TestUtil.clearAllStaticFieldsForUnitTest(); diff --git a/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/elasticsearch/FhirServerConfig.java b/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/elasticsearch/FhirServerConfig.java index 902bd9ab74a..363f8708e0e 100644 --- a/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/elasticsearch/FhirServerConfig.java +++ b/hapi-fhir-jpaserver-example/src/main/java/ca/uhn/fhir/jpa/demo/elasticsearch/FhirServerConfig.java @@ -6,7 +6,7 @@ import javax.sql.DataSource; import ca.uhn.fhir.jpa.config.BaseJavaConfigDstu3; import ca.uhn.fhir.jpa.dao.DaoConfig; -import ca.uhn.fhir.jpa.search.elastic.ElasticsearchMappingProvider; +import ca.uhn.fhir.jpa.search.ElasticsearchMappingProvider; import ca.uhn.fhir.jpa.util.SubscriptionsRequireManualActivationInterceptorDstu3; import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; import ca.uhn.fhir.rest.server.interceptor.ResponseHighlighterInterceptor;