Search code examples
hapihapi-fhir

Hapi FHIR Bundle - Resource doesn't contain id element


I'm trying to generate a Bundle that contains a diagnosticReport and multiple associated medias. However, I can't manage to get the generated bundle to contain the id element for the media's resource (entry[i]/resource/id).

I generated the diagnosticReport and medias like this:

    protected Media createMedia(String patientID, DatasetFile datasetFile, Thumbnail thumbnail) {
        Media media = new Media();

        media.setId(IdType.newRandomUuid());
        media.setStatus(Media.MediaStatus.COMPLETED);

        media.setHeight(thumbnail.height);
        media.setWidth(thumbnail.width);

        media.getCreatedDateTimeType().setValue(DateTime.now().toDate());

        media.getSubject().setReference("Patient/" + patientID);
        media.getSubject().setType("Patient");

        Identifier identifier = media.addIdentifier();

        identifier.setSystem(testSystem);
        identifier.setValue(datasetFile.getGlobalId().toString());

        media.getType().addCoding()
            .setSystem("http://hl7.org/fhir/ValueSet/media-type")
            .setCode("image");

        media.getContent()
            .setTitle(thumbnail.fileName)
            .setContentType(thumbnail.contentType)
            .setData(thumbnail.imageContent);

        return media;
    }

    protected DiagnosticReport createDiagnosticReport(String patientID, Long globalMessageId, Dataset dataset, Map<DatasetFile, Media> medias) {
        DiagnosticReport diagnosticReport = new DiagnosticReport();

        diagnosticReport.setId(IdType.newRandomUuid());

        diagnosticReport.setStatus(DiagnosticReport.DiagnosticReportStatus.FINAL);

        diagnosticReport.getText().getDiv().setValueAsString("TBD");
        diagnosticReport.getText().setStatus(Narrative.NarrativeStatus.ADDITIONAL);

        diagnosticReport.getCode()
            .addCoding()
            .setSystem("http://loinc.org")
            .setCode("10197-2")
            .setDisplay("Physical findings of Eye Narrative");

        diagnosticReport.getSubject().setReference("Patient/" + patientID);

        diagnosticReport.getEffectiveDateTimeType().setValue(DateTime.now().toDate());

        Identifier identifier = diagnosticReport.addIdentifier();

        identifier.setSystem(testSystem);
        identifier.setValue(dataset.getGlobalId().toString());

        medias.values().stream().forEach(m -> {
            DiagnosticReport.DiagnosticReportMediaComponent mediaRep = diagnosticReport.addMedia();
            mediaRep.getLink().setReference(m.getIdElement().getValue());
            mediaRep.getLink().setType("Media");
        });

        return diagnosticReport;
    }

This is the code I'm using to put the diagnosticReport and the medias in the bundle:

    protected void addDiagnosticReportAndMediasToBundle(Bundle bundle, DiagnosticReport diagnosticReport, Map<DatasetFile, Media> medias) {
        bundle.setType(Bundle.BundleType.TRANSACTION);

        bundle.addEntry()
            .setResource(diagnosticReport)
            .getRequest()
            .setUrl("DiagnosticReport")
            .setMethod(Bundle.HTTPVerb.POST);

        medias.values().forEach(m -> {
            Bundle.BundleEntryComponent bundleEntryComponent = bundle.addEntry();
            bundleEntryComponent.setResource(m);
//            bundleEntryComponent.setId(m.getId());
            bundleEntryComponent.getRequest()
                .setUrl("Media")
                .setMethod(Bundle.HTTPVerb.POST);
        });
    }

Which results in the following snipper (extract):

{
  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [ {
    "resource": {
      "resourceType": "DiagnosticReport",
      "text": {
        "status": "additional",
        "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">TBD</div>"
      },
      "identifier": [ {
        "system": "http://test.com/my",
        "value": "10"
      } ],
      "status": "final",
      "code": {
        "coding": [ {
          "system": "http://loinc.org",
          "code": "10197-2",
          "display": "Physical findings of Eye Narrative"
        } ]
      },
      "subject": {
        "reference": "Patient/559d0494ddd50c0e847e14b5"
      },
      "effectiveDateTime": "2021-03-17T19:41:11+01:00",
      "media": [ {
        "link": {
          "reference": "urn:uuid:67f3d12d-85f2-412c-bcd7-ab1f55acb63a",
          "type": "Media"
        }
      } ]
    },
    "request": {
      "method": "POST",
      "url": "DiagnosticReport"
    }
  },
  {
    "resource": {
      "resourceType": "Media",
      "identifier": [ {
        "system": "http://test.com/my",
        "value": "1616"
      } ],
      "status": "completed",
      "type": {
        "coding": [ {
          "system": "http://hl7.org/fhir/ValueSet/media-type",
          "code": "image"
        } ]
      },
      "subject": {
        "reference": "Patient/559d0494ddd50c0e847e14b5",
        "type": "Patient"
      },
      "createdDateTime": "2021-03-17T19:41:11+01:00",
      "height": 200,
      "width": 200,
      "content": {
        "contentType": "image/jpeg",
        "data": "/9j/4AAQSkZJRgABAgEAYABgAAD(...)",
        "title": "dummy image.jpg"
      }
    },
    "request": {
      "method": "POST",
      "url": "Media"
    }
  } ]
}

The random uuid of the media appears in the diagnosticReport entry[i]/resource/media[j]/link/reference

Calling m.setId or m.setIdElement doesn't change anything as expected.

Is this an expected behaviour for FHIR? In my example, I only have one media but if there were 2, I wouldn't be able to identify which media is which?

Thanks!


Solution

  • Using media.setId(IdType.newRandomUuid()); doesn't work. If I pass any static string as argument, it works as expected.