LinuxForHealth / hl7v2-fhir-converter

Converts HL7 v2 Messages to FHIR Resources
Apache License 2.0
86 stars 34 forks source link

Extensions on FHIR primitives cannot be added with the primitive itself #510

Closed mkalish closed 7 months ago

mkalish commented 11 months ago

Describe the bug Extensions cannot be added to primitive types since the names prefixed with _ (i.e. _birthTime) are dropped when processing schemas.

To Reproduce This is a contrived example to conveniently highlight the bug.

Apply this diff to HumanName.yml

diff --git a/src/main/resources/hl7/datatype/HumanName.yml b/src/main/resources/hl7/datatype/HumanName.yml
index c3ea8c36..0dc77943 100644
--- a/src/main/resources/hl7/datatype/HumanName.yml
+++ b/src/main/resources/hl7/datatype/HumanName.yml
@@ -20,6 +20,20 @@ family:
      type: STRING
      valueOf: XPN.1 |XCN.2 |CNN.2 |NDL.2 | PPN.2
      expressionType: HL7Spec
+
+_family:
+  expressionType: nested
+  expressionsMap:
+    extension_1:
+      generateList: true
+      expressionType: nested
+      expressionsMap:
+        url:
+          type: SYSTEM_URL
+          value: "mothersMaidenName"
+        valueBoolean:
+          type: STRING
+          valueOf: "true"
 given:
     type: STRING
     valueOf: 'GeneralUtils.makeStringArray( first, middle)'

and this test

diff --git a/src/test/java/io/github/linuxforhealth/FHIRConverterTest.java b/src/test/java/io/github/linuxforhealth/FHIRConverterTest.java
index 543c51d7..c1301507 100644
--- a/src/test/java/io/github/linuxforhealth/FHIRConverterTest.java
+++ b/src/test/java/io/github/linuxforhealth/FHIRConverterTest.java
@@ -13,22 +13,15 @@ import java.io.InputStream;
 import java.math.BigDecimal;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Collectors;

 import org.apache.commons.io.IOUtils;
 import org.hl7.fhir.exceptions.FHIRException;
 import org.hl7.fhir.instance.model.api.IBaseResource;
-import org.hl7.fhir.r4.model.Bundle;
+import org.hl7.fhir.r4.model.*;
 import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent;
 import org.hl7.fhir.r4.model.Bundle.BundleType;
-import org.hl7.fhir.r4.model.CodeableConcept;
-import org.hl7.fhir.r4.model.Coding;
-import org.hl7.fhir.r4.model.Identifier;
-import org.hl7.fhir.r4.model.Immunization;
-import org.hl7.fhir.r4.model.Organization;
-import org.hl7.fhir.r4.model.Quantity;
-import org.hl7.fhir.r4.model.Resource;
-import org.hl7.fhir.r4.model.ResourceType;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
@@ -69,6 +62,27 @@ class FHIRConverterTest {

     }

+    @Test
+    void test_patient_human_name_extension() throws IOException {
+
+        String hl7message = "MSH|^~\\&|REGADT|MCM|RSP1P8|MCM|200301051530|SEC|ADT^A40^ADT_A39|00000003|P|2.6\n" +
+                "PID|||MR1^^^XYZ||MAIDENNAME^EVE\n" +
+                "MRG|MR2^^^XYZ\n";
+
+        Bundle b = ftv.convertToBundle(hl7message, OPTIONS, null);
+
+        assertThat(b.getType()).isEqualTo(BundleType.COLLECTION);
+
+        Resource patientResource = b.getEntry().stream()
+                .filter(v -> ResourceType.Patient == v.getResource().getResourceType())
+                .findFirst()
+                .map(BundleEntryComponent::getResource)
+                .get();
+        assertThat(patientResource).isInstanceOf(Patient.class);
+        assertThat(((Patient) patientResource).getName().get(0).getFamily()).isEqualTo("MAIDENNAME");
+        assertThat(((Patient) patientResource).getName().get(0).getFamilyElement().getExtension()).hasSize(1);
+    }
+
     @Test
     void test_patient_encounter() throws IOException {

Expected behavior The expectation is that the test passes and an extension can be added to a primitive FHIR data type. For the above example, the family StringType on all HumanName should include an extension "mothersMaidenName"

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context The problem appears to be in ExpressionUtility.getKeyName which ends up stripping a leading underscore and results in both _family and family converting to family and only one value being persisted in the resolved values

LisaWellman commented 7 months ago

In release 1.1.0