Stream: hapi
Topic: FHIRPathEngine ignores annotated Extensions
Sergej Reiser (Sep 24 2020 at 14:46):
The FHIRPathEngine is not seeing Extensions, that have been added by using the Field-Annotations @Child / @Extension.
Lets say i have the following class for MyPatient (just like in the HAPI documentation):
@ResourceDef(name="Patient", profile="http://example.com/StructureDefinition/mypatient")
public class MyPatient extends Patient {
private static final long serialVersionUID = 1L;
@Child(name="petName")
@Extension(url="http://example.com/dontuse#petname", definedLocally=false, isModifier=false)
@Description(shortDefinition="The name of the patient's favourite pet")
private StringType myPetName;
@Override
public boolean isEmpty() {
return super.isEmpty() && ElementUtil.isEmpty(myPetName);
}
public StringType getPetName() {
if (myPetName == null) {
myPetName = new StringType();
}
return myPetName;
}
public void setPetName(StringType thePetName) {
myPetName = thePetName;
}
}
Now i would like to read the Extension with a FHIR-Path expression.
@Test
void readByFhirPath() throws Exception {
IFhirPath fhirPath = FhirContext.forR4().newFhirPath();
MyPatient myPatient = new MyPatient();
myPatient.setPetName(new StringType("Adam"));
// notice: myPatient.getExtension() is empty
List<Extension> result = fhirPath.evaluate(myPatient, "Patient.extension('http://example.com/dontuse#petname')", Extension.class);
assert result.size() == 1; // fails, because the list is empty
}
This fails! But when i add the Extension the "old-fsahion" way everything is fine!
@Test
void readByFhirPath() throws Exception {
IFhirPath fhirPath = FhirContext.forR4().newFhirPath();
MyPatient myPatient = new MyPatient();
// myPatient.setPetName(new StringType("Adam"));
myPatient.addExtension(new Extension("http://example.com/dontuse#petname", new StringType("Adam")));
List<Extension> result = fhirPath.evaluate(myPatient, "Patient.extension('http://example.com/dontuse#petname')", Extension.class);
assert result.size() == 1; // succeeds
}
Im not sure if this is an Issue with the FHIRPathEngine itself, or rather with the fact, that annotated Extensions are not accessable through resource.getExtension()
. Is there some kind of a workaround to this?
Sergej Reiser (Oct 21 2020 at 08:51):
PUSH
Could someone please look into this? The circumstance, that FHIR-Path is not working for annotated Extensions has further implications for indexing of SearchParams.
If i create a custom SearchParameter for petName
it will not work, because HAPI also uses FHIR-Path for indexing. So this is what happens:
Before indexing or persisting, HAPI will parse the resource into it's corresponding class. If i added theMyPatient
class to the FhirContext by fhirContext.setDefaultTypeForProfile("http://example.com/StructureDefinition/mypatient", MyPatient.class)
, HAPI will parse the resource into MyPatient
and further pass this object for indexing. Since MyPatient
uses the @Extension
annotation FHIR-Path will fail to evaluate the expression i use for my SearchParameter (Patient.extension('http://example.com/dontuse#petname')
). So petName
will never be indexed as long as HAPI uses the MyPatient
class for processing.
The only workaround i found is to temporarily remove the MyPatient
class from the FHIR-Context fhirContext.setDefaultTypeForProfile("http://example.com/StructureDefinition/mypatient", null)
. Than HAPI will parse incoming resources into the default Patient
class which has no @Extension
annotations.
Last updated: Apr 12 2022 at 19:14 UTC