2

I have a FHIR Device resource that contains a FHIR DeviceComponent resource. I use the following HAPI FHIR code to 'insert' one resource into the other:

    protected static void insertResourceInResouce(BaseResource resource, BaseResource resourceToInsert)
{
    ContainedDt containedDt = new ContainedDt();
    ArrayList<IResource> resourceList = new ArrayList<IResource>();
    resourceList.add(resourceToInsert);
    containedDt.setContainedResources(resourceList);
    resource.setContained(containedDt);
}

According to the Eclipse debugger the insertion works fine. This resource with its insertion is then added to a bundle. When all the work is done the Eclipse debugger shows the resource with the contained resource properly placed in the bundle. However, when generating a JSON string the contained resources are not there. The encoding operation appears as follows:

return fhirContext.newJsonParser().setPrettyPrint(true)
            .encodeResourceToString(bundle);

Any ideas what I am doing wrong?

Brian Reinhold
  • 2,313
  • 3
  • 27
  • 46

1 Answers1

2

It turns out that one must reference the contained resource from the parent resource using the "#" to prefix the reference. If one does that then the contained resource will be present in the XML and JSON.

Admittedly this requirement makes no sense to me. Why would I include a resource INSIDE another scoping resource if I did not think it was important?

Brian Reinhold
  • 2,313
  • 3
  • 27
  • 46
  • Hi Brian, glad you got this working! Just as an FYI: The reason that this is required is that contained resources are only valid in FHIR if they are referred to from the containing resource (or indirectly from another contained resource which is referred to by the containing resource). HAPI ignores invalid contained resources just to help people avoid relying on invalid behaviour, which may not be portable across FHIR implementations. – James Agnew Jul 06 '15 at 21:33