nazrulworld / fhir.resources

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

Instant on DocumentReference not properly output on json #145

Open richard-keebler opened 9 months ago

richard-keebler commented 9 months ago

Description

When loading a document reference from a json representation that originally came from a DocumentReference, it fails on validating the date. Below is a contrived example, but the json we're getting (and loading) is from a fhir repository that delivers and accepts json representations.

What I Did

d = DocumentReference(
  id="test",
  status=Code('unknown'),
  content=[DocumentReferenceContent(id="testc", attachment=Attachment(contentType=Code("text/plain"), data="hi"))],
  date=Instant(year=2023, day=8, month=8, hour=8, minute=8, second=8)
)

j = d.json()

item = json.loads(j)

dd = DocumentReference(**item) <-- this throws error invalid datetime format (type=value_error.datetime)

There is a hacky fix here. Just add a Z to the end of the date string representation and the problem is solved, i.e. item['date'] + "Z". I would expect though, that the string representation would already have that.

janwijbrand commented 9 months ago

Ah, you ran into this issue more or less simultaneously with me. Let say +1 to see if we can find a solution to this issue of "instants" that require timezone information in their string representation. What I did to circumvent for the time being is to add timezone info to the dt object that gets serialized later: dt.replace(tzinfo=datetime.timezone.utc)