diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3261-revinclude-match-correct-type.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3261-revinclude-match-correct-type.yaml new file mode 100644 index 00000000000..1bfc16475c0 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_0_0/3261-revinclude-match-correct-type.yaml @@ -0,0 +1,6 @@ +--- +type: fix +issue: 3261 +title: "When performing a search with a `_revinclude`. the results sometimes incorrectly included + resources that were reverse included by other search parameters with the same name. Thanks to + GitHub user @vivektk84 for reporting and to Jean-Francois Briere for proposing a fix." diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/builder/SearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/builder/SearchBuilder.java index 404f399348f..d40b76d21c8 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/builder/SearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/builder/SearchBuilder.java @@ -997,7 +997,7 @@ public class SearchBuilder implements ISearchBuilder { continue; } - paths = param.getPathsSplit(); + paths = param.getPathsSplitForResourceType(resType); String targetResourceType = defaultString(nextInclude.getParamTargetType(), null); for (String nextPath : paths) { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoFtTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoFtTest.java index a48ff11ab6e..17d462cf291 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoFtTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchNoFtTest.java @@ -1964,6 +1964,29 @@ public class FhirResourceDaoR4SearchNoFtTest extends BaseJpaR4Test { assertThat(ids, containsInAnyOrder(obsId, ptId, encId)); } + @Test + public void testSearchWithRevIncludeDoesntSelectWrongResourcesWithSameSpName() { + Patient pt = new Patient(); + pt.setActive(true); + IIdType ptId = myPatientDao.create(pt, mySrd).getId().toUnqualifiedVersionless(); + + Encounter enc = new Encounter(); + enc.setStatus(Encounter.EncounterStatus.ARRIVED); + enc.getSubject().setReference(ptId.getValue()); + IIdType encId = myEncounterDao.create(enc, mySrd).getId().toUnqualifiedVersionless(); + + Observation obs = new Observation(); + obs.getSubject().setReference(ptId.getValue()); + myObservationDao.create(obs, mySrd).getId().toUnqualifiedVersionless(); + + SearchParameterMap map = new SearchParameterMap() + .addRevInclude(Encounter.INCLUDE_PATIENT); + IBundleProvider outcome = myPatientDao.search(map); + List ids = toUnqualifiedVersionlessIds(outcome); + assertThat(ids, contains(ptId, encId)); + + } + @Test public void testSearchWithRevIncludeStarQualified() {