nazrulworld / fhir.resources

FHIR Resources https://www.hl7.org/fhir/resourcelist.html
https://pypi.org/project/fhir.resources/
Other
372 stars 104 forks source link

FHIR Reference does not resolve references in Bundle #42

Open hackermd opened 3 years ago

hackermd commented 3 years ago

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 commented 3 years ago

@nazrulworld any thoughts on this?

nazrulworld commented 3 years ago

@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

hackermd commented 3 years ago

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.