Skip to content

[ModelsR4] MedicationStatement crashing when medicationX is null #39

Closed

Description

@lets-swapcode

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!
Image

Activity

  1. p2-apple commented on Oct 16, 2025

    @p2-apple
    Member

    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.

  2. self-assigned this
    on Oct 16, 2025
  3. added theissue type on Oct 16, 2025
  4. p2-apple commented on Oct 16, 2025

    @p2-apple
    Member

    Actually, there's this further up:

    // 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?

  5. removed theissue type on Oct 23, 2025
  6. p2-apple commented on Oct 23, 2025

    @p2-apple
    Member

    This isn't actually an issue.

  7. lets-swapcode commented on Oct 27, 2025

    @lets-swapcode
    Author

    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)

  8. lets-swapcode commented on Oct 27, 2025

    @lets-swapcode
    Author

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

  9. p2-apple commented on Oct 28, 2025

    @p2-apple
    Member

    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.

  10. added
    bug
    on Oct 28, 2025
  11. p2-apple commented on Oct 31, 2025

    @p2-apple
    Member

    @lets-swapcode any update?

  12. lets-swapcode commented on Nov 3, 2025

    @lets-swapcode
    Author

    I will update you tomorrow with a sample response if possible. Thanks for reopening. @p2-apple

  13. p2-apple commented on Nov 19, 2025

    @p2-apple
    Member

    @lets-swapcode any update?

  14. p2-apple commented on Nov 27, 2025

    @p2-apple
    Member

    @lets-swapcode I can not reproduce this.

  15. added a commit that references this issue on Dec 24, 2025
  16. p2-apple commented on Jun 2, 2026

    @p2-apple
    Member

    Found how to reproduce and will add a fix in the next release.

  17. added a commit that references this issue on Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions