You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ModelsR4] MedicationStatement crashing when medicationX is null #39
As per the source code, Medication is force unwrapping outside of optional chain block. class @MedicationStatement
Look's like we just need to set medication inside the if let statements.
Here's the code snippet and screenshot for reference.
var _t_medication: MedicationX? = nil
if let medicationCodeableConcept = try CodeableConcept(from: _container, forKeyIfPresent: .medicationCodeableConcept) {
if _t_medication != nil {
throw DecodingError.dataCorruptedError(forKey: .medicationCodeableConcept, in: _container, debugDescription: "More than one value provided for \"medication\"")
}
_t_medication = .codeableConcept(medicationCodeableConcept)
}
if let medicationReference = try Reference(from: _container, forKeyIfPresent: .medicationReference) {
if _t_medication != nil {
throw DecodingError.dataCorruptedError(forKey: .medicationReference, in: _container, debugDescription: "More than one value provided for \"medication\"")
}
_t_medication = .reference(medicationReference)
}
self.medication = _t_medication!
medication on MedicationStatement is required, so it makes sense for some error to happen, but I agree, it shouldn't run into a force unwrap, this should be handled more gracefully.
// Validate that we have at least one of the mandatory properties for expanded properties
guard _container.contains(CodingKeys.medicationCodeableConcept) || _container.contains(CodingKeys.medicationReference) else {
throw DecodingError.valueNotFound(Any.self, DecodingError.Context(codingPath: [CodingKeys.medicationCodeableConcept, CodingKeys.medicationReference], debugDescription: "Must have at least one value for \"medication\" but have none"))
}
Are you actually seeing a crash or are you inferring from the code?
I have faced two crashes multiple times at MedicationStatement. I had to disabled the Medication flow for time being.
One is at Sources -> ModelsR4 -> MedicationStatement line 241 self.medication = _t_medication!
AND Other is at Source -> ModelsBuild -> MedicationStatement line 241 try derivedFrom?.encode(on: &_container, forKey: .derivedFrom)
@p2-apple can you please reopen this issue so it can be planned to pick to handle force unwrapping and throw error in this case? (I do not have the permission to do so)
If you see actual crashes then I'm clearly wrong. Do you have a stack trace or example FHIR resources that cause the crash? I want to fully understand the situation when fixing it. I'm assuming the resource has actual content under the valueX properties but they fail to decode.
Activity
p2-apple commentedon Oct 16, 2025
medicationonMedicationStatementis required, so it makes sense for some error to happen, but I agree, it shouldn't run into a force unwrap, this should be handled more gracefully.p2-apple commentedon Oct 16, 2025
Actually, there's this further up:
Are you actually seeing a crash or are you inferring from the code?
p2-apple commentedon Oct 23, 2025
This isn't actually an issue.
lets-swapcode commentedon Oct 27, 2025
I have faced two crashes multiple times at MedicationStatement. I had to disabled the Medication flow for time being.
One is at Sources -> ModelsR4 -> MedicationStatement line 241
self.medication = _t_medication!AND Other is at Source -> ModelsBuild -> MedicationStatement line 241
try derivedFrom?.encode(on: &_container, forKey: .derivedFrom)lets-swapcode commentedon Oct 27, 2025
@p2-apple can you please reopen this issue so it can be planned to pick to handle force unwrapping and throw error in this case? (I do not have the permission to do so)
p2-apple commentedon Oct 28, 2025
If you see actual crashes then I'm clearly wrong. Do you have a stack trace or example FHIR resources that cause the crash? I want to fully understand the situation when fixing it. I'm assuming the resource has actual content under the
valueXproperties but they fail to decode.p2-apple commentedon Oct 31, 2025
@lets-swapcode any update?
lets-swapcode commentedon Nov 3, 2025
I will update you tomorrow with a sample response if possible. Thanks for reopening. @p2-apple
p2-apple commentedon Nov 19, 2025
@lets-swapcode any update?
p2-apple commentedon Nov 27, 2025
@lets-swapcode I can not reproduce this.
Update `build` models to R6 ballot 3
p2-apple commentedon Jun 2, 2026
Found how to reproduce and will add a fix in the next release.
Better handling of ValueX