2533 - Fix Issue with Reference Resources Not Being Returned In Search Queries (#2539)

* 2533 - Initial commit to show how I might fix this Bug but not ready for merge at this time as it causes some other Unit Test failures in FhirResourceDaoR4VersionedReferenceTest.

* Fix up test

* 2533 - Cleaned up Unit Test to make it ready to merge.

Co-authored-by: jamesagnew <jamesagnew@gmail.com>
This commit is contained in:
Kevin Dougan SmileCDR 2021-04-13 05:28:39 -04:00 committed by GitHub
parent 3637dfc60f
commit 129df5dc14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View File

@ -643,8 +643,8 @@ public class SearchBuilder implements ISearchBuilder {
*/
if (resourcePidToVersion != null) {
Long version = resourcePidToVersion.get(next.getResourceId());
resourceId.setVersion(version);
if (version != null && !version.equals(next.getVersion())) {
resourceId.setVersion(version);
IFhirResourceDao<? extends IBaseResource> dao = myDaoRegistry.getResourceDao(resourceType);
next = dao.readEntity(next.getIdDt().withVersion(Long.toString(version)), null);
}

View File

@ -8,14 +8,21 @@ import ca.uhn.fhir.util.BundleBuilder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Condition;
import org.hl7.fhir.r4.model.Encounter;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Observation;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.Task;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -397,6 +404,46 @@ public class FhirResourceDaoR4VersionedReferenceTest extends BaseJpaR4Test {
}
@Test
public void testSearchAndIncludeVersionedReference_WhenOnlyOneVersionExists() {
HashSet<String> refPaths = new HashSet<String>();
refPaths.add("Task.basedOn");
myFhirCtx.getParserOptions().setDontStripVersionsFromReferencesAtPaths(refPaths);
myModelConfig.setRespectVersionsForSearchIncludes(true);
myFhirCtx.getParserOptions().setStripVersionsFromReferences(false);
// Create a Condition
Condition condition = new Condition();
IIdType conditionId = myConditionDao.create(condition).getId().toUnqualified();
// Create a Task which is basedOn that Condition
Task task = new Task();
task.setBasedOn(Arrays.asList(new Reference(conditionId)));
IIdType taskId = myTaskDao.create(task).getId().toUnqualified();
// Search for the Task using an _include=Task.basedOn and make sure we get the Condition resource in the Response
IBundleProvider outcome = myTaskDao.search(SearchParameterMap.newSynchronous().addInclude(Task.INCLUDE_BASED_ON));
assertEquals(2, outcome.size());
List<IBaseResource> resources = outcome.getResources(0, 2);
assertEquals(2, resources.size(), resources.stream().map(t->t.getIdElement().toUnqualified().getValue()).collect(Collectors.joining(", ")));
assertEquals(taskId.getValue(), resources.get(0).getIdElement().getValue());
assertEquals(conditionId.getValue(), ((Task)resources.get(0)).getBasedOn().get(0).getReference());
assertEquals(conditionId.withVersion("1").getValue(), resources.get(1).getIdElement().getValue());
// Now, update the Condition to generate another version of it
condition.setRecordedDate(new Date(System.currentTimeMillis()));
String conditionIdString = myConditionDao.update(condition).getId().getValue();
// Search for the Task again and make sure that we get the original version of the Condition resource in the Response
outcome = myTaskDao.search(SearchParameterMap.newSynchronous().addInclude(Task.INCLUDE_BASED_ON));
assertEquals(2, outcome.size());
resources = outcome.getResources(0, 2);
assertEquals(2, resources.size());
assertEquals(taskId.getValue(), resources.get(0).getIdElement().getValue());
assertEquals(conditionId.getValue(), ((Task)resources.get(0)).getBasedOn().get(0).getReference());
assertEquals(conditionId.withVersion("1").getValue(), resources.get(1).getIdElement().getValue());
}
@Test
public void testSearchAndIncludeUnersionedReference_Asynchronous() {