fhir.resources icon indicating copy to clipboard operation
fhir.resources copied to clipboard

Object gets serialized as null instead of empty dict

Open ev-agelos opened this issue 2 years ago • 1 comments

  • fhir.resources version:
  • Python version:
  • Operating System:

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":[{}]}'

ev-agelos avatar Dec 23 '22 16:12 ev-agelos

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))]

biagiodistefano avatar Jul 26 '23 10:07 biagiodistefano