diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java index c66a8663312..8ab09545a6d 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java @@ -179,9 +179,16 @@ public class FhirTerser { switch (theDefinition.getChildType()) { case PRIMITIVE_XHTML: case PRIMITIVE_DATATYPE: - case RESOURCE_REF: // These are primitive types break; + case RESOURCE_REF: + ResourceReferenceDt resRefDt = (ResourceReferenceDt)theElement; + if (resRefDt.getReference().getValue() == null && resRefDt.getResource() != null) { + IResource theResource = resRefDt.getResource(); + BaseRuntimeElementCompositeDefinition def = myContext.getResourceDefinition(theResource); + visit(theResource, null, def, theCallback); + } + break; case RESOURCE_BLOCK: case COMPOSITE_DATATYPE: case RESOURCE: { diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/ContainedResourceEncodingTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/ContainedResourceEncodingTest.java index ef0fde36220..ca8c2cb2ce8 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/ContainedResourceEncodingTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/ContainedResourceEncodingTest.java @@ -12,13 +12,20 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + + import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.Bundle; +import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.composite.CodeableConceptDt; +import ca.uhn.fhir.model.dstu.composite.CodingDt; import ca.uhn.fhir.model.dstu.composite.HumanNameDt; import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; import ca.uhn.fhir.model.dstu.resource.Composition; import ca.uhn.fhir.model.dstu.resource.Composition.Section; import ca.uhn.fhir.model.dstu.resource.Condition; +import ca.uhn.fhir.model.dstu.resource.DiagnosticReport; +import ca.uhn.fhir.model.dstu.resource.Observation; import ca.uhn.fhir.model.dstu.resource.Patient; import ca.uhn.fhir.model.dstu.resource.Practitioner; import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum; @@ -26,6 +33,7 @@ import ca.uhn.fhir.model.dstu.valueset.ConditionStatusEnum; import ca.uhn.fhir.model.dstu.valueset.NameUseEnum; import ca.uhn.fhir.model.dstu.valueset.PractitionerRoleEnum; import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.RestfulServer; /** * Initially contributed by Alexander Kley for bug #29 @@ -155,5 +163,84 @@ public class ContainedResourceEncodingTest { Assert.assertArrayEquals(expectedCompXml.getBytes(), actualCompXml.getBytes()); } + + + @Test + public void testBundleWithContained() { + + DiagnosticReport dr = new DiagnosticReport(); + dr.setId(new IdDt("DiagnosticReport","123")); + + Observation observation = new Observation(); + + CodeableConceptDt obsName = new CodeableConceptDt(); + obsName.setText("name"); + observation.setName(obsName); + + ResourceReferenceDt result = dr.addResult(); + result.setResource(observation); + + ArrayList performers = new ArrayList(); + ResourceReferenceDt performer = new ResourceReferenceDt(); + + Practitioner p = new Practitioner(); + p.setId(new IdDt(UUID.randomUUID().toString())); + p.addIdentifier().setSystem("DoctorID").setValue("4711"); + p.addRole(PractitionerRoleEnum.DOCTOR); + p.setName(new HumanNameDt().addFamily("Mueller").addGiven("Klaus").addPrefix("Prof. Dr.")); + + performer.setResource(p); + performers.add(performer); + observation.setPerformer(performers); + + + List list = new ArrayList(); + list.add(dr); + Bundle bundle = RestfulServer.createBundleFromResourceList(new FhirContext(), null, list, null, null, 0); + + IParser parser = this.ctx.newXmlParser().setPrettyPrint(true); + String xml = parser.encodeBundleToString(bundle); + Assert.assertTrue(xml.contains("Mueller")); + + } + + @Test + public void testBundleWithContainedWithNoIdDt() { + + DiagnosticReport dr = new DiagnosticReport(); + dr.setId(new IdDt("DiagnosticReport","123")); + + Observation observation = new Observation(); + + CodeableConceptDt obsName = new CodeableConceptDt(); + obsName.setText("name"); + observation.setName(obsName); + + ResourceReferenceDt result = dr.addResult(); + result.setResource(observation); + + ArrayList performers = new ArrayList(); + ResourceReferenceDt performer = new ResourceReferenceDt(); + + Practitioner p = new Practitioner(); + // no idDt on practitioner p + p.addIdentifier().setSystem("DoctorID").setValue("4711"); + p.addRole(PractitionerRoleEnum.DOCTOR); + p.setName(new HumanNameDt().addFamily("Mueller").addGiven("Klaus").addPrefix("Prof. Dr.")); + + performer.setResource(p); + performers.add(performer); + observation.setPerformer(performers); + + + List list = new ArrayList(); + list.add(dr); + Bundle bundle = RestfulServer.createBundleFromResourceList(new FhirContext(), null, list, null, null, 0); + + IParser parser = this.ctx.newXmlParser().setPrettyPrint(true); + String xml = parser.encodeBundleToString(bundle); + Assert.assertTrue(xml.contains("Mueller")); + + } }