diff --git a/CHANGELOG.md b/CHANGELOG.md index 73776de..878931f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.12 + +* Fix fetch_raw for custom resource class + ## 2.0.11 * Rename dump to dump_resource diff --git a/fhirpy/__init__.py b/fhirpy/__init__.py index d1a9c11..3f5b547 100644 --- a/fhirpy/__init__.py +++ b/fhirpy/__init__.py @@ -1,7 +1,7 @@ from .lib import AsyncFHIRClient, SyncFHIRClient __title__ = "fhir-py" -__version__ = "2.0.11" +__version__ = "2.0.12" __author__ = "beda.software" __license__ = "None" __copyright__ = "Copyright 2024 beda.software" diff --git a/fhirpy/base/lib_async.py b/fhirpy/base/lib_async.py index db29c6c..a57f465 100644 --- a/fhirpy/base/lib_async.py +++ b/fhirpy/base/lib_async.py @@ -400,8 +400,8 @@ async def fetch_raw(self) -> Any: data = await self.client._fetch_resource(self.resource_type, self.params) data_resource_type = data.get("resourceType", None) - if data_resource_type == "Bundle": - for item in data["entry"]: + if data_resource_type == "Bundle" and not self.custom_resource_class: + for item in data.get("entry", []): item.resource = self._dict_to_resource(item.resource) return data diff --git a/fhirpy/base/lib_sync.py b/fhirpy/base/lib_sync.py index 60787ed..97e61ef 100644 --- a/fhirpy/base/lib_sync.py +++ b/fhirpy/base/lib_sync.py @@ -398,8 +398,8 @@ def fetch_raw(self) -> Any: data = self.client._fetch_resource(self.resource_type, self.params) data_resource_type = data.get("resourceType", None) - if data_resource_type == "Bundle": - for item in data["entry"]: + if data_resource_type == "Bundle" and not self.custom_resource_class: + for item in data.get("entry", []): item.resource = self._dict_to_resource(item.resource) return data diff --git a/tests/test_lib_async.py b/tests/test_lib_async.py index f93b42d..9d2fa72 100644 --- a/tests/test_lib_async.py +++ b/tests/test_lib_async.py @@ -792,6 +792,16 @@ async def test_fetch_raw(self): assert isinstance(entry.resource, AsyncFHIRResource) assert len(bundle.entry) == 2 # noqa: PLR2004 + @pytest.mark.asyncio() + async def test_typed_fetch_raw(self): + await self.create_resource("Patient", name=[{"text": "RareName"}]) + await self.create_resource("Patient", name=[{"text": "RareName"}]) + bundle = await self.client.resources(Patient).search(name="RareName").fetch_raw() + assert bundle.resourceType == "Bundle" + for entry in bundle.entry: + assert not isinstance(entry.resource, AsyncFHIRResource) + assert len(bundle.entry) == 2 # noqa: PLR2004 + async def create_test_patients(self, count=10, name="Not Rare Name"): bundle = { "type": "transaction", diff --git a/tests/test_lib_sync.py b/tests/test_lib_sync.py index 927d3b2..dbfa202 100644 --- a/tests/test_lib_sync.py +++ b/tests/test_lib_sync.py @@ -708,6 +708,15 @@ def test_fetch_raw(self): assert isinstance(entry.resource, SyncFHIRResource) assert len(bundle.entry) == 2 # noqa: PLR2004 + def test_typed_fetch_raw(self): + self.create_resource("Patient", name=[{"text": "RareName"}]) + self.create_resource("Patient", name=[{"text": "RareName"}]) + bundle = self.client.resources(Patient).search(name="RareName").fetch_raw() + assert bundle.resourceType == "Bundle" + for entry in bundle.entry: + assert not isinstance(entry.resource, SyncFHIRResource) + assert len(bundle.entry) == 2 # noqa: PLR2004 + def create_test_patients(self, count=10, name="Not Rare Name"): bundle = { "type": "transaction",