Skip to content

Implement subtype matching for type-related operators and functions (is, as, ofType) #2524

@piotrszul

Description

@piotrszul

The is operator, as operator, and ofType() function currently only perform exact type matching and do not recognize FHIR type inheritance. According to the FHIRPath specification, these should return true for "the type specified in the second operand, or a subclass thereof."

Failing test cases:

  • Observation is DomainResource → Expected: true, Got: false
  • Observation is Resource → Expected: true, Got: false
  • Observation.value is Element → Expected: true, Got: false
  • Patient.contact is Element → Expected: true, Got: false

Root Cause:
The current implementation in Collection.filterByType() uses exact type matching via equals():

case FHIR_NAMESPACE -> getFhirType().filter(type.toFhirType()::equals).map(t -> this);

Implementation Note:
Use HAPI FHIR implementation classes to check for subtypes. FHIR type names follow a consistent naming convention to their implementing classes in the R4 model (e.g., "Element" → org.hl7.fhir.r4.model.Element). Use Class.isAssignableFrom() to check inheritance relationships.

Affected Components:

  • Collection.filterByType()
  • ResourceCollection.filterByType()
  • is operator
  • as operator
  • ofType() function

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions