Search code examples
javahl7-fhirhapihapi-fhir

How to include the full object instead of "contained" in HAPI FHIR


I am very new to hapi FHIR, I am trying to encode the request in following format.

   CoverageEligibilityRequest coverageEligibilityRequest =  new CoverageEligibilityRequest();
   Patient patient = new Patient().addIdentifier(new Identifier().setType(getPatientIdentifierCodeableConcept()).setSystem("http://www.abc.xyz").setValue("123"));
   coverageEligibilityRequest.setPatient(new Reference(patient));

Above code is java snippet for populating the patient in CoverageEligibilityRequest.

{
  "resourceType": "Bundle",
  "type": "batch",
  "entry": [ {
    "resource": {
      "resourceType": "CoverageEligibilityRequest",
      "id": "7890",
      "contained": [ {
        "resourceType": "Patient",
        "id": "1",
        "identifier": [ {
          "type": {
            "coding": [ {
             ...
             ...

}

But I want the request should be of following format

{
  "resourceType": "Bundle",
  "type": "batch",
  "entry": [ {
    "resource": {
      "resourceType": "CoverageEligibilityRequest",
      "id": "7890",
      "patient": {
        "type": "Patient",
        "identifier": {
          "type": {
            "coding": [ {
              ...
              ...

            } ]
          },

where I want to omit contained with actual string?


Solution

  • FHIR doesn't generally let you express an entire graph of objects as a single resource, so if you're trying to send a Patient resource as part of a CoverageEligibilityRequest resource, the only way you can do that is by setting the patient in the contained field. The CoverageEligibilityResource.patient field is defined as a Reference type and so can only contain the data allowed by a Reference data type and not arbitrary data.

    It seems like what you actually want to do is to add a Patient to the HAPI FHIR server and a CoverageEligibilityRequest resource that references the patient. The right way to do this in FHIR is to construct a single batch or transaction bundle containing both of the resources. Basically, you want to construct a Bundle that looks something like this:

    {
      "resourceType": "Bundle",
      "type": "batch",
      "entry": [ {
          "resource": {
            "resourceType": "Patient",
            "id": "1",
            "identifier": [ {
              "type": {
                "coding": [ {
                 ...
          }
        }, {
          "resource": {
            "resourceType": "CoverageEligibilityRequest",
            "id": "7890",
            "patient": "Patient/1",
            ...
    

    The easiest way to construct something similar in HAPI FHIR would be to use a transaction bundle like this:

    IGenericClient client = ...
    CoverageEligibilityRequest coverageEligibilityRequest =  new CoverageEligibilityRequest();
    Patient patient = new Patient().addIdentifier(new Identifier().setType(getPatientIdentifierCodeableConcept()).setSystem("http://www.abc.xyz").setValue("123"));
    coverageEligibilityRequest.setPatient(new Reference(patient));
    client.transaction().withResources(patient, coverageEligibilityRequest);