_include in hapi fhir

Viewed 413

Currently I am developing a FHIR Search API using HAPI FHIR (Java).

In the FHIR resource MedicationRequest in HAPI FHIR (Java), I want to include the Medication resource inline under MedicationRequest.medication[x], instead of as a Reference. I have provided a snippet below.

//code
@IncludeParam(allow = { "MedicationRequest:medication" }) Set<Include> theIncludes 


// url
/MedicationRequest?_include=MedicationRequest:medication&status=stopped,active

However, when I hit the url the response does not include the Medication resource inline.

If I use _include=*, then I get the desired outcome. Medication is included inline, along with the other References.

// code
@IncludeParam(allow = { "*" }) Set<Include> theIncludes

// url
/MedicationRequest?_include=*&status=stopped,active

Can anyone please help me in understanding why I am not getting desired outcome when I am using _include=MedicationRequest:medication?

1 Answers

_include=MedicationRequest:medication is the appropriate usage.

medication[x] refers to either:

  • medicationCodeableConcept
  • medicationReference

My best guess is you're attempting to _include a Medication resource that does not have a globally resolvable id set.

Without a globally resolvable id, HAPI FHIR will generate a locally resolvable id and include the locally resolvable Medication under MedicationRequest.contained.

Fix by setting an id and using a globally resolvable reference, i.e. medicationReference.reference: Medication/myMed1 instead of medicationReference.reference: #myMed1

See:


URL: http://test.fhir.org/r4/MedicationRequest?_id=medrx0311&_include=MedicationRequest:medication

Returns a bundle with only the MedicationRequest. Medication is contained at MedicationRequest.contained.

{
  "resourceType" : "MedicationRequest",
  "id" : "medrx0311",
  ...
  "contained" : [
    {
      "resourceType" : "Medication",
      "id" : "med0316",
  ...
  "medicationReference" : {
      "reference" : "#med0316"
},

The medicationReference.reference: #med0316 is not global; therefore, it's contained.

URL http://test.fhir.org/r4/MedicationRequest?_id=medrx002&_include=MedicationRequest:medication

Returns a bundle with both the MedicationRequest and Medication.

{
  "resourceType" : "MedicationRequest",
  "id" : "medrx002",
... 
  "medicationReference" : {
  "reference" : "Medication/med0316",
  "display" : "prescribed medication"
},
...
{
  "resourceType" : "Medication",
  "id" : "med0316",

Here, the appropriate global id medicationReference.reference: Medication/med0316 is used, so Medication is listed as a separate entry in the bundle.

Related