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 30a7787cada..1a3dd2fd764 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 @@ -447,8 +447,12 @@ public class SearchBuilder implements ISearchBuilder { } if (resourceTypes.isEmpty()) { - RuntimeResourceDefinition resourceDef = myContext.getResourceDefinition(myResourceType); - String paramPath = myCallingDao.getSearchParamByName(resourceDef, theParamName).getPath(); + RuntimeResourceDefinition resourceDef = myContext.getResourceDefinition(theResourceName); + RuntimeSearchParam searchParamByName = myCallingDao.getSearchParamByName(resourceDef, theParamName); + if (searchParamByName == null) { + throw new InternalErrorException("Could not find parameter " + theParamName ); + } + String paramPath = searchParamByName.getPath(); if (paramPath.endsWith(".as(Reference)")) { paramPath = paramPath.substring(0, paramPath.length() - ".as(Reference)".length()) + "Reference"; } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchCustomSearchParamTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchCustomSearchParamTest.java index da3756c9f58..7c454b0d560 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchCustomSearchParamTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchCustomSearchParamTest.java @@ -264,7 +264,11 @@ public class FhirResourceDaoDstu3SearchCustomSearchParamTest extends BaseJpaDstu Patient p2 = new Patient(); p2.addName().setFamily("P2"); p2.addExtension().setUrl("http://acme.org/sibling").setValue(new Reference(p1id)); + IIdType p2id = myPatientDao.create(p2).getId().toUnqualifiedVersionless(); + Appointment app = new Appointment(); + app.addParticipant().getActor().setReference(p2id.getValue()); + IIdType appid = myAppointmentDao.create(app).getId().toUnqualifiedVersionless(); SearchParameterMap map; IBundleProvider results; @@ -284,6 +288,14 @@ public class FhirResourceDaoDstu3SearchCustomSearchParamTest extends BaseJpaDstu foundResources = toUnqualifiedVersionlessIdValues(results); assertThat(foundResources, contains(p2id.getValue())); + // Search by two level chain + map = new SearchParameterMap(); + map.add("patient", new ReferenceParam("sibling.name", "P1")); + results = myAppointmentDao.search(map); + foundResources = toUnqualifiedVersionlessIdValues(results); + assertThat(foundResources, containsInAnyOrder(appid.getValue())); + + } @Test