Skip to content

Commit a7b2dee

Browse files
committed
Fix fetch_raw for typed using
1 parent b3cda90 commit a7b2dee

File tree

6 files changed

+28
-5
lines changed

6 files changed

+28
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.12
2+
3+
* Fix fetch_raw for custom resource class
4+
15
## 2.0.11
26

37
* Rename dump to dump_resource

fhirpy/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .lib import AsyncFHIRClient, SyncFHIRClient
22

33
__title__ = "fhir-py"
4-
__version__ = "2.0.11"
4+
__version__ = "2.0.12"
55
__author__ = "beda.software"
66
__license__ = "None"
77
__copyright__ = "Copyright 2024 beda.software"

fhirpy/base/lib_async.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ async def fetch_raw(self) -> Any:
400400
data = await self.client._fetch_resource(self.resource_type, self.params)
401401
data_resource_type = data.get("resourceType", None)
402402

403-
if data_resource_type == "Bundle":
404-
for item in data["entry"]:
403+
if data_resource_type == "Bundle" and not self.custom_resource_class:
404+
for item in data.get("entry", []):
405405
item.resource = self._dict_to_resource(item.resource)
406406

407407
return data

fhirpy/base/lib_sync.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ def fetch_raw(self) -> Any:
398398
data = self.client._fetch_resource(self.resource_type, self.params)
399399
data_resource_type = data.get("resourceType", None)
400400

401-
if data_resource_type == "Bundle":
402-
for item in data["entry"]:
401+
if data_resource_type == "Bundle" and not self.custom_resource_class:
402+
for item in data.get("entry", []):
403403
item.resource = self._dict_to_resource(item.resource)
404404

405405
return data

tests/test_lib_async.py

+10
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,16 @@ async def test_fetch_raw(self):
792792
assert isinstance(entry.resource, AsyncFHIRResource)
793793
assert len(bundle.entry) == 2 # noqa: PLR2004
794794

795+
@pytest.mark.asyncio()
796+
async def test_typed_fetch_raw(self):
797+
await self.create_resource("Patient", name=[{"text": "RareName"}])
798+
await self.create_resource("Patient", name=[{"text": "RareName"}])
799+
bundle = await self.client.resources(Patient).search(name="RareName").fetch_raw()
800+
assert bundle.resourceType == "Bundle"
801+
for entry in bundle.entry:
802+
assert not isinstance(entry.resource, AsyncFHIRResource)
803+
assert len(bundle.entry) == 2 # noqa: PLR2004
804+
795805
async def create_test_patients(self, count=10, name="Not Rare Name"):
796806
bundle = {
797807
"type": "transaction",

tests/test_lib_sync.py

+9
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,15 @@ def test_fetch_raw(self):
708708
assert isinstance(entry.resource, SyncFHIRResource)
709709
assert len(bundle.entry) == 2 # noqa: PLR2004
710710

711+
def test_typed_fetch_raw(self):
712+
self.create_resource("Patient", name=[{"text": "RareName"}])
713+
self.create_resource("Patient", name=[{"text": "RareName"}])
714+
bundle = self.client.resources(Patient).search(name="RareName").fetch_raw()
715+
assert bundle.resourceType == "Bundle"
716+
for entry in bundle.entry:
717+
assert not isinstance(entry.resource, SyncFHIRResource)
718+
assert len(bundle.entry) == 2 # noqa: PLR2004
719+
711720
def create_test_patients(self, count=10, name="Not Rare Name"):
712721
bundle = {
713722
"type": "transaction",

0 commit comments

Comments
 (0)