#1817: fix problem with missing resource references when extensions are used (#1848)

Co-authored-by: Christian Ohr <christian.ohr@icw.de>
This commit is contained in:
christian ohr 2020-07-21 11:36:25 +02:00 committed by GitHub
parent 05735900fd
commit 1c27726055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 184 additions and 0 deletions

View File

@ -1067,9 +1067,19 @@ class ParserState<T> {
* At some point it would be good to write code which can present a view * At some point it would be good to write code which can present a view
* of one type backed by another type and use that. * of one type backed by another type and use that.
*/ */
FhirTerser t = myContext.newTerser();
// Clean up the cached resources
myGlobalResources.remove(myInstance);
myGlobalReferences.removeAll(t.getAllPopulatedChildElementsOfType(myInstance, IBaseReference.class));
IParser parser = myContext.newJsonParser(); IParser parser = myContext.newJsonParser();
String asString = parser.encodeResourceToString(myInstance); String asString = parser.encodeResourceToString(myInstance);
myInstance = parser.parseResource(wantedProfileType, asString); myInstance = parser.parseResource(wantedProfileType, asString);
// Add newly created instance
myGlobalResources.add(myInstance);
myGlobalReferences.addAll(t.getAllPopulatedChildElementsOfType(myInstance, IBaseReference.class));
} }
} }
} }

View File

@ -0,0 +1,31 @@
package ca.uhn.fhir.parser;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import org.hl7.fhir.dstu3.model.CodeType;
import org.hl7.fhir.dstu3.model.Patient;
@ResourceDef(
name = "Patient",
profile = "http://acme.org//StructureDefinition/patient-with-eyes"
)
public class ExtendedPatient extends Patient {
@Child(name = "eyeColour")
@Extension(url = "http://acme.org/#extpt", definedLocally = false, isModifier = false)
private CodeType myEyeColour;
public CodeType getEyeColour() {
if (myEyeColour == null) {
myEyeColour = new CodeType();
}
return myEyeColour;
}
public void setEyeColour(CodeType theEyeColour) {
myEyeColour = theEyeColour;
}
}

View File

@ -0,0 +1,56 @@
package ca.uhn.fhir.parser;
import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.CodeType;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Reference;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
public class ExtendedPatientTest {
@Test
public void testBundleReferences() {
FhirContext fhirContext = FhirContext.forDstu3();
fhirContext.setDefaultTypeForProfile("http://acme.org//StructureDefinition/patient-with-eyes", ExtendedPatient.class);
ExtendedPatient homer = new ExtendedPatient();
homer.setId("homer");
homer.addName().setFamily("Simpson").addGiven("Homer");
ExtendedPatient marge = new ExtendedPatient();
marge.setId("marge");
marge.addName().setFamily("Simpson").addGiven("Marge");
marge.setEyeColour(new CodeType("blue"));
marge.getLink().add(new Patient.PatientLinkComponent()
.setType(Patient.LinkType.REFER)
.setOther(new Reference("Patient/homer")));
Bundle bundle = new Bundle()
.addEntry(new Bundle.BundleEntryComponent()
.setFullUrl("http://acme.org/Patient/homer").setResource(homer)
.setSearch(new Bundle.BundleEntrySearchComponent()
.setMode(Bundle.SearchEntryMode.INCLUDE)))
.addEntry(new Bundle.BundleEntryComponent()
.setFullUrl("http://acme.org/Patient/marge").setResource(marge)
.setSearch(new Bundle.BundleEntrySearchComponent()));
IParser p = fhirContext.newXmlParser().setPrettyPrint(true);
String encoded = p.encodeResourceToString(bundle);
Bundle parsedBundle = p.parseResource(Bundle.class, encoded);
ExtendedPatient parsedHomer = (ExtendedPatient)parsedBundle.getEntry().get(0).getResource();
ExtendedPatient parsedMarge = (ExtendedPatient)parsedBundle.getEntry().get(1).getResource();
IBaseResource referencedHomer = parsedMarge.getLinkFirstRep().getOther().getResource();
assertNotNull(referencedHomer);
assertSame(parsedHomer, referencedHomer);
}
}

View File

@ -0,0 +1,31 @@
package ca.uhn.fhir.parser;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import org.hl7.fhir.r4.model.CodeType;
import org.hl7.fhir.r4.model.Patient;
@ResourceDef(
name = "Patient",
profile = "http://acme.org//StructureDefinition/patient-with-eyes"
)
public class ExtendedPatient extends Patient {
@Child(name = "eyeColour")
@Extension(url = "http://acme.org/#extpt", definedLocally = false, isModifier = false)
private CodeType myEyeColour;
public CodeType getEyeColour() {
if (myEyeColour == null) {
myEyeColour = new CodeType();
}
return myEyeColour;
}
public void setEyeColour(CodeType theEyeColour) {
myEyeColour = theEyeColour;
}
}

View File

@ -0,0 +1,56 @@
package ca.uhn.fhir.parser;
import ca.uhn.fhir.context.FhirContext;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.CodeType;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
public class ExtendedPatientTest {
@Test
public void testBundleReferences() {
FhirContext fhirContext = FhirContext.forR4();
fhirContext.setDefaultTypeForProfile("http://acme.org//StructureDefinition/patient-with-eyes", ExtendedPatient.class);
ExtendedPatient homer = new ExtendedPatient();
homer.setId("homer");
homer.addName().setFamily("Simpson").addGiven("Homer");
ExtendedPatient marge = new ExtendedPatient();
marge.setId("marge");
marge.addName().setFamily("Simpson").addGiven("Marge");
marge.setEyeColour(new CodeType("blue"));
marge.getLink().add(new Patient.PatientLinkComponent()
.setType(Patient.LinkType.REFER)
.setOther(new Reference("Patient/homer")));
Bundle bundle = new Bundle()
.addEntry(new Bundle.BundleEntryComponent()
.setFullUrl("http://acme.org/Patient/homer").setResource(homer)
.setSearch(new Bundle.BundleEntrySearchComponent()
.setMode(Bundle.SearchEntryMode.INCLUDE)))
.addEntry(new Bundle.BundleEntryComponent()
.setFullUrl("http://acme.org/Patient/marge").setResource(marge)
.setSearch(new Bundle.BundleEntrySearchComponent()));
IParser p = fhirContext.newXmlParser().setPrettyPrint(true);
String encoded = p.encodeResourceToString(bundle);
Bundle parsedBundle = p.parseResource(Bundle.class, encoded);
ExtendedPatient parsedHomer = (ExtendedPatient)parsedBundle.getEntry().get(0).getResource();
ExtendedPatient parsedMarge = (ExtendedPatient)parsedBundle.getEntry().get(1).getResource();
IBaseResource referencedHomer = parsedMarge.getLinkFirstRep().getOther().getResource();
assertNotNull(referencedHomer);
assertSame(parsedHomer, referencedHomer);
}
}