fhir.resources icon indicating copy to clipboard operation
fhir.resources copied to clipboard

FHIR Reference does not resolve references in Bundle

Open hackermd opened this issue 4 years ago • 3 comments

  • fhir.resources version: 5.1.1.
  • Python version: 3.8

Description

Resolution of references within a Bundle fails unless an instance to a server is provided (and a couple of additional undocumented assumptions are met).

The problem is in fhir.resources.fhirresource.FHIRResource.resolved(). In case of a relative reference, the method attempts to build the fullUrl based on the value of bundle.server.base_uri. If no server instance is set, resolution fails.

The standard states

If the reference is not an absolute reference, convert it to an absolute URL:

if the reference has the format [type]/[id], and
if the fullUrl for the bundle entry containing the resource is a RESTful one (see the RESTful URL regex)
    extract the [root] from the fullUrl, and append the reference (type/id) to it
    then try to resolve within the bundle as for a RESTful URL reference.
    If no resolution is possible, then the reference has no defined meaning within this specification

The important part is extract the [root] from the fullUrl, and append the reference (type/id) to it. For example:

base_uri = '/'.join(bundle_entry.fullUrl.split('/')[:-2])
fullUrl = f'{base_uri}/{resource_type}/{resource_id}'

The bundle.server.base_uri is not needed (and should not be expected to be present).

What I Did

import requests
from fhir.resources.bundle import Bundle
from fhir.resources.patient import Patient

response = requests.get('https://www.hl7.org/fhir/bundle-example.json')
bundle = Bundle(response.json())

patient = bundle.entry[0].resource.subject.resolved(Patient)

results in

Not implemented: resolving absolute reference to resource Patient/347

hackermd avatar Nov 20 '20 03:11 hackermd

@nazrulworld any thoughts on this?

hackermd avatar Jan 27 '21 03:01 hackermd

@hackermd I am so sorry for the late reply. Unfortunately from the version of 6.x.x, this library is completely standalone, it does not any idea about any server info/connection so unable to resolve the reference. Now it's the developer's responsibility to create a resolver according to his/her own business logic. For example https://github.com/nazrulworld/fhirpath/blob/master/src/fhirpath/utils.py#L766

nazrulworld avatar Jan 27 '21 06:01 nazrulworld

Thanks for your feedback @nazrulworld. It seems version 6 introduced several breaking API changes. You have listed three in the README, but it would be great if you could document these changes more comprehensively.

hackermd avatar Jan 27 '21 12:01 hackermd