Extension
FHIR is used in many different systems and countries, making it very difficult for all systems to implement all requirements of all other systems, even though they may be valid requirements. FHIR therefore allows Extensions to resources and datatypes.
there can be no stigma associated with the use of extensions by any application, project, or standard - regardless of the institution or jurisdiction that uses or defines the extensions. The use of extensions is what allows the FHIR specification to retain a core simplicity for everyone.
An extension element may have either a value or more child extensions, but not both. So extensions become a tree: simple extensions as leaf nodes with values, and complex nodes as branch nodes with child extensions.
Extensions must have a uri for meaning. The value of an extension may be any primitive value.

While the generated Gleam resources support any complex extension, they are somewhat verbose and require checking url strings. Generate a library version with Custom Extension for nicer specific extensions. By default types do not have primitive extensions; see Primitive Extension to use them.
import fhir/r4/complex_types as ct
import fhir/r4/resources
import gleam/json
import gleam/list
pub fn main() {
let assert Ok(pat) =
json.parse(patient_example_sex_and_gender, resources.patient_decoder())
let rsg_extensions =
list.filter(pat.extension, fn(e) {
e.url
== "http://hl7.org/fhir/StructureDefinition/individual-recordedSexOrGender"
})
let assert [rsg, _, _] = rsg_extensions
let assert ct.ExtComplex(rsg_children) = rsg.ext
let assert Ok(value_child) =
list.find(rsg_children, fn(e) { e.url == "value" })
let assert ct.ExtSimple(value_ext) = value_child.ext
echo value_ext
let type_ext = list.find(rsg_children, fn(e) { e.url == "type" })
let _ = echo type_ext
let source_doc_ext =
list.find(rsg_children, fn(e) { e.url == "sourceDocument" })
echo source_doc_ext
}
const patient_example_sex_and_gender = "{
\"resourceType\": \"Patient\",
\"id\": \"patient-example-sex-and-gender\",
\"text\": {
\"status\": \"generated\",
\"div\": \"<div>https://hl7.org/fhir/R5/patient-example-sex-and-gender.json.html</div>\"},
\"extension\": [
{
\"url\" : \"http://hl7.org/fhir/StructureDefinition/individual-genderIdentity\",
\"extension\": [
{
\"url\": \"value\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\" : \"http://snomed.info/sct\",
\"code\" : \"446141000124107\",
\"display\": \"Identifies as female gender (finding)\"
}
]
}
},
{ \"url\": \"period\", \"valuePeriod\": {\"start\": \"2001-05-06\"} },
{
\"url\" : \"comment\",
\"valueString\": \"Patient transitioned from male to female in 2001.\"
}
]
},
{
\"url\" : \"http://hl7.org/fhir/StructureDefinition/individual-pronouns\",
\"extension\": [
{
\"url\": \"value\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\" : \"http://loinc.org\",
\"code\" : \"LA29519-8\",
\"display\": \"she/her/her/hers/herself\"
}
]
}
},
{ \"url\": \"period\", \"valuePeriod\": {\"start\": \"2001-05-06\"} },
{
\"url\" : \"comment\",
\"valueString\": \"Patient transitioned from male to female in 2001.\"
}
]
},
{
\"url\" : \"http://hl7.org/fhir/StructureDefinition/individual-recordedSexOrGender\",
\"extension\": [
{
\"url\": \"value\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\" : \"http://hl7.org/fhir/administrative-gender\",
\"code\" : \"male\",
\"display\": \"Male\"
}
]
}
},
{
\"url\": \"type\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\" : \"http://loinc.org\",
\"code\" : \"76689-9\",
\"display\": \"Sex Assigned At Birth\"
}
]
}
},
{ \"url\": \"effectivePeriod\", \"valuePeriod\": {\"start\": \"1974-12-25\"} },
{\"url\": \"acquisitionDate\", \"valueDateTime\": \"2005-12-06\"},
{
\"url\": \"sourceDocument\",
\"valueReference\": {\"reference\": \"DocumentReference/1\"}
},
{\"url\": \"sourceField\", \"valueString\": \"SEX\"},
{
\"url\": \"jurisdiction\",
\"valueCodeableConcept\": {
\"coding\": [
{\"system\": \"https://www.usps.com/\", \"code\": \"OH\", \"display\": \"Ohio\"}
]
}
},
{
\"url\": \"comment\",
\"valueString\": \"Patient transitioned from male to female in 2001, but their birth certificate still indicates male.\"
}
]
},
{
\"url\" : \"http://hl7.org/fhir/StructureDefinition/individual-recordedSexOrGender\",
\"extension\": [
{
\"url\": \"value\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\" : \"http://hl7.org/fhir/administrative-gender\",
\"code\" : \"male\",
\"display\": \"Male\"
}
]
}
},
{
\"url\": \"type\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\": \"http://local-code-system.org/recorded-sex-or-gender-type\",
\"code\": \"insurance-card\",
\"display\": \"Insurance Card\"
}
]
}
},
{ \"url\": \"effectivePeriod\", \"valuePeriod\": {\"start\": \"2021-05-25\"} },
{\"url\": \"acquisitionDate\", \"valueDateTime\": \"2021-06-06\"},
{
\"url\": \"sourceDocument\",
\"valueReference\": {\"reference\": \"DocumentReference/2\"}
},
{\"url\": \"sourceField\", \"valueString\": \"SEX\"},
{
\"url\": \"jurisdiction\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\": \"http://local-code-system.org/recorded-sex-or-gender-jurisdiction\",
\"code\": \"ICCA-P\",
\"display\": \"Indigo Crucifix Cobalt Aegis Payer\"
}
]
}
},
{
\"url\": \"comment\",
\"valueString\": \"Patient transitioned from male to female in 2001, but their insurance card still indicates male.\"
}
]
},
{
\"url\" : \"http://hl7.org/fhir/StructureDefinition/individual-recordedSexOrGender\",
\"extension\": [
{
\"url\": \"value\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\" : \"http://ohio.example.gov/drivers-license-sex\",
\"code\" : \"M\",
\"display\": \"Male\"
}
]
}
},
{
\"url\": \"type\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\": \"http://jurisdiction-specific.example.com/document-type-code-system\",
\"code\": \"drivers-license\",
\"display\": \"Driver's License\"
}
]
}
},
{ \"url\": \"effectivePeriod\", \"valuePeriod\": {\"start\": \"1974-12-25\"} },
{\"url\": \"acquisitionDate\", \"valueDateTime\": \"2005-12-06\"},
{
\"url\": \"sourceDocument\",
\"valueReference\": {\"reference\": \"DocumentReference/1\"}
},
{
\"url\": \"jurisdiction\",
\"valueCodeableConcept\": {
\"coding\": [
{\"system\": \"https://www.usps.com/\", \"code\": \"OH\", \"display\": \"Ohio\"}
]
}
},
{
\"url\": \"comment\",
\"valueString\": \"Patient transitioned from male to female in 2001, but their driver's license still indicates male.\"
}
]
},
{
\"url\": \"http://hl7.org/fhir/StructureDefinition/patient-sexParameterForClinicalUse\",
\"extension\": [
{
\"url\": \"value\",
\"valueCodeableConcept\": {
\"coding\": [
{
\"system\": \"http://terminology.hl7.org/CodeSystem/sex-parameter-for-clinical-use\",
\"code\": \"specified\",
\"display\": \"Apply specified setting or reference range\"
}
]
}
},
{ \"url\": \"period\", \"valuePeriod\": {\"start\": \"2002-07-13\"} },
{
\"url\" : \"comment\",
\"valueString\": \"Patient transitioned from male to female in 2001.\"
},
{
\"url\": \"supportingInfo\",
\"valueReference\": {\"reference\": \"Observation/1\"}
},
{
\"url\": \"supportingInfo\",
\"valueReference\": {\"reference\": \"MedicationStatement/2\"}
}
]
}
],
\"identifier\": [
{
\"use\" : \"usual\",
\"type\" : {
\"coding\": [
{\"system\": \"http://terminology.hl7.org/CodeSystem/v2-0203\", \"code\": \"MR\"}
]
},
\"system\": \"urn:oid:1.2.36.146.595.217.0.1\",
\"value\" : \"12345\"
}
],
\"active\": true,
\"name\": [
{ \"use\": \"official\", \"family\": \"Roth\", \"given\": [\"Patrick\" ] },
{ \"use\": \"usual\", \"family\": \"Roth\", \"given\": [\"Patricia\"] },
{ \"use\": \"nickname\", \"given\": [\"Pat\" ] }
],
\"gender\": \"male\",
\"birthDate\": \"1974-12-25\",
\"deceasedBoolean\": false,
\"managingOrganization\": {\"reference\": \"Organization/1\"}
}"