Brian Reinhold
Brian Reinhold

Reputation: 2455

HAPI FHIR Json encoder not handling contained resources

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?

Upvotes: 2

Views: 830

Answers (1)

Brian Reinhold
Brian Reinhold

Reputation: 2455

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?

Upvotes: 2

Related Questions