LinuxForHealth/hl7v2-fhir-converter

required:true should skip if specs OR condition fail

cragun47 opened this issue · 0 comments

Describe the bug
Required:true is a mechanism where if a field is required and cannot be extracted then the resource generation will fail even if other fields were extracted (from the documentation). It should be that any valid failure of the required field should cause the resource generation to fail. EITHER a null specs or false condition should prevent the resource generation. Successful creation of the resource should require
BOTH a non-null valid specs input and a true condition.

However this does not work. It appears that a failing condition only causes the element itself to fail creation and the resource is still generated without it.

To Reproduce
An example to see this failure is found in elementcode in Observation.yml. In Observation.yml, when OBX's are missing or TX based, we don't want to create the Observation resource. (because OBX type TX is already appended to an associated DiagnosticReport or DocumentReference.) We want the generation of an Observation object to be created only if OBX exists AND OBX.2 is NOT TX.

One would expect the following to work in Observation.yml, but it fails in cases where OBX.2 is TX. The condition fails, and the code element is not created, but the resource is still generated.

code:
   valueOf: datatype/CodeableConcept
   condition: $obx2 NOT_EQUALS_STRING TX
   expressionType: resource
   specs: OBX.3
   required: true
      vars: 
         obx2: STRING, OBX.2

When the above is used, 75 test fail, most with a validation error java.lang.IllegalArgumentException: Validation issues encountered. Bundle.entry[0].resource.ofType(Observation) Profile http://hl7.org/fhir/StructureDefinition/Observation, Element 'Bundle.entry[0].resource.ofType(Observation).code': minimum required = 1, but only found 0 ERROR showing that the Observation is being created without the code element.

A good test case to see this is test_MDMT06_encounter_present in Hl7MDMMessageTest.java

A workaround was found which is putting the logic within a nested expression. This works, but it is needlessly complicated.

# When OBX's are missing or TX based, we don't want to create an Observation.
# When TX, because content is already appended to an associated DiagnosticReport or DocumentReference
# Logic: if OBX exists AND ( (OBX.2 exists AND OBX.2 is NOT TX) OR (OBX.2 is null/empty) ) AND OBX.3 NOT NULL CREATE the Observation.
# 'code' is set 'required: true' to cause all or nothing creation logic
# The expression is nested and values are passed to the nested so that BOTH values control the 'all or nothing'
code:
   expressionType: nested
   required: true
   vars:
     obx3: OBX.3
     obx2: STRING, OBX.2
   expressions:
   -  condition: $obx3 NOT_NULL && $obx2 NOT_EQUALS_STRING TX
      valueOf: datatype/CodeableConcept
      expressionType: resource
      specs: $obx3

Expected behavior
We expect either a false Condition or a NULL spec to prevent the resource from being generated.

Desktop (please complete the following information):

  • Operating system. OSX
  • Version 11.6

Additional context