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

Object gets serialized as null instead of empty dict #115

Open ev-agelos opened 1 year ago

ev-agelos commented 1 year ago

Description

I was expecting an empty object to be serialized to empty dict instead gets serialized as null.

What I Did

from fhir.resources.careteam import CareTeamParticipant
CareTeamParticipant(role=[{}]).json()
# output: '{"role":[null]}'
# I was expecting it to be: '{"role":[{}]}'
biagiodistefano commented 1 year ago

I have encountered this bug as well.

This issue is relevant because sometimes it makes it impossible to serialize a resource and then load it again, as the serialization creates an invalid resource.

For instance, a resource is serialized as follows:

{
    "fullUrl": "urn:uuid:ff258b30-915f-4561-b356-7b30e7a9393a",
    "resource": {
        "resourceType": "Immunization",
        "id": "ff258b30-915f-4561-b356-7b30e7a9393a",
        "meta": {
            "profile": [
                "https://fhir.kbv.de/StructureDefinition/KBV_PR_MIO_Vaccination_Record_Prime|1.1.0"
            ]
        },
        "text": {
            "status": "extensions",
            "div": "<div>\n                        <div/></div>",
            "_div": {
                "extension": [
                    null
                ]
            }
        },
...
}

And then when trying to load from the json:

[[ErrorWrapper(exc=ValidationError(model='BundleEntry', errors=[{'loc': ('resource', 'text', '_div', 'extension', 0), 'msg': 'none is not an allowed value', 'type': 'type_error.none.not_allowed'}]), loc=('entry', 6))]