beda-software / fhir-py

FHIR Client for python
MIT License
168 stars 31 forks source link

to_resource() error "Unknown search parameter 'id'" #55

Closed motey closed 4 years ago

motey commented 4 years ago

I am trying to resolve references from a resource, with AsyncReference.to_resource() This seems to fail.

example

import asyncio
from fhirpy import AsyncFHIRClient
from py2neo import Graph, Node, Relationship

async def main():
    # Create an instance
    client = AsyncFHIRClient(
        "http://hapi.fhir.org/baseR4",
        # "http://localhost:8080/fhir",
        authorization="Bearer TOKEN",
    )

    obser_client = client.resources("Observation")  # Return lazy search set
    obser_query = obser_client.search().limit(5)
    observations = await obser_query.fetch()
    for obser in observations:
        if "subject" in obser:
            # This will fail
            patient = await obser.subject.to_resource()

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

It looks like a typo on line

https://github.com/beda-software/fhir-py/blob/f85b7d5d2614cf23436680bce537a8c64bd1f0ab/fhirpy/base/lib.py#L355

because when changing the line

self.resource_type).search(id=self.id).get()

to

self.resource_type).search(_id=self.id).get()

it seems to work. Im just starting to work with fhir, and do not have a good overview of the subject matter. Do I use the function in a wrong way? Is the fhir test server data corrupt? If i found a bug, i can provide a merge request.

motey commented 4 years ago

Attaching error log:

Traceback (most recent call last):
  File "/media/bleimehl/Daten_Linux/Repos/git/dzdgitlab/fhir2neo4j/try-resolve-ref.py", line 25, in <module>
    loop.run_until_complete(main())
  File "/usr/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
    return future.result()
  File "/media/bleimehl/Daten_Linux/Repos/git/dzdgitlab/fhir2neo4j/try-resolve-ref.py", line 20, in main
    patient = await obser.subject.to_resource()
  File "/home/bleimehl/.local/lib/python3.7/site-packages/fhirpy/base/lib.py", line 374, in to_resource
    return await self.client.resources(self.resource_type).search(id=self.id).get()
  File "/home/bleimehl/.local/lib/python3.7/site-packages/fhirpy/base/lib.py", line 270, in get
    res_data = await searchset.fetch()
  File "/home/bleimehl/.local/lib/python3.7/site-packages/fhirpy/base/lib.py", line 215, in fetch
    bundle_data = await self.client._fetch_resource(self.resource_type, self.params)
  File "/home/bleimehl/.local/lib/python3.7/site-packages/fhirpy/base/lib.py", line 89, in _fetch_resource
    return await self._do_request("get", path, params=params)
  File "/home/bleimehl/.local/lib/python3.7/site-packages/fhirpy/base/lib.py", line 86, in _do_request
    raise OperationOutcome(await r.text())
fhirpy.base.exceptions.OperationOutcome: {
  "resourceType": "OperationOutcome",
  "text": {
    "status": "generated",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><h1>Operation Outcome</h1><table border=\"0\"><tr><td style=\"font-weight: bold;\">ERROR</td><td>[]</td><td><pre>Unknown search parameter &quot;id&quot;. Value search parameters for this search are: [_id, _language, active, address, address-city, address-country, address-postalcode, address-state, address-use, addresscontenttype, birthdate, death-date, deceased, email, eyecolour, family, gdaddresstype, gender, general-practitioner, given, identifier, language, link, middlename, mothersMaidenName, name, organization, phone, phonetic, race, sex, telecom]</pre></td>\n\t\t\t\t\t\n\t\t\t\t\n\t\t\t</tr>\n\t\t</table>\n\t</div>"
  },
  "issue": [
    {
      "severity": "error",
      "code": "processing",
      "diagnostics": "Unknown search parameter \"id\". Value search parameters for this search are: [_id, _language, active, address, address-city, address-country, address-postalcode, address-state, address-use, addresscontenttype, birthdate, death-date, deceased, email, eyecolour, family, gdaddresstype, gender, general-practitioner, given, identifier, language, link, middlename, mothersMaidenName, name, organization, phone, phonetic, race, sex, telecom]"
    }
  ]
}
mkizesov commented 4 years ago

Thanks for pointing this out. I think it's a bug according to FHIR spec https://www.hl7.org/fhir/search.html#id It worked for us because our FHIR server accept both id and _id params. Feel free to provide a merge request!